Switch statement is very useful for creating a calculator which performs following operations: Addition, Substraction, Multiplication, Division and Modulus
#include<stdio.h>
#include<conio.h>
void main()
{
char a,b,c,d;
clrscr();
printf("Enter the values of a and b:");
scanf("%d%d" ,&a,&b);
printf(" Enter your choice between 1 to 5 /n");
{
case 1;
c=a+b;
printf(" Addition=%d", c);
break:
case 2;
c=a-b;
printf(" Substraction=%d" , c);
break:
case 3;
c=a*b;
printf(" Multiplication=%d", c);
break:
case 4;
c=a/b;
printf(" Divide=%d", c);
break:
case 5;
c=a%b;
printf(" Remainder=%d",c );
break:
default:
printf(" Invalid Result");
break:
}
Output1:
Enter the values of a and b:
3
5
Enter your choice between 1 to 5
1
Addition=8
Output2:
Enter the values of a and b:
5
5
Enter your choice between 1 to 5
3
Multiplication=25
Output3:
Enter the values of a and b:
50
10
Enter your choice between 1 to 5
4
Divide=5
Output4:
Enter the values of a and b:
8
2
Enter your choice between 1 to 5
5
Remainder=0
Output5:
Enter the values of a and b:
3
5
Enter your choice between 1 to 5
9
Invalid Result
Variable d is a character not integer. So ...int d ..... not char d? Is this correct?
ReplyDeleteright
DeleteIts not "SUBsTRACTION" its "SUBTRACTION".
ReplyDeleteProgram rightwork nhi ker rha he Program me start se thoda change hoga isme mistak he (#include #includ main() { int a,b,c,d; char ch;) ise chang kerne se program right work kreg
ReplyDeleteWow, this "example" is so terrible I don't even know where to start
ReplyDeleteRight off the bat this program won't compile for a number of reasons:
- You're missing a quotation mark on the last scanf call: scanf("%d , &d);
- Case statements are appended with COLON not semi-colon: 'case 1:' not 'case 1;'
- Break statements are appended with a semi-colon, NOT a colon
- There is no closing bracket for the main statement
Even fixing these problems, the program still doesn't actually work.
- You have '/n' instead of '\n' to denote a newline
- As others have pointed out you're using char variables but using '%d' decimal integer format specifiers. It works, but converts values to ASCII and produces the complete wrong results. Most "calculations" of this poor excuse for a calculator just print out 0.
- Absolutely no use of indentation or code formatting
If the output you posted is indeed output, this was definitely not the source code that was used in compilation. Please at least make sure your source code compiles before posting it to the web for the world to see.
True to the point
Deleteperfect answer
DeleteThis information is very interesting, I really enjoyed, I would like get more information about this, because is very beautiful, thanks for sharing. novated lease calculator
ReplyDeleteThis comment has been removed by the author.
ReplyDelete#include
ReplyDelete#include
void main()
{
int a,b,c;
char op;
clrscr();
printf("Enter 1st Number :");
scanf("%d",&a);
printf("Enter Operator:\n");
scanf("%c",&op);
printf("Enter 2nd Number:");
scanf("%d",&b);
switch (op)
{
case '+':
c=a+b;
printf("Addition=%d",c);
break;
case '-':
c=a-b;
printf("Subtraction=%d",c);
break;
case '*':
c=a*b;
printf("Multiplication=%d",c);
break;
case '/':
c=a/b;
printf("Devision=%d",c);
break;
case '%':
c=a%b;
printf("Reminder=%d",c);
break;
default:
printf("Wrong Input");
}
getch();
}
#include
Delete#include
While executing this code after getting first number and the operator it directly skips to the default statement..
DeletePlease help me out from this..
Thanks in advance..
While executing this code after getting first number and the operator it directly skips to the default statement..
DeletePlease help me out from this..
Thanks in advance..
Thank you for sharing this Useful Information !!
ReplyDeleteLINUX INTERVIEW QUESTIONS
Linux FTP vsftpd Interview Questions
SSH Interview Questions
Apache Interview Questions
Nagios Interview questions
IPTABLES Interview Questions
Ldap Server Interview Questions
LVM Interview questions
Sendmail Server Interview Questions
Read more at Linux Troubleshooting
Keep it simple friend's
ReplyDelete%c is use for switch case
Program work absolutely
Regard:
Ahsan Siddique
#include
#include
void main()
{
int a,b,r;
char op;
printf("simply enter your input and operater);
scanf("%d %c %d", &a, &op, &b);
switch (op)
{
case '+':
r=a+b;
printf("\n Addition=%d\n",r);
break;
case '-':
r=a-b;
printf("\n Subtraction=%d\n",r);
break;
case '*':
r=a*b;
printf("\n Multiplication=%d\n",r);
break;
case '/':
r=a/b;
printf("\n Devision=%d\n",r);
break;
case '%':
r=a%b;
printf("\n Reminder=%d\n",r);
break;
default:
printf("Wrong Input");
}
return (0);
}
excellent piece of information,oneflimpadheye
ReplyDeleteSuperb work thanks for sharing.
ReplyDeleteRegards. http://www.dishtvbazar.com/dish-tv-royle-hd.html
Calculator program in C
ReplyDeleteIn C language we can design a program to add, subtract, multiply, divide any number, these all operation you can perform by using switch case.
Total waste of time your programs have lot of errors
ReplyDeleteTotal waste of time your programs have lot of errors
ReplyDeleteNice C program to make a calculator in C.
ReplyDeleteProgramming is very interesting and creative thing if you do it with love. Your blog code helps a lot to beginners to learn programming from basic to advance level. I really love this blog because I learn a lot from here and this process is still continuing.
ReplyDeleteLove from Pro Programmer
Programming is combination of intelligent and creative work. Programmers can do anything with code. The entire Programming tutorials that you mention here on this blog are awesome. Beginners Heap also provides latest tutorials of Programming from beginning to advance level.
ReplyDeleteBe with us to learn programming in new and creative way.