0:19 AM 2008-2009 question 2 |
program to calculate the area of a square , triangle , circle ,, using :
1- switch
2- if-else
switch
#include"stdio.h"
main()
{
float x, y;
int a;
printf("area of :\n 1- square\n 2- tringle\n 3- circle\n\nchoose : ");
scanf("%d", &a);
switch(a)
{
case 1:
printf("enter the length of square side : ");
scanf("%f", &x);
printf("area of square is %.2f", x*x);
break;
case 2:
printf("enter base and height of triangle : ");
scanf("%f%f", &x, &y);
printf("area of triangle is %.2f", 0.5*x*y);
break;
case 3:
printf("enter radius of circle : ");
scanf("%f", &x);
printf("area of circle is %.2f", 3.14*x*x);
break;
default:printf("wrong number !");
break;
}
getch();
return 0;
}
if-else
#include"stdio.h"
main()
{
float x, y;
int a;
printf("area of :\n 1- square\n 2- tringle\n 3- circle\n\nchoose : ");
scanf("%d", &a);
if(a==1){
printf("enter the length of square side : ");
scanf("%f", &x);
printf("area of square is %.2f", x*x);
}else if(a==2){
printf("enter base and height of triangle : ");
scanf("%f%f", &x, &y);
printf("area of triangle is %.2f", 0.5*x*y);
}else if(a==3){
printf("enter radius of circle : ");
scanf("%f", &x);
printf("area of circle is %.2f", 3.14*x*x);
}else{
printf("wrong number !");
}
getch();
return 0;
}
|
|
Total comments: 0 | |