this is a real time simple calculator in c programming language.this program will calculate values.in this program I have used loop to continue calculating number till user exits program.Here character variable "o" is used to get operator from user.Now let use see program.
Program:
/*PROGRAMMER: SOJITRA AADARSH
DATE:22/8/2013
AGE: 16 YEARS
E-MAIL: adarshsojitra7@gmail.com*/
#include<stdio.h> /*include header files*/
#include<conio.h> /*include header files*/
main()
{
/*Defining variables*/
char ab[50];
float a,ans;
char o;
clrscr();
ans=0;
printf("\n Press A for help");
while(1)
{
printf("\n\n Answer: %.3f",ans);
printf("\n\n Enter opertor and number: ");
fgets(ab,sizeof(ab),stdin);
sscanf(ab,"%c %f",&o,&a);
/*-----------------------------------------------------*/
if((o=='a')||(o=='A')) /*For help*/
{
clrscr();
printf("\n Enter operator and number to calculate.");
printf("\n Do not put space between operator and number.");
printf("\n Enter N or n to quit.");
printf("\n Press c or C to clear screen.");
printf("\n To reset multiply with 0.");
printf("\n You can calculate form -3.4e-38 to +3.4e-38.");
printf("\n The answer will be 0 first time when you are calcutating from this window.");
printf("\n\n Answer: %.3f",ans);
printf("\n\n Enter opertor and number: ");
fgets(ab,sizeof(ab),stdin);
sscanf(ab,"%c %.3f",&o,&a);
}
/*----------------------------------------------------*/
/*To clear screen*/
if((o=='c')||(o=='C'))
{
clrscr();
printf("\n\n Answer: %.3f",ans);
printf("\n\n Enter opertor and number: ");
fgets(ab,sizeof(ab),stdin);
sscanf(ab,"%c %.3f",&o,&a);
}
/*------------------------------------------------*/
/*To quit*/
if((o=='N')||(o=='n'))
{
printf("\n\t\t\tPress any key to quit............");
break;
}
switch(o)
{
case'+':ans+=a;
break;
case'-':ans-=a;
break;
case'*':ans*=a;
break;
case'/':if(a==0)
{
printf("\n\t\t\t\t !ERROR!");
printf("\n\t\t\t Operation is cancelled");
}
else
{
ans/=a;
}
break;
default:printf("\n\n Error in operator");
break;
}
}
getch();
return 0;
}
output:

Program:
/*PROGRAMMER: SOJITRA AADARSH
DATE:22/8/2013
AGE: 16 YEARS
E-MAIL: adarshsojitra7@gmail.com*/
#include<stdio.h> /*include header files*/
#include<conio.h> /*include header files*/
main()
{
/*Defining variables*/
char ab[50];
float a,ans;
char o;
clrscr();
ans=0;
printf("\n Press A for help");
while(1)
{
printf("\n\n Answer: %.3f",ans);
printf("\n\n Enter opertor and number: ");
fgets(ab,sizeof(ab),stdin);
sscanf(ab,"%c %f",&o,&a);
/*-----------------------------------------------------*/
if((o=='a')||(o=='A')) /*For help*/
{
clrscr();
printf("\n Enter operator and number to calculate.");
printf("\n Do not put space between operator and number.");
printf("\n Enter N or n to quit.");
printf("\n Press c or C to clear screen.");
printf("\n To reset multiply with 0.");
printf("\n You can calculate form -3.4e-38 to +3.4e-38.");
printf("\n The answer will be 0 first time when you are calcutating from this window.");
printf("\n\n Answer: %.3f",ans);
printf("\n\n Enter opertor and number: ");
fgets(ab,sizeof(ab),stdin);
sscanf(ab,"%c %.3f",&o,&a);
}
/*----------------------------------------------------*/
/*To clear screen*/
if((o=='c')||(o=='C'))
{
clrscr();
printf("\n\n Answer: %.3f",ans);
printf("\n\n Enter opertor and number: ");
fgets(ab,sizeof(ab),stdin);
sscanf(ab,"%c %.3f",&o,&a);
}
/*------------------------------------------------*/
/*To quit*/
if((o=='N')||(o=='n'))
{
printf("\n\t\t\tPress any key to quit............");
break;
}
switch(o)
{
case'+':ans+=a;
break;
case'-':ans-=a;
break;
case'*':ans*=a;
break;
case'/':if(a==0)
{
printf("\n\t\t\t\t !ERROR!");
printf("\n\t\t\t Operation is cancelled");
}
else
{
ans/=a;
}
break;
default:printf("\n\n Error in operator");
break;
}
}
getch();
return 0;
}
output:
