C Programming: 10 Problems and solutions
1. Question: Write a program that displays the distance from Sylhet to Dhaka (245 km) in miles, meters, and centimeters.
Solution:
#includeint main()
{
int syl_dha_km;
float syl_dha_miles, syl_dha_meters, syl_dha_cms;
syl_dha_km = 245;
syl_dha_miles = syl_dha_km * 0.621371;
syl_dha_meters = syl_dha_km * 1000;
syl_dha_cms = syl_dha_meters * 100;
printf("Sylhet To Dhaka (in KMs): %.3f\n", syl_dha_km);
printf("Sylhet To Dhaka (in Miles): %.3f\n", syl_dha_miles);
printf("Sylhet To Dhaka (in Meters): %.3f\n", syl_dha_meters);
printf("Sylhet To Dhaka (in Centimeters): %.3f\n", syl_dha_cms);
return 0;
}
2. Question: Write a program that will take the value of ‘a’ and ‘b’ from the user and calculate the area of an ellipse using the formula: Area = πab (where π = 3.14).
Solution:
#includeint main()
{
float const pi = 3.14;
float a, b, area;
printf("Enter the value of a: ");
scanf("%f", &a);
printf("Enter the value of b: ");
scanf("%f", &b);
area = pi * a * b;
printf("The Area = %.2f", area);
return 0;
}
3. Question: In a supermarket, the prices of some products are given below:
Apple = Tk. 140/kg
Orange = Tk. 90/kg
Grapes = Tk. 360/kg
Write a program that will take the amount of some products (e.g., Apple 2.5 kg, Orange 2 kg, Grapes 1 kg) as input and display the individual price for each product bought and the total bill for all products.
Solution:
#includeint main()
{
float amount_of_apple, amount_of_orange, amount_of_grapes;
float price_of_apple, price_of_orange, price_of_grapes, total;
float apple_per_kg = 140;
float orange_per_kg = 90;
float grapes_per_kg = 360;
printf("Enter the amount of Apple (in Kg): ");
scanf("%f", &amount_of_apple);
printf("Enter the amount of Orange (in Kg): ");
scanf("%f", &amount_of_orange);
printf("Enter the amount of Grapes (in Kg): ");
scanf("%f", &amount_of_grapes);
price_of_apple = apple_per_kg * amount_of_apple;
price_of_orange = orange_per_kg * amount_of_orange;
price_of_grapes = grapes_per_kg * amount_of_grapes;
total = price_of_apple price_of_orange price_of_grapes;
printf("\n\nThe Price of Apple = TK %.2f\n", price_of_apple);
printf("The Price of Orange = TK %.2f\n", price_of_orange);
printf("The Price of Grapes = TK %.2f\n", price_of_grapes);
printf("The Total Price = TK %.2f", total);
return 0;
}
4. Question: Write a program that will take five numbers from the user and display the total and average of those numbers.
Solution:
#includeint main()
{
float n1, n2, n3, n4, n5, total, average;
printf("Enter the number 1: ");
scanf("%f", &n1);
printf("Enter the number 2: ");
scanf("%f", &n2);
printf("Enter the number 3: ");
scanf("%f", &n3);
printf("Enter the number 4: ");
scanf("%f", &n4);
printf("Enter the number 5: ");
scanf("%f", &n5);
total = n1 n2 n3 n4 n5;
average = total / 5.0;
printf("\n\nTotal = %.2f\n", total);
printf("Average = %.2f\n", average);
return 0;
}
5. Question: The interest rate of Fixed Deposit in a bank is 8.5%. Write a program that will take1 the initial deposit and number of years from the user and calculate the total amount after maturity. (Assume simple interest)
Solution:
#includeint main()
{
float rate = 8.5 / 100;
float initial_deposit, number_of_years, amount, total;
printf("Enter the Initial Deposit: ");
scanf("%f", &initial_deposit);
printf("Enter the number of years: ");
scanf("%f", &number_of_years);
amount = initial_deposit * rate * number_of_years;
total = initial_deposit amount;
printf("\nThe Total Amount (after %.0f years) = TK %.2f", number_of_years, total);
return 0;
}
6. Question: The interest rate of Fixed Deposit in a bank is 7.5%. Write a program that will take the initial deposit and number of years from the user and calculate the total amount after maturity. (Assume compound interest)
Solution:
#include#include
int main()
{
float rate = 7.5 / 100;
float p, t, n, amount, total, temp, temp2;
printf("Enter the Initial Deposit: ");
scanf("%f", &p);
printf("Enter the number of years: ");
scanf("%f", &t);
printf("Enter the term of comp. per year: ");
scanf("%f", &n);
temp2 = n * t;
temp = 1 rate / n;
amount = p * pow(temp, temp2);
total = amount;
printf("\nThe Total Amount (Comp.) = TK %.2f", t, total);
return 0;
}
7. Question: Write a program that will take two numbers from the user and display the bigger number between those.
Solution:
#includeint main()
{
float num1, num2;
printf("Enter number 1: ");
scanf("%f", &num1);
printf("Enter number 2: ");
scanf("%f", &num2);
if (num1 > num2)
{
printf("\nThe Number 1 is Biggest.\n");
} else {
printf("\nThe Number 2 is Biggest.\n");
}
return 0;
}
8. Question: Write a program that will take a number from the user and display whether the number is positive or negative.
Solution:
#includeint main()
{
float num;
printf("Enter Number: ");
scanf("%f", &num);
if (num > 0)
{
printf("\nThe Number Positive.\n");
} else {
printf("\nThe Number is Negative.\n");
}
return 0;
}
9. Question: Write a program that will take a character from the user and display whether the character is vowel or consonant.
Solution:
#includeint main()
{
char latter;
printf("Enter The Latter (ex: A, a, P, p etc.): ");
scanf("%c", &latter);
if (latter == 'a' || latter == 'e' || latter == 'i' || latter == 'o' || latter == 'u' ||
latter == 'A' || latter == 'E' || latter == 'I' || latter == 'O' || latter == 'U') {
printf("This is a vowel.\n");
} else {
printf("This is not a vowel.\n");
}
return 0;
}
10. Question: Write an inheritance calculator in C that will take an input (Total wealth of a deceased person) from the user and divide that amount among his wife, 1 son and 1 daughter. The rule for dividing the wealth is as follows: Wife gets 12.5% of total wealth, Son gets 58.33% of total wealth, Daughter gets 29.17% of total wealth.
Solution:
#includeint main()
{
float wealth, son_get, daughter_get, wife_get;
printf("Enter the total wealth: ");
scanf("%f", &wealth);
son_get = wealth * 58.33 / 100;
daughter_get = wealth * 29.17 / 100;
wife_get = wealth * 12.5 / 100;
printf("Son Gets = %.2f of %.2f\n", son_get, wealth);
printf("Daughter Gets = %.2f of %.2f\n", daughter_get, wealth);
printf("Wife Gets = %.2f of %.2f\n", wife_get, wealth);
return 0;
}
Parvej Husen Talukder
- Parvej Husen Talukder's profile
- 8 followers

