C calculator (command based) part- Final

2

Yahoo!!!!!!!!!!!!!!!!!!!

We are in the final part of a functional Calculator in C program. If anybody missed the past parts then please the link below.

C calculator (command based) part-1

C calculator (command based) part-2

C calculator (command based) part-3

C calculator (command based) part-4

We know the functions and the switch case, how to print a program, what is pre  processor directives, what is a variable, how to store data in a variable and how to use them and print them. Now finally we will write a program for command based program. We can update this program by our own creativity but this format will be like a kernel of this program. 😛 Now see this code and try it in your C compiler.

#include<stdio.h>
#include<conio.h>

main(){ float num1,num2;
int option;
float result;
char press;
clrscr();
start:
printf(“\n”);
textcolor(GREEN);
cprintf(“Enter first number:  “);
scanf(“%f”,&num1);
textcolor(YELLOW);
cprintf(“Enter second number:  “);
scanf(“%f”,&num2);
textcolor(RED);
cprintf(“Select your operation from the list:”);
printf(“\n\t\t”);
textcolor(BLUE);
cprintf(“1 for add”);
printf(“\n\t\t”);
cprintf(“2 for subtract “);
printf(“\n\t\t”);
cprintf(“3 for multiply “);
printf(“\n\t\t”);
cprintf(“4 for divide “);
printf(“\n\t\t”);
cprintf(“Enter your Choice:  “);
scanf(“%d”,&option);

if(option==1 || option==2 || option==3 || option==4){

switch(option){

case 1:
result=num1+num2;
break;

case 2:
result=num1-num2;
break;

case 3:
result=num1*num2;
break;

case 4:
if(num2==0)
break;
else
result=num1/num2;
break;

default: break;
}
}
else
goto start;
if(option==4 && num2==0)
printf(“You have entered a zero as divider which seems as undefined”);

else if(option==2 && num2>num1)
printf(“your second number is greater than first number”);

else
printf(“Result = %1.2f”,result);

printf(“\n\n Do you want to continue? If yes then press [y]or press [n]”);
press=getch();
if(press==’y’)
goto start;
else
printf(“\n Program terminated normally\n ***Thank you***”);

return 0;
}

 

N.B:

  • Here we have used goto statement to jump into the desired place here. [goto —-]will jump the place which is written after goto—- and is written like statement:
  • for goto example:-

start:

printf(“Start”\n);

stop:

printf(“Stop\n”);

goto start;

// here the program will print the program like this:

Start

Stop

Start

……………..****……………..   Thanks

By the way I have my own blog now. I am going to start it with some tricks and hacks of programming and technology. If you wish to visit there please click here. Or visit blog.techiehunter.org

Share.

About Author

Rajumeazi

I am a free learner like to fly in the whole cyber world to gather knowledge. And I know I have only a little to share but I will try my best to give you what I have. :) Thank you :)

2 Comments

Leave A Reply

CAPTCHA ImageChange Image