Write a C program to calculate simple interest. The program
should accept the principal amount, rate of interest, and time period as input.
Solution:
#include <stdio.h>
int main() {
float principal, rate, time, simple_interest;
printf("Enter the principal amount: ");
scanf("%f", &principal);
printf("Enter the annual rate of interest (in percentage): ");
scanf("%f", &rate);
printf("Enter the time period (in years): ");
scanf("%f", &time);
simple_interest = (principal * rate * time) / 100;
printf("\nSimple Interest = %.2f\n", simple_interest);
return 0;
}Question 3: Find the Largest of Three Numbers
Problem: Write a C program to find the largest of three different numbers entered by the user.
Solution:
#include <stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 >= num2 && num1 >= num3) {
printf("The largest number is: %d\n", num1);
} else if (num2 >= num1 && num2 >= num3) {
printf("The largest number is: %d\n", num2);
} else {
printf("The largest number is: %d\n", num3);
}
return 0;
}Question 4: Print Numbers from 1 to 10
Problem: Write a C program to print numbers from 1 to 10 using a loop.
Solution:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}
Question 5: Calculate Sum and Average
Problem: Write a C program to find the sum and average of two numbers entered by the user.
Solution:
#include <stdio.h>
int main() {
int num1, num2, sum;
float average;
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
sum = num1 + num2;
average = (float)sum / 2;
printf("\nSum = %d\n", sum);
printf("Average = %.2f\n", average);
return 0;
}
Question 6: Area and Perimeter of a Rectangle
Problem: Write a C program to calculate the area and perimeter of a rectangle. The program should take the length and width as input.
Solution:
#include <stdio.h>
int main() {
float length, width, area, perimeter;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
perimeter = 2 * (length + width);
printf("\nArea of the rectangle = %.2f\n", area);
printf("Perimeter of the rectangle = %.2f\n", perimeter);
return 0;
}Question 7: Celsius to Fahrenheit Conversion
Problem: Write a C program to convert temperature from Celsius to Fahrenheit.
Solution:
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("\nTemperature in Fahrenheit = %.2f\n", fahrenheit);
return 0;
}
Question 8: Factorial of a Number
Problem: Write a C program to find the factorial of a positive integer entered by the user.
Solution:
#include <stdio.h>
int main() {
int n, i;
unsigned long long factorial = 1; // Use long long for larger numbers
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
for (i = 1; i <= n; ++i) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", n, factorial);
}
return 0;
}
Question 9: Check for a Leap Year
Problem: Write a C program to check if a year is a leap year or not.
Solution:
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}
return 0;
}Question 10: Sum of Digits
Problem: Write a C program to find of sum of digits of given number.
Solution:
#include <stdio.h>
int main() {
int n, sum = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
// Loop continues until n becomes 0
while (n != 0) {
remainder = n % 10;
sum += remainder;
n /= 10;
}
printf("Sum of digits = %d\n", sum);
return 0;
}