Contact Learn C
Copy

Program 26:Calculator

Program 26:
 
#include<stdio.h>
main()
{
char choice;
float num1,num2,result;
int flag=1;
printf("Enter +,-,/,* for knowing the result\n");
scanf("%c",&choice);
printf("Enter number 1\n");
scanf("%f",&num1);
printf("Enter number 2\n");
scanf("%f",&num2);

switch(choice)
{
case '+':
result=num1+num2;
break;
case '-':
result=num1-num2;
break;
case '/':
{

if(num2==0)
{
flag=0;
}
else
{
result=num1/num2;
}
break;
}
case '*': result=num1*num2;
break;
default:printf("Error");
break;
}
if(flag==1)
{
printf("%f %c %f = %f",num1,choice,num2,result);
}
else
{
printf("%f %c %f = undefined\n",num1,choice,num2);
}

}
Explanation:
  1. The program starts with initializing
    • choice → For storing the operators like +,-,/,*
    • num1,num2 → For storing two integers
    • result → For storing the resulting value
    • flag → To store true or false.
  2. First we take choice of user (+,-,/,*) then the two numbers.
  3. Now we use switch case
    • If the user enters '+' as his choice then the result will be result=num1+num2
    • If the user enters '-' as his choice then the result will be result=num1-num2
    • If the user enters '/' as his choice then the result will be result=num1/num2 if num2 is not zero else flag will be zero and the output would be undefined
    • If the user enters '*' as his choice then the result will be result=num1*num2
Output:

Calculator

Calculator

3 comments:

  1. factorial-of-hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

    factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.

    ReplyDelete
  2. college-party-guide One of the most important rituals for students in college is to throw the most memorable student dorm celebration.
    gigolo The term”gigolo” usually refers to the person who lives an arranged lifestyle that includes many of these relationships in a series, rather than having any other support system.
    sniffies Sniffies isn’t bookmarked on my phone, however I’ve been logged in enough that the web target will auto-complete on my personal browser.
    erome However, there’s a challenge with streaming online. There are some limitations when streaming online videos. We are limited by the speed of internet, access to internet and other internet-related issues.

    ReplyDelete
  3. college-dorm-party One of the most important rituals for students in college is to throw the most memorable student dorm celebration.
    erome Finding the perfect woman or keeping track of the man who posts footage of his gorgeous wife having a sexy time are the main challenges for amateur porn.
    sniffies Sniffies is a location-based networking application specifically designed for gay, bi trans, queer and curious males who want to meet other like-minded individuals to discover their sexuality.

    ReplyDelete

Donate

Download App and Learn when ever you want

Get it on PlayStore
Get it on Amazon App Store
Get it on Aptoide