0% found this document useful (0 votes)
13 views13 pages

C Programming Problem Solutions

Uploaded by

maheerahmed4180
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views13 pages

C Programming Problem Solutions

Uploaded by

maheerahmed4180
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Problem 1: Swap Two Numbers

Code:

#include <stdio.h>

int main() {

int a, b, temp;

printf("Enter two numbers: ");

scanf("%d %d", &a, &b);

// Swapping

temp = a;

a = b;

b = temp;

printf("After swapping: a = %d, b = %d\n", a, b);

return 0;

Output:
Problem 2: Sum of Father and Son's Age

Code:

#include <stdio.h>

int main() {

int fatherAge, sonAge, totalAge;

printf("Enter father's age: ");

scanf("%d", &fatherAge);

printf("Enter son's age: ");

scanf("%d", &sonAge);

totalAge = fatherAge + sonAge;

printf("Total age (Father + Son) = %d\n", totalAge);

return 0;

Output:
Problem 3: Convert USD to BDT

Code:

#include <stdio.h>

int main() {

double usd, bdt;

double rate = 122.50; // 1 USD = 122.50 BDT

printf("Enter amount in USD: ");

scanf("%lf", &usd);

bdt = usd * rate;

printf("%.2lf USD = %.2lf BDT\n", usd, bdt);

return 0;

Output:
Problem 4: Sum and Average of Five Subjects

Code:

#include <stdio.h>

int main() {

int s1, s2, s3, s4, s5;

int sum;

double average;

printf("Enter marks of five subjects: ");

scanf("%d %d %d %d %d", &s1, &s2, &s3, &s4, &s5);

sum = s1 + s2 + s3 + s4 + s5;

average = sum / 5.0;

printf("Total marks = %d, Average = %.2lf\n", sum, average);

return 0;

Output:
Problem 5: Area of a Circle

Code:

#include <stdio.h>

int main() {

double radius, area;

const double pi = 3.14159;

printf("Enter radius of the circle: ");

scanf("%lf", &radius);

area = pi * radius * radius;

printf("Area of the circle = %.2lf\n", area);

return 0;

Output:
Problem 6: Area of a Triangle

Code:

#include <stdio.h>

int main() {

double base, height, area;

printf("Enter base and height of the triangle: ");

scanf("%lf %lf", &base, &height);

area = 0.5 * base * height;

printf("Area of the triangle = %.2lf\n", area);

return 0;

Output:
Problem 7: Volume and Surface Area of a Cylinder

Code:

#include <stdio.h>

int main() {

double radius, height;

const double pi = 3.14159;

double volume, surfaceArea;

printf("Enter radius and height of the cylinder: ");

scanf("%lf %lf", &radius, &height);

volume = pi * radius * radius * height;

surfaceArea = 2 * pi * radius * (radius + height);

printf("Volume = %.2lf\n", volume);

printf("Surface Area = %.2lf\n", surfaceArea);

return 0;

Output:
Problem 8 : Calculate average CGPA:

Code:

#include <stdio.h>

int main() {

int n, i;

double cgpa, sum = 0.0, average;

printf("Enter the number of subjects/semesters: ");

scanf("%d", &n);

for(i = 1; i <= n; i++) {

printf("Enter CGPA for subject/semester %d: ", i);

scanf("%lf", &cgpa);

sum += cgpa;

average = sum / n;

printf("Average CGPA = %.2lf\n", average);

return 0;

Output:
Problem 9: Write a program that asks the user to enter the lengths of three sides of a
triangle. The program should then determine if a valid triangle can be formed with
those lengths.

Code:

#include <stdio.h>

int main() {

double a, b, c;

printf("Enter the three sides of the triangle: ");

scanf("%lf %lf %lf", &a, &b, &c);

// Check triangle validity

if (a + b > c && a + c > b && b + c > a) {

printf("A triangle can be formed with sides %.2lf, %.2lf, %.2lf\n", a, b, c);

} else {

printf("A triangle cannot be formed with sides %.2lf, %.2lf, %.2lf\n", a, b, c);

return 0;}

Output:
Problem 10: Create a program that functions as a basic calculator. It should take two
numbers and an operator (+, -, *, /) from the user and perform the corresponding
calculation.

Requirements:

- Ask the user to enter the first number.

- Ask for the operator character.

- Ask for the second number.

- Use a switch statement to determine which operation to perform based on the


operator character.

- Include a special check within the division case to prevent division by zero

Code:

#include <stdio.h>

int main() {

double num1, num2, result;

char op;

printf("Enter the first number: ");

scanf("%lf", &num1);

printf("Enter an operator (+, -, *, /): ");

scanf(" %c", &op);

printf("Enter the second number: ");

scanf("%lf", &num2);

switch(op) {

case '+':

result = num1 + num2;

printf("Result: %.2lf\n", result);

break;

case '-':
result = num1 - num2;

printf("Result: %.2lf\n", result);

break;

case '*':

result = num1 * num2;

printf("Result: %.2lf\n", result);

break;

case '/':

if(num2 != 0) {

result = num1 / num2;

printf("Result: %.2lf\n", result);

} else {

printf("Error: Division by zero is not allowed.\n");

break;

default:

printf("Error: Invalid operator.\n");

return 0;

Output:
Problem 11: write a program that asks the user for the any positive integer and
calculate the sum of its all digits.

Code:

#include <stdio.h>

int main() {

int number, sum = 0, digit;

printf("Enter a positive integer: ");

scanf("%d", &number);

if (number < 0) {

printf("Please enter a positive integer.\n");

return 0;

while (number > 0) {

digit = number % 10;

sum += digit;

number /= 10;

printf("Sum of digits = %d\n", sum);

return 0;

Output:

You might also like