calculator output in c - c

I made a simple calculator program using switch case, but the output is different than I expected.
int main(){
double a, b;
double sum = 0;
char o; //operator
printf("Enter operator\n");
scanf("%c", &o);
printf("Enter first operand\n");
scanf("%f", &a);
printf("Enter second operand\n");
scanf("%f", &b);
switch (o)
{
case '+':
sum = a + b;
break;
case '-':
sum = a - b;
break;
case '*':
sum = a * b;
break;
case '/':
sum = a / b;
break;
if (b == 0){ printf("Error"); }
break;
}
printf("The result is\n%10.10lf\n", sum);
getchar();
getchar();
}
The result of 'sum' is some huge astronomical numbers. Can someone tell why?

Try %lf instead of %f because that way you will have a and b as the type double rather than float.

a and b are both of type double. The correct format specifier for double in scanf is %lf, not %f (which is for float).
scanf("%lf", &a);
Note that in printf, %f is used or for double (the same for float because it's promoted to double). Since C99, %lf in printf is the same as %f.

you are trying to convert double to float which is not implicitly possible inside scanf or printf function. What scanf was doing, It was purging/reformatting the whole (double)input into an empty float value. Here is working one :
#include<stdio.h>
int main(){
double a, b;
double sum = 0;
char o; //operator
printf("Enter operator\n");
scanf("%c", &o);
printf("Enter first operand\n");
scanf("%lf", &a);
printf("Enter second operand\n");
scanf("%lf", &b);
switch (o)
{
case '+':
sum = a + b;
break;
case '-':
sum = a - b;
break;
case '*':
sum = a * b;
break;
case '/':
sum = a / b;
break;
if (b == 0){ printf("Error"); }
break;
}
printf("The result is\n %f\n", sum);
}

As everyone has pointed out, you need scanf ("%lf", &a); and same for b.
Now in your switch statement you wanted to prevent division by zero :
case '/':
sum = a / b;
break;
if (b == 0){ printf("Error"); }
break;
But what happens there is that you break; before getting to the condition. Suppose we removed that first break; the division by zero is still performed before you print the error message anyway.
One way to prevent division by zero would be :
case '/':
if (b)
sum = a / b;
else
printf("Error");
break;
where we check that b != 0 before we make the division and then we either divide or print the error message.

Related

Repeat the c program according to user

I want this program to repeat every time when users wants to continue.
I want user to choose "yes" to continue the program using other options but it's not executable. I used 'if else' statement for that but I am not getting any applicable or satisfying results. Can someone help me on this matter?
#include <stdio.h>
int main() {
float a, b;
int p;
char q;
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo u want to continue:(y for yes and n for no) :");
scanf("%c", &q);
if (q == 'y')
return main();
else
return 0;
}
You need to use a loop for this. Explanations in the comments:
int main() {
float a, b;
int p;
char q;
do // use a loop here
{
printf("Enter the first number:");
scanf("%f", &a);
printf("Enter the second number:");
scanf("%f", &b);
printf("Choose the option:\n1.Sum\n \n2.Subtraction\n \n3.Multiplication\n \n4.Divison\n \nyour choice=");
scanf("%d", &p);
switch (p)
{
case 1:
printf("The sum of above numbers is: %f", a + b);
break;
case 2:
printf("The subtraction of above numbers is:%f", a - b);
break;
case 3:
printf("The multiplication of above numbers is:%f", a * b);
break;
case 4:
printf("The division of above numbers is:%f", a / b);
break;
}
printf("\n\nDo you want to continue: (y for yes and n for no) :");
scanf(" %c", &q); // use " %c" instead of "%c", the white space
// will absorb any blank space (including newlines)
} while (q == 'y'); // continue loop if user has entered 'y'
return 0;
}

C for loop runs good first iteration only the next iteration it ignores scanf in loop

I'm trying to write calculator code in C for loop. The problem it runs good at first iteration only. the next iterations it ignores scanf command so it gives wrong output.
#include <stdio.h>
#include <stdlib.h>
int main(){
int i ;
char operator_;
double x, y;
for (i = 1; i <= 3; ++i)
{
printf("Enter an operator (+, -, *,/): ");
scanf("%c",&operator_);
printf("\nEnter the first number: ");
scanf("%lf",&x);
printf("Enter the second number: ");
scanf("%lf",&y);
switch (operator_) {
case '+':
printf("The sum equals to %lf + %lf = %lf\n\n\n", x, y, x + y);
break;
case '-':
printf("The difference equals to %lf - %lf = %lf\n\n\n", x, y, x - y);
break;
case '*':
printf("The product equals to %lf * %lf = %lf\n\n\n", x, y, x * y);
break;
case '/':
printf("The quotient equals to %lf / %lf = %lf\n\n\n", x, y, x / y);
break;
default:
printf("Error! operator is not correct\n\n\n");
}
}
printf("\nyour trial period has ended\n\n\n");
return 0;
}

can you modify my program to use "y/n" statement in c

Here, i am writing a c programming code i need help form you guys! I want to modify my program and want to use this in a loop i.e if a user enters "y" & if "n" will be entered it must end the program, I am trying this with switch case, I tried my best to end the program, it is returning to end but i don't know how to use "y" value for running the program again!
#include<conio.h>
#include<stdio.h>
int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
while (1)
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. pound to kgs\n5. inches to meters\n");
scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
goto end;
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}
}
end:
return 0;
}
I want a kindful modification from you developers to help me as I am a beginner and learing from a month, I except that the actual output must come like this ----o/p- do you want to quit press "q" ,do you want to run again press "y"
If you change the while(1) to a variable, then you can test after every loop whether it's time to exit.
The changes I made to your code all have comments below:
#include<conio.h>
#include<stdio.h>
int main()
{
char n, A, B;
float kmTOm = 0.621371;
float inchesTOfoot = 0.0833333;
float cmTOinches = 0.393701;
float poundtokg = 0.453592;
float inchesTOmeter = 0.0254;
float a, b;
n = 'y'; //set n to make sure while repeats
while (n != 'n') //change while to test "n" instead of 1, this way the value of n can be changed and the loop can be exited
{
printf("Enter the input character. q to quit\n1. kms to miles\n2. inches to foot\n3. cms to inches\n4. po
und to kgs\n5. inches to meters\n");
scanf(" %c", &n);
switch (n)
{
case 'q':
printf("Quitting the program...");
return(0); //exit entire program
break;
case '1':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * kmTOm;
printf("%.2f Kms is equal to %.2f Miles\n\n\n", a, b);
break;
case '2':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOfoot;
printf("%f Inches is equal to %f Foot\n", a, b);
break;
case '3':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * cmTOinches;
printf("%f Cms is equal to %f Inches\n", a, b);
break;
case '4':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * poundtokg;
printf("%f Pounds is equal to Kgs %f \n", a, b);
break;
case '5':
printf("Enter quantity in terms of a unit\n");
scanf("%f", &a);
b = a * inchesTOmeter;
printf("%f inches is equal to %f meters \n", a, b);
break;
default:
printf("In default now");
break;
}
//Ask the user if they want to run again. The code will exit if the user types 'n', it will repeat if they type anything else (including 'y').
printf("\nRun again? y/n:");
scanf(" %c",&n);
}
return 0;
}

How to check whether the entered symbol is char or not?

I have a problem with calculator. I want to program a calculator, but it "works" with letters too. If I enter f.e letters "s" and "u", i get answer 0. How to repair that?
# include <stdio.h>
int main() {
char operator;
int a,b;
printf("Enter the operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two number and seperate them with space: ");
scanf("%lf %lf",&a, &b);
switch(operator)
{
case '+':
printf("%.2lf + %.2lf = %.2lf",a, b, a + b);
break;
case '-':
printf("% 2lf - %.2lf = % 2lf",a, b, a - b);
break;
case '*':
printf("%.2lf * %.2lf = %.2lf",a, b, a * b);
break;
case '/':
printf("%.2lf / %.2lf = %.2lf",a, b, a / b);
break;
default:
printf("Error!");
}
return 0;
}
Sorry, if the question has been up here.
The link to answer is also appreciated!
Cheers!
I have made the corrections discussed in comments
#include <stdio.h>
int main(void) { // correct definition
char operator;
double a,b; // correct type
printf("Enter the operator (+, -, *, /): ");
if(scanf("%c", &operator) != 1) { // add error check
puts("Bad operator entered");
return 1;
}
printf("Enter two number and seperate them with space: ");
if(scanf("%lf %lf",&a, &b) != 2) { // add error check
puts("Bad value(s) entered");
return 1;
}
switch(operator)
{
case '+':
printf("%.2lf + %.2lf = %.2lf",a, b, a + b);
break;
case '-':
printf("%.2lf - %.2lf = % 2lf",a, b, a - b); // corrected typo
break;
case '*':
printf("%.2lf * %.2lf = %.2lf",a, b, a * b);
break;
case '/':
printf("%.2lf / %.2lf = %.2lf",a, b, a / b);
break;
default:
printf("Error!");
}
return 0;
}
UPDATED:
i found my Ooldest code in library Folder maybe it helps you in that problem. This will definitely Work.
char operator;
printf("Enter the Operation in ( * , / , - , + )");
scanf("%s",&operator);
if(operator!='+' || operator!='-' || operator!='*' || operator!='/')
{
return 0;
}
I got it working. It was easier to do than I thought.
So the right code, I'll put comment to answer.
#include
int main(void) { // correct definition
char operator;
double a,b; // correct type
printf("Enter the operator (+, -, *, /): ");
if(scanf("%c", &operator) != 1) { // add error check
puts("Bad operator entered");
return 1;
}
printf("Enter two number and seperate them with space: ");
if(scanf("%lf %lf",&a, &b) != 2) { // add error check
puts("Bad value(s) entered");
return 1;
}
switch(operator)
{
case '+':
printf("%.2lf + %.2lf = %.2lf",a, b, a + b);
break;
case '-':
printf("%.2lf - %.2lf = % 2lf",a, b, a - b); // corrected typo
break;
case '*':
printf("%.2lf * %.2lf = %.2lf",a, b, a * b);
break;
case '/':
if(b!=0){ //added an if-sentence
printf("%.2lf / %.2lf = %.2lf\n",a, b,
a / b);
}
else
printf("Can't divide by zero\n");
break;
default:
printf("Error!");
}
return 0;
}
Basically, I just put an if-sentence to case('/').

While loop - changing string variable

As a newbie to c programming (I've only had experience in visual basic), I'm not entirely sure how a while loop with a changing string variable in its condition statement should function.
The following code is a simple calculator that I was making that allows the user to input an operation and two numbers, then output the respective result. I'am trying to code in a while loop that continually repeats the procedure until the user decides to exit it. However it seems that the line scanf("%c", &quit); isn't affecting the while loop condition statement.
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = "n";
while (quit = "n"){
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}
Thanks for all your help in advance.
You could do like this
#include <stdio.h>
int main() {
float num1, num2;
char operation;
char quit = 'n';
//while (quit = "n") //
while (quit!= 'y')
{
printf("Enter an operator (+, -, *, /) \n");
scanf(" %c", &operation);
printf("Enter the numbers you wish to carry out the operation on \n");
scanf("%f %f", &num1, &num2);
switch(operation) {
case '+':
printf("%f\n", num1+num2);
break;
case '-':
printf("%f\n", num1-num2);
break;
case '*':
printf("%f\n", num1*num2);
break;
case '/':
printf("%f\n", num1/num2);
break;
}
printf("Would you like quit the program, is so enter 'y' \n");
scanf("%c", &quit);
}
return 0;
}
replace while (quit = "n") with while (quit! = 'y')
That's because you're assigning the value of the quit variable in your while loop, instead of checking for its value.
Use == for =
while(quit == "n"){...}

Resources