Practical 2 - Introduction To C Programming: 1. Write A C Program To Find The Average of 3 Numbers

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

PRACTICAL 2 - INTRODUCTION TO C PROGRAMMING

Name:- Mayuresh Pandey Date: 23rd Aug 2024 Roll no:-

1. Write a C program to find the average of 3 numbers.


Input:-

#include<stdio.h>

void main()

float a,b,c,d;

printf("Enter three values:\t");

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

printf("The three values uploaded are %f, %f, and%f.\n\n",a,b,c);

d=(a+b+c)/3;

printf(" the average of three numbers %f,%f, and %f is %f .\n\n",a,b,c,d);

Output:-

Enter three values: 2

The three values uploaded are 2.000000, 5.000000, and 6.000000.

the average of three numbers 2.000000,5.000000, and 6.000000 is 4.333333.


2. Write a C program to calculate simple interest.
Input:-

#include<stdio.h>

void main()

float a,b,c,d;

printf("Enter Principal value:\n");

scanf("%f",&a);

printf("Enter Rate of Interest value:\n");

scanf("%f",&b);

printf("Enter Time (number of years) value: \n");

scanf("%f",&c);

printf("The principal value is %f, Rate of interest is %f, and Time in years is %f.\n\n",a,b,c);

d=(a*b*c)/100;

printf("The simple interest deduced is %f.\n\n",d);

Output:-

Enter Principal value:

100000

Enter Rate of Interest value:

20000

Enter Time (number of years) value:

The principal value is 100000.000000, Rate of interest is 20000.000000, and Time in years is
5.000000.

The simple interest deduced is 100000000.000000.


3. Write a C program to convert the distance from kilometers to meters,
centimeters, feet and inches.
Input:-

#include<stdio.h>

void main()

float a,b,c,d,e;

printf("Enter Kilometer value: \n");

scanf("%f",&a);

printf("Value entered is %f Kilometers.\n\n",a);

b=a*1000;

printf("%f kilometers converted into meters is %f meters.\n\n",a,b);

c=b*100;

printf("%f kilometers converted into centimeters is %f centimeters.\n\n",a,c);

d=a*3281;

printf("%f kilometers converted into feet is %f feet.\n\n",a,d);

e=d*12;

printf("%f kilometers converted into inches is %f inches.\n\n",a,e);

Output:-

Enter Kilometer value:

Value entered is 4.000000 Kilometers.

4.000000 kilometers converted into meters is 4000.000000 meters.

4.000000 kilometers converted into centimeters is 400000.000000 centimeters.

4.000000 kilometers converted into feet is 13123.360352 feet.

4.000000 kilometers converted into inches is 157480.328125 inches.


4. Write a C program to convert time from hours to seconds.
Input:-

#include<stdio.h>

void main()

float a,b;

printf("Enter Time (in hours): \n");

scanf("%f",&a);

printf("Time entered is %f hours.\n\n",a);

b=a*3600;

printf("%f hours converted into seconds is %f seconds.\n\n",a,b);

Output:-

Enter Time (in hours):

Time entered is 1.000000 hours.

1.000000 hours converted into seconds is 3600.000000 seconds.


5. Write a C program to find the area and volume of a cube.
Input:-
#include<stdio.h>

void main()

float a,area,volume;

printf("Enter side of a cube:\t");

scanf("%f",&a);

printf(" The side of a cube is %f.\n\n",a);

area=6*(a*a);

volume=a*a*a;

printf("The area of cube is %f.\n\n",area);

printf("The volume of cube is %f.\n\n",volume);

Output:-
Enter side of a cube: 3

The side of a cube is 3.000000.

The area of cube is 54.000000.

The volume of cube is 27.000000.


6. Write a C program to print a number its square and its cube.
Input:-

#include<stdio.h>
void main()
{
int a,b,c;
printf("Enter a value:\n");
scanf("%d",&a);
printf("Value entered is %d.\n\n",a);
b=a*a;
c=a*a*a;
printf("square of %d is %d,",a,b);
printf("and cube of %d is %d,\n\n",a,c);
}
Output:-
Enter a value:

25

Value entered is 25.

square of 25 is 625,and cube of 25 is 15625,


7. Write a C program to find the sum, difference, product, division and mod of
two numbers.
Input:-
#include<stdio.h>

void main()

int a,b;

printf("Enter 2 integer values: \n");

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

printf("The value of a is %d and b is %d\n\n",a,b);

int c,d,e,f,g;

c=a+b;

d=a-b;

e=a*b;

f=a/b;

g=a%b;

printf("The addition of a and b is %d \n\n",c);

printf("The subtraction of a and b is %d \n\n",d);

printf("The multiplication of a and b is %d \n\n",e);

printf("The division of a and b is %d \n\n",f);

printf("The modulus of a and b is %d \n\n",g);

Output:-
Enter 2 integer values:

The value of a is 6 and b is 4

The addition of a and b is 10

The subtraction of a and b is 2

The multiplication of a and b is 24

The division of a and b is 1


The modulus of a and b is 2

8. Write a C program to calculate gross monthly salary of Ramesh using


Basic+DA+HRA from basic salary. Calculate DA by 40% of basic, HRA by
20% of basic.
Input:-
#include<stdio.h>

void main()

float bs,da,hra,gs;/*bs: basic salary,da:dearness allowance,

hra: house rent allowance,gs: gross salary*/

printf("Enter basic salary of Ramesh: \t");

scanf("%f",&bs);

da=0.40*bs;

hra=0.20*bs;

gs=bs+da+hra;

printf("Basic salary of Ramesh is %f rupees.\n\n",bs);

printf("The gross salary of Ramesh is %f rupees.\n\n",gs);

Output:-
Enter basic salary of Ramesh: 20000

Basic salary of Ramesh is 20000.000000 rupees.

The gross salary of Ramesh is 32000.000000 rupees.


9. Write a C program to print the total marks obtained and percentage of seven
subjects.
Input:-
#include<stdio.h>

void main()

float eng, mat, sci, alg, geo ,socio, polsci, sum, perc;

printf("Enter marks obtained in English: \t");

scanf("%f",&eng);

printf("Enter marks obtained in Maths: \t");

scanf("%f",&mat);

printf("Enter marks obtained in Science: \t");

scanf("%f",&sci);

printf("Enter marks obtained in Algebra: \t");

scanf("%f",&alg);

printf("Enter marks obtained in Geometery: \t");

scanf("%f",&geo);

printf("Enter marks obtained in Sociology: \t");

scanf("%f",&socio);

printf("Enter marks obtained in Political Science: \t");

scanf("%f",&polsci);

sum=eng+mat+sci+alg+geo+socio+polsci;

perc=(eng+mat+sci+alg+geo+socio+polsci)/700*100;

printf("The sum of all marks is %f.\n\n",sum);

printf("The percentage obtained is %f%f.\n\n",perc);

}
Output:-
Enter marks obtained in English: 80

Enter marks obtained in Maths: 80

Enter marks obtained in Science: 80

Enter marks obtained in Algebra: 80

Enter marks obtained in Geometery: 80

Enter marks obtained in Sociology: 80

Enter marks obtained in Political Science: 80

The sum of all marks is 560.000000.

The percentage obtained is 80.000

10. Write a C program to calculate area and perimeter of the rectangle. Program
should accept length and breadth of a rectangle as input.
Input:-
#include<stdio.h>

void main()

float l,b,area,per;

printf("Enter length and breadth of rectangle: \t");

scanf("%f%f",&l,&b);

printf("The length and breadth of rectangle is %f and %f.\n\n",l,b);

area=l*b;

per=2*(l+b);

printf("The area of rectangle is %f\n\n",area);

printf("The perimeter of Rectangle is %f\n\n",per);

Output:-
Enter length and breadth of rectangle: 5 6

The length and breadth of rectangle is 5.000000 and 6.000000.

The area of rectangle is 30.000000

The perimeter of Rectangle is 22.000000


11. Write a C program to calculate area and circumference of circle. Program
should accept radius as input.
Input:-
#include<stdio.h>

void main()

float radius,area,circum;

printf("Enter radius of circle: \t");

scanf("%f",&radius);

area=3.14*(radius*radius);

circum=2*3.14*radius;

printf("The area of circle is %f\n\n",area);

printf("The circumference of circle is%f\n\n",circum);

Output:-
Enter radius of circle: 10

The area of circle is 314.000000

The circumference of circle is62.799999


12. Write a C program to interchange the contents of A and B variables with and
without temporary variable.
Input:-
#include<stdio.h>

void main()

int x,a,b,c,sum;

printf("Enter three digit number: \t");

scanf("%d",&x);

printf("The number entered is %d.\n\n",x);

a=x/100;

b=(x%100)/10;

c=x%10;

sum=a+b+c;

printf("The sum of all three numbers is %d.\n\n",sum);

printf("The three numbers read in reverse are %d%d%d.\n\n",c,b,a);

Output:-
Enter three digit number: 125

The number entered is 125.

The sum of all three numbers is 8.

The three numbers read in reverse are 521.


13. Write a C program to read a 3 digit number from the user then find the sum of
its digits and print the reverse of the number.
Input:-

#include<stdio.h>

void ()

int x,y,temp;

printf("Enter first value: \t");

scanf("%d",&x);

printf("Enter second value: \t");

scanf("%d",&y);

//with temporary value

temp=y;

y=x;

x=temp;

printf("The value interchanged will turn first value into %d and second value into %d\n\n",x,y);

// without temporary value

x=x+y;

y=x-y;

x=x-y;

printf("The value now turns into original value with first value is%d and second value is %d\n\
n",x,y);

Output:-
Enter first value: 40

Enter second value: 32

The value interchanged will turn first value into 32 and second value into 40

The value now turns into original value with first value is40 and second value is 32
14. Write a C program to find and print the total marks obtained and percentage of
5 subjects.
Input:-
#include<stdio.h>

void main()

float eng, mat, sci, alg, geo , sum, perc;

printf("Enter marks obtained in English: \t");

scanf("%f",&eng);

printf("Enter marks obtained in Maths: \t");

scanf("%f",&mat);

printf("Enter marks obtained in Science: \t");

scanf("%f",&sci);

printf("Enter marks obtained in Algebra: \t");

scanf("%f",&alg);

printf("Enter marks obtained in Geometery: \t");

scanf("%f",&geo);

sum=eng+mat+sci+alg+geo;

perc=(eng+mat+sci+alg+geo)/500*100;

printf("The sum of all marks is %f.\n\n",sum);

printf("The percentage obtained is %f%f.\n\n",perc);

Output:-
Enter marks obtained in English: 50

Enter marks obtained in Maths: 50

Enter marks obtained in Science: 50

Enter marks obtained in Algebra: 50

Enter marks obtained in Geometery: 50

The sum of all marks is 250.000000.

The percentage obtained is 50.000000


15. Write a C program that converts temperature. (F=9C/5+32)
a) from Celsius to Fahrenheit
b) from Fahrenheit to Celsius
Input:-
#include<stdio.h>

void main()

int c,f;

printf("Enter temperature in celcius: \t");

scanf("%d",&c);

f=(9*c)/5+32;

c=(5*f)/9-32;

printf("Temperature from celcius to farenheit is%df.\n\n" ,f);

printf("Temperature from farenheit to celcius is%dc.\n\n" ,c);

Output:-

Enter temperature in celcius: 40

Temperature from celcius to farenheit is104f.

Temperature from farenheit to celcius is25c.

You might also like