LAB1
Q1)
Write a program in C to understand the input and output process.
Input Code:
#include <stdio.h>
int main() {
int number;
printf("This program demonstrates input and output process in C\n");
printf("Enter an integer: ");
scanf("%d", &number);
printf("You entered: %d\n", number);
return 0;
}
Output Code Screenshot:
Q2)
Write a program to print an integer and a float value.
Input Code:
#include <stdio.h>
int main() {
int integer_value = 42;
float float_value = 3.14159;
printf("Integer value: %d\n", integer_value);
printf("Float value: %.2f\n", float_value);
return 0;
}
Output Code Screenshot:
Q3)
Write a program to find the sum, difference, multiplication, division, and average of two
numbers.
Input Code:
#include <stdio.h>
int main() {
float num1, num2;
float sum, difference, multiplication, division, average;
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
sum = num1 + num2;
difference = num1 - num2;
multiplication = num1 * num2;
division = num1 / num2;
average = (num1 + num2) / 2;
printf("Results:\n");
printf("Sum: %.2f\n", sum);
printf("Difference: %.2f\n", difference);
printf("Multiplication: %.2f\n", multiplication);
printf("Division: %.2f\n", division);
printf("Average: %.2f\n", average);
return 0;
}
Output Code Screenshot:
Q4)
Write a program to find the Simple Interest.
Input Code:
#include <stdio.h>
int main() {
float principal, rate, time, si;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time period: ");
scanf("%f", &time);
si = (principal * rate * time) / 100;
printf("Simple Interest: %.2f\n", si);
return 0;
}
Output Code Screenshot:
Lab 2
Q1)
Write a program to print the size of different data types.
Input Code:
#include <stdio.h>
int main() {
printf("Size of int: %lu bytes\n", sizeof(int));
printf("Size of float: %lu bytes\n", sizeof(float));
printf("Size of double: %lu bytes\n", sizeof(double));
printf("Size of char: %lu byte\n", sizeof(char));
return 0;
}
Output Code Screenshot:
Q2)
Write a program in C to print area of circle when radius of circle is entered by user.
Input Code:
#include <stdio.h>
int main() {
float radius, area;
printf("Enter radius of circle: ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area of circle: %.2f\n", area);
return 0;
}
Output Code Screenshot:
Q3)
Write a program to compute quotient and remainder when dividend and divisor are given.
Input Code:
#include <stdio.h>
int main() {
int dividend, divisor, quotient, remainder;
printf("Enter dividend: ");
scanf("%d", ÷nd);
printf("Enter divisor: ");
scanf("%d", &divisor);
quotient = dividend / divisor;
remainder = dividend % divisor;
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);
return 0;
}
Output Code Screenshot:
Q4)
Write a program in C to compute the perimeter and area of rectangle with a height of 7
inches and width of 5 inches.
Input Code:
#include <stdio.h>
int main() {
int height = 7, width = 5;
int perimeter, area;
perimeter = 2 * (height + width);
area = height * width;
printf("Rectangle with height %d inches and width %d inches:\n",
height, width);
printf("Perimeter: %d inches\n", perimeter);
printf("Area: %d square inches\n", area);
return 0;
}
Output Code Screenshot:
Q5)
Write a program in C to convert specified days into years, weeks, and days. Ignore leap year.
Input Code:
#include <stdio.h>
int main() {
int days, years, weeks, remaining_days;
printf("Enter number of days: ");
scanf("%d", &days);
years = days / 365;
remaining_days = days % 365;
weeks = remaining_days / 7;
remaining_days = remaining_days % 7;
printf("%d days = %d years, %d weeks, %d days\n", days, years,
weeks, remaining_days);
return 0;
}
Output Code Screenshot:
Lab 3
Q1)
Write a program in C to check whether the entered number is prime or not.
Input Code:
#include <stdio.h>
int main() {
int num, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &num);
if (num <= 1) flag = 1;
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number\n", num);
else
printf("%d is not a prime number\n", num);
return 0;
}
Output Code Screenshot:
Q2)
Write a program in C to swap two numbers.
Input Code:
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
printf("Before swapping: a = %d, b = %d\n", a, b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Output Code Screenshot:
Q3)
Write a program in C to multiply two floating point numbers.
Input Code:
#include <stdio.h>
int main() {
float num1, num2, result;
printf("Enter first floating point number: ");
scanf("%f", &num1);
printf("Enter second floating point number: ");
scanf("%f", &num2);
result = num1 * num2;
printf("Result: %.2f * %.2f = %.2f\n", num1, num2, result);
return 0;
}
Output Code Screenshot:
Q4)
Write a program in C to print the ASCII value of characters.
Input Code:
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
printf("ASCII value of '%c' is %d\n", ch, ch);
return 0;
}
Output Code Screenshot:
Q5)
Write a program in C to evaluate the sum of a five-digit number.
Input Code:
#include <stdio.h>
int main() {
int num, sum = 0, digit;
printf("Enter a five-digit number: ");
scanf("%d", &num);
while (num > 0) {
digit = num % 10;
sum += digit;
num /= 10;
}
printf("Sum of digits: %d\n", sum);
return 0;
}
Output Code Screenshot: