Mega Code Archive

 
Categories / C / Beginners
 

Program to perform functions of a calculator

#include<stdio.h> #include<conio.h> void main() { int i,d,k,a,r,h; char m='y'; long s,n1,n2; clrscr(); printf(" calculation ?"); scanf("%c",&m); while(m=='Y'||m=='y') { clrscr(); printf(" 1.factorial 2.addition 3.subtraction 4.multiplication 5.division 6.squares 7.exit enter your choice ......."); scanf(" %d",&d); while(d>7||d<1) scanf(" %d",&d); switch (d) { case 1: clrscr(); printf("enter any positive manageable number"); scanf("%d",&a); while (a<0) scanf("%d",&a); k=1; for(i=1;i<=a;i++) k=k*i; printf("factorial is %d",k);getch(); break; case 2: clrscr(); printf("enter any two numbers"); scanf(" %ld %ld",&n1,&n2); s=n1+n2; printf("sum of the input numbers is %ld",s); getch(); break; case 3: clrscr(); printf("enter any number"); scanf("%ld,%ld",&n1,&n2); s=n1-n2; printf("the difference between the two numbers is %ld",s); getch(); break; case 4: clrscr(); printf("enter any numbers which are to be multilied"); scanf(" %ld,%ld",&n1,&n2); s=n1*n2; printf("the product is %ld",s); getch(); break; case 5: clrscr(); printf("enter dividend"); scanf("%d",&n1); printf("enter divisor "); scanf("%d",&n2); while (n2==0) { scanf("%d",&n1); scanf("%d",&n2); } s=n1/n2; printf("the quotient is %d",s); getch(); break; case 6: clrscr(); printf("enter the number whose square is to be found out"); scanf(" %d",&n1); s=n1*n1; printf("the square of the number is %d",s); getch(); break; case 7: exit(); default: printf(" Error"); } printf("another calculation"); scanf(" %c",&m); } }