Tuesday, January 17, 2012

Menu Driven Calculator


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch;
clrscr();
printf(“\n1.add\n2.subtract\n3.multiply\n4.division\n5.remainder\n);
printf(“\nenter your choice\n”);
scanf(%d”,&ch);
switch(ch)
{
case1:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a+b;
printf(“\nthe answer is %d”,c);
break;
case2:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a-b; 
printf(“\nthe answer is %d”,c);
break;
case3:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a*b; 
printf(“\nthe answer is %d”,c);
break;
case4:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a/b; 
printf(“\nthe answer is %d”,c);
break;
case5:
printf(“\nenter values of a and b\n”);
scanf(%d%d”,&a,&b);
c=a%b; 
printf(“\nthe answer is %d”,c);
break;
default:
printf(“\nenter the correct choice”);
break;
}
getch();
}

Output:
1.add
2.subtract
3.multiply
4.division
5.remainder
enter your choice
2
enter the values  of a and b
7
4
the answer is 3 

Greatest of two Numbers - Conditional Operator


#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“enter the 2 numbers”);
scanf(%d%d”,&a,&b);
(a>b?printf(“a is greater”):printf(“b is greater”));
getch();
}

Output:
enter the two numbers
6
3
a is greater 

Area and Perimeter of Square


#include<stdio.h>
#include<conio.h>
void main()
{
float s,a,p;
clrscr();
printf(“enter the s value:\n”);
scanf(%f”,&s);
a=s*s;
p=4*s;
printf(“area=%f\n perimeter=%f\n”,a,p);
getch();
}
Output:
enter the s value:
5
area=25.000000
perimeter=20.000000 

Area and Perimeter of Rectangle


#include<stdio.h>
#include<conio.h>
void main()
{
float a,b,l,p;
clrscr();
printf(“enter the l and b value:\n”);
scanf(%f%f”,&l,&b);
a=l*b;
p=2*(l+b);
printf(“area=%f\n perimeter=%f\n”,a,p);
getch();
}


Output:
enter the l and b value:
6
8
area=48.000000
perimeter=28.000000