Simple Programs Using Operators
| Program to find
area of circle. Hint: Area = PI * r * r |
||||||
| #include<stdio.h> #include<conio.h> void main() { float r,a; clrscr(); printf("Enter Radius of Circle:"); scanf("%f",&r); a=3.14*r*r; printf("Area of Circle is %f",a); getch(); } Output Enter Radius of Circle: 3 Area of Circle is 28.260000 |
||||||
| Program to find area of cylinder. Hint: Area = 2 * PI * r * h |
||||||
| #include<stdio.h> #include<conio.h> void main() { float r,h,a; clrscr(); printf("Enter Radius:"); scanf("%f",&r); printf("Enter Height:"); scanf("%f",&h); a= 2* 3.14 * r * h; printf("Area of Cylinder is %f",a); getch(); } Output Enter Radius 2 Enter Height: 5 Area of Cylinder is 62.800000 |
||||||
| Program to find area of Triangle. Hint: Area = (0.5) * b * h |
||||||
| #include<stdio.h> #include<conio.h> void main() { float b,h,a; clrscr(); printf("Enter Base:"); scanf("%f",&b); printf("Enter Height:"); scanf("%f",&h); a= (0.5) * b * h; printf("Area of Trinagle is %f",a); getch(); } Output Enter Base 10 Enter Height 5 Area of Triangle is 25 |
||||||
| Program to find area of Rectangle. Hint: Area = w * h |
||||||
| #include<stdio.h> #include<conio.h> void main() { float w,h,a; clrscr(); printf("Enter Width:"); scanf("%f",&w); printf("Enter Height:"); scanf("%f",&h); a= w * h; printf("Area of Rectangle is %f",a); getch(); } Output Enter Width: 5 Enter Height: 6 Area of Rectangle is 30.000000 |
||||||
| Program to enter temprature in celcius and convert it into feranhit. | ||||||
| #include<stdio.h> #include<conio.h> void main() { float f,c; clrscr(); printf("Enter temperature in celcius:"); scanf("%f",&c); f=1.8*(c+32); printf("Feranhit=%f",f); getch(); } Output Enter Temperature in Celcius: 23 Feranhit= 99 |
||||||
| Program to enter temprature in feranhit and convert it into celcius. | ||||||
| #include<stdio.h> #include<conio.h> void main() { float f,c; clrscr(); printf("Enter temperature in feranhit:"); scanf("%f",&f); c=0.56*(f-32); printf("Celcius=%f",c); getch(); } Output Enter Temperature in Feranhit 99 Celcius=37.520000 |
||||||
| program to swap (exchange) value of two numbers using third variable. Hint: c=a, a=b, b=c | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter Value of A:"); scanf("%d",&a); printf("Enter Value of B:"); scanf("%f",&b); printf("Before Swap:\n"); printf("A=%d\nB=%d\n",a,b); c=a; a=b; b=c; printf("After Swap:\n"); printf("A=%d\nB=%d\n",a,b); getch(); } Output Enter Value of A:2 Enter Value of B:3 Before Swap: A=2 B=3 After Swap A=3 B=2 |
||||||
| program to swap (exchange) value of two numbers without using third variable. Hint: a=a+b, b=a-b, a=a-b | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter Value of A:"); scanf("%d",&a); printf("Enter Value of B:"); scanf("%f",&b); printf("Before Swap:\n"); printf("A=%d\nB=%d\n",a,b); a=a+b; b=a-b; a=a-b; printf("After Swap:\n"); printf("A=%d\nB=%d\n",a,b); getch(); } Output Enter Value of A:2 Enter Value of B:3 Before Swap: A=2 B=3 After Swap A=3 B=2 |
||||||
| Program to Enter days and convert them into year,month and days. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int day,month,year; clrscr(); printf("Enter Total Days:"); scanf("%d",&day); year=day/365; day=day%365; month=day/30; day=day%30; printf("Year=%d\nMonth=%d\nDay=%d",year,month,day); getch(); } Output Enter Total Days 402 Year=1 Month=1 Day=7 |
||||||
| Program to find maximum from two numbers using conditional operator. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter Value of a:"); scanf("%d",&a); printf("Enter Value of b:"); scanf("%d",&b); (a>b)?printf("%d is maximum",a):printf("%d is maximum",b); getch(); } Output Enter Value of a: 30 Enter Value of B: 25 30 is maximum |
||||||
| Program to find maximum from three numbers using conditional operator. | ||||||
| #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter Value of a:"); scanf("%d",&a); printf("Enter Value of b:"); scanf("%d",&b); printf("Enter Value of c:"); scanf("%d",&c); ((a>b) && (a>c))?printf("%d is maximum",a):((b>c)&&(b>a))?printf("%d is maximum",b):printf("%d is maximum",c); getch(); } Output Enter Value of A:21 Enter Value of B:32 Enter Value of C:45 45 is maximum |