0% found this document useful (0 votes)
16 views

C_Programming_Solutions

Uploaded by

Anish Gelal
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)
16 views

C_Programming_Solutions

Uploaded by

Anish Gelal
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
You are on page 1/ 10

C Programming Solutions

1. Program to print 'Hello World'

#include <stdio.h>

int main() {

printf("Hello World

");

return 0;

Output:

Hello World

2. Program to find the sum of two numbers

#include <stdio.h>

int main() {

int num1, num2, sum;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

sum = num1 + num2;

printf("Sum = %d

", sum);

return 0;

Output:

Enter two numbers: 5 8


Sum = 13

3. Program to find the simple interest

#include <stdio.h>

int main() {

float principal, rate, time, interest;

printf("Enter principal amount, rate of interest, and time: ");

scanf("%f %f %f", &principal, &rate, &time);

interest = (principal * rate * time) / 100;

printf("Simple Interest = %.2f

", interest);

return 0;

Output:

Enter principal amount, rate of interest, and time: 1000 5 2

Simple Interest = 100.00

4. Program to find the area of a circle

#include <stdio.h>

#define PI 3.14

int main() {

float radius, area;

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

scanf("%f", &radius);

area = PI * radius * radius;


printf("Area of circle = %.2f

", area);

return 0;

Output:

Enter radius of the circle: 5

Area of circle = 78.50

5. Program to convert Celsius to Fahrenheit

#include <stdio.h>

int main() {

float celsius, fahrenheit;

printf("Enter temperature in Celsius: ");

scanf("%f", &celsius);

fahrenheit = (celsius * 9/5) + 32;

printf("Temperature in Fahrenheit = %.2f

", fahrenheit);

return 0;

Output:

Enter temperature in Celsius: 25

Temperature in Fahrenheit = 77.00

6. Program to find total and percentage of five subjects

#include <stdio.h>
int main() {

float marks[5], total = 0, percentage;

int i;

printf("Enter marks for 5 subjects: ");

for(i = 0; i < 5; i++) {

scanf("%f", &marks[i]);

total += marks[i];

percentage = (total / 500) * 100;

printf("Total = %.2f

", total);

printf("Percentage = %.2f

", percentage);

return 0;

Output:

Enter marks for 5 subjects: 90 85 78 88 92

Total = 433.00

Percentage = 86.60

7. Program to check whether a number is divisible by 5 and 8

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);
if (num % 5 == 0 && num % 8 == 0) {

printf("The number is divisible by both 5 and 8.

");

} else {

printf("The number is not divisible by both 5 and 8.

");

return 0;

Output:

Enter a number: 40

The number is divisible by both 5 and 8.

8. Program to check if a character is a vowel or a consonant

#include <stdio.h>

#include <ctype.h>

int main() {

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

ch = tolower(ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {

printf("%c is a vowel.

", ch);

} else {

printf("%c is a consonant.
", ch);

return 0;

Output:

Enter a character: A

A is a vowel.

9. Program to check if a number is even or odd

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0) {

printf("%d is even.

", num);

} else {

printf("%d is odd.

", num);

return 0;

Output:

Enter a number: 7

7 is odd.
10. Program to find the largest number among three numbers

#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("%d is the largest.

", num1);

} else if (num2 > num1 && num2 > num3) {

printf("%d is the largest.

", num2);

} else {

printf("%d is the largest.

", num3);

return 0;

Output:

Enter three numbers: 4 7 2

7 is the largest.

11. Program to find the smallest among three numbers using nested if-else

#include <stdio.h>

int main() {
int num1, num2, num3;

printf("Enter three numbers: ");

scanf("%d %d %d", &num1, &num2, &num3);

if (num1 <= num2) {

if (num1 <= num3) {

printf("%d is the smallest.

", num1);

} else {

printf("%d is the smallest.

", num3);

} else if (num2 <= num3) {

printf("%d is the smallest.

", num2);

} else {

printf("%d is the smallest.

", num3);

return 0;

Output:

Enter three numbers: 3 6 2

2 is the smallest.

12. Program to calculate the power of a number using pow() function

#include <stdio.h>
#include <math.h>

int main() {

double base, exponent, result;

printf("Enter base and exponent: ");

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

result = pow(base, exponent);

printf("Result = %.2lf

", result);

return 0;

Output:

Enter base and exponent: 2 3

Result = 8.00

13. Program to calculate the remainder of two numbers using modulus operator

#include <stdio.h>

int main() {

int num1, num2, remainder;

printf("Enter two numbers: ");

scanf("%d %d", &num1, &num2);

remainder = num1 % num2;

printf("Remainder = %d

", remainder);

return 0;

Output:
Enter two numbers: 10 3

Remainder = 1

You might also like