Calculator in division operation using C [closed] - c

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I am making a calculator using C language. But the problem is that I am stuck in division.
I want that when I input 0 divided by 0, it will print undefined and if a number divided by zero will print cannot be divide by zero (which seems I got this).
Here is my code:
#include <stdio.h>
#include <math.h>
main(){
float num1, num2, total;
int a;
char choice;
clrscr();
printf("What operation you want to perform?\n 1. Addition\n 2. Subtraction\n 3. Multiplication\n 4. Division\n");
scanf("%d", &a);
switch(a) {
case 1: //Addition
printf("Addition\n Enter two numbers to be added\n");
scanf("%f%f",&num1, &num2);
total=num1 + num2;
printf("\nThe total is: %f + %f = %f\n", num1, num2, total);
printf("\nDo want to continue? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
case 2: //Subtraction
printf("Subtraction\n Enter two numbers to be subtract\n");
scanf("%f%f", &num1, &num2);
total=num1 - num2;
printf("\nThe Difference is: %f + %f = %f\n", num1, num2, total);
printf("\nDo you want to continue? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
case 3: //Multiplication
printf("Multiplication\n Enter two numbers to be multiplied\n");
scanf("%f%f",num1, num2);
total=num1*num2;
printf("\nThe product is: %f * %f = %f\n", num1, num2, total);
printf("\nDo you wish to continue? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
case 4: //Division
abcd:
printf("Division\n Enter two numbers to be divide\n");
scanf("%f%f",&num1, &num2);
if(num2==0.0)
{ printf("\n %f cannot be divided by zero\n\n", num1); // ;(
goto abcd;}
else if(num1==0.0 && num2==0.0) //This is my problem here.
{printf("\nIts Undefined. 0 / 0\n\n ");
}
total=num1/num2;
printf("\nThe qoutient is %f / %f = %f\n", num1, num2, total);
printf("\nDo you want more? Y or N: ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
default:
printf("Can you add letters? LOL only numbers");
printf("\nDo you want to continue? Y/N ");
scanf("%s", &choice);
if(choice=='y'||choice=='Y')
main();
else
exit(1);
break;
}
getch();
}

You have:
if(num2==0.0)
// division by zero
else if(num1==0.0 && num2==0.0)
// zero/zero is undefined
The problem is that if both numbers are 0.0, it's caught by the first test, and the second is never executed.
You need to test for both numbers being zero first, and then check whether the second number is zero.
Or you can combine the two tests:
if (num2 == 0.0) {
if (num1 == 0.0) {
// zero / zero
}
else {
// non-zero / zero
}
}
else {
// ok
}

Your if else statements are not ordered correctly.
Always try to put the case that is most specific before the more general cases.
So in your case, the specific case is when num1 == 0.0 && num2 == 0.0
Whereas the general case is num2 == 0.0
Also, your program would crash when both num1 and num2 are zero, seeing as it divides them anyway in that case.

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;
}

y/n loop at the end of a function

good evening,
Here is my code. I am making a little calculator but I'm battling at the end to make the function repeat with a y/n loop. I have looked at others but can't seem to get the right answer.
Thanks.
#include <stdio.h>
int main()
{
int n, num1, num2, result;
char answer;
{
printf("\nWhat operation do you want to perform?\n");
printf("Press 1 for addition.\n");
printf("Press 2 for subtraction.\n");
printf("Press 3 for multiplication.\n");
printf("Press 4 for division.\n");
scanf("%d", &n);
printf("Please enter a number.\n");
scanf("%d", &num1);
printf("Please enter the second number.\n");
scanf("%d", &num2);
switch(n)
{
case 1: result = num1 + num2;
printf("The addition of the two numbers is %d\n", result );
break;
case 2: result = num1 - num2;
printf("The subtraction of the two numbers is %d\n", result );
break;
case 3: result = num1 * num2;
printf("The multiplication of the two numbers is %d\n", result );
break;
case 4: result = num1 / num2;
printf("The division of the two numbers is %d\n", result );
break;
default: printf("Wrong input!!!");
}
printf("\nDo you want to continue, y/n?\n");
scanf("%c", &answer);
while(answer == 'y');
}
return 0;
}
You have this code
char answer;
{
printf("\nWhat operation do you want to perform?\n");
//...
//... more code
//...
printf("\nDo you want to continue, y/n?\n");
scanf("%c", &answer);
while(answer == 'y');
}
Try to change it to:
char answer;
do {
printf("\nWhat operation do you want to perform?\n");
//...
//... more code
//...
printf("\nDo you want to continue, y/n?\n");
scanf("%c", &answer);
} while(answer == 'y');
So the basic form is:
do {
// code to repeat
} while (Boolean-expression);
BTW - You should always check the value returned by scanf
Example:
if (scanf("%c", &answer) != 1)
{
// Add error handling
}
Also notice that you often want a space before %c to remove any white space (including newlines) in the input stream. Like
if (scanf(" %c", &answer) != 1)
{
// Add error handling
}

Simple calculator using switch with if else in C

This is my code so far for my simple calculator. Im working on sine right now(case 6) with a degree range of 0-360.Here is the output.
$ ./a.exe ProblemSolving.c
Arithmetic : Add(0) Sub(1) Mult(2) Div(4) Mod(5)
Trigonometry : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)
Exponent : x^y(11) 2^x(12) 10^x(13)
Enter the choice of operation:6
The choice of operation is:6
Enter degree range from 0 to 360
Enter degrees:400
After I enter the desired degrees nothing else happens and the program ends. I believe there is something wrong with my if statement or the sine function.
#include <stdio.h>
#include <math.h>
int main() {
double Add1, Add2, Sub1, Sub2, Mult1, Mult2;
int Choice, Div1, Div2, Mod1, Mod2, Base1, Power1, Deg1;
printf("Arithmetic : Add(0) Sub(1) Mult(2) Div(4) Mod(5)\n");
printf("Trigonometry : sine(6) cosine(7) tan(8) arc_sin(9) arc_cos(10)\n");
printf("Exponent : x^y(11) 2^x(12) 10^x(13)\n");
printf("Enter the choice of operation:");
scanf("%d", &Choice);
printf("The choice of operation is:%d\n", Choice);
switch(Choice) {
case 0:
printf("Enter number one:");
scanf("%lf", &Add1);
printf("Enter number two:");
scanf("%lf", &Add2);
printf("%2.2lf + %2.2lf = %2.2lf", Add1, Add2, Add1+Add2);
break;
case 1:
printf("Enter number one:");
scanf("%lf", &Sub1);
printf("Enter number two:");
scanf("%lf", &Sub2);
printf("%2.2lf - %2.2lf = %2.2lf", Sub1, Sub2, Sub1-Sub2);
break;
case 2:
printf("Enter number one:");
scanf("%lf", &Mult1);
printf("Enter number two:");
scanf("%lf", &Mult2);
printf("%2.2lf * %2.2lf = %2.2lf", Mult1, Mult2, Mult1*Mult2);
break;
case 4:
printf("Enter number one:");
scanf("%d", &Div1);
printf("Enter number two:");
scanf("%d", &Div2);
if (Div2 == 0)
printf("Error! Denominator cannot equal 0");
else
printf("%d / %d = %d", Div1, Div2, Div1/Div2);
break;
case 5:
printf("Enter number one:");
scanf("%d", Mod1);
printf("Enter number two:");
scanf("%d", Mod2);
if (Mod2 == 0)
printf("Error! Denominator cannot equal 0");
else
printf("%d % %d = %d", Mod1, Mod2, Mod1%Mod2);
break;
case 6:
printf("Enter degree range from 0 to 360\n");
printf("Enter degrees:");
scanf("%d", Deg1);
if (0 > Deg1 > 360)
printf("Error! Value Entered is not within valid range");
else
printf("sin(%d) = %d", Deg1, sin(Deg1));
break;
default:
printf("Error! operator is not correct");
break;
}
return 0;
}
There are several problems in this code:
Change scanf("%d", Deg1); to scanf("%d", &Deg1);, because scanf() call for addresses. Also, I think it might be better to declare Deg1 as a double.
0 > Deg1 > 360 is wrong in C. You have to write Deg1 < 0 || Deg1 > 360. Operator || stands for "logical Or".
In math.h, sin() works in radians. So use sin(Deg1 * 3.14159265 / 180). Or, to improve readability and maintenance, #define PI 3.14159265 and sin(Deg1 * PI / 180). Note that you cannot write Deg1 / 180 * 3.14159265, because integer literals in C is ints, and int/int = int. For example, 3 / 2 == 1, rather than 1.5. To get the exact value, write 3.0 / 2.0.
In math.h, sin() returns double, so write printf("sin(%d) = %g", Deg1, sin(...));.
Fixed code here:
#include <stdio.h>
#include <math.h>
#define PI 3.14159265
// ...many lines of code...
case 6:
printf("Enter degree range from 0 to 360\n");
printf("Enter degrees:");
scanf("%d", &Deg1);
if (0 > Deg1 || Deg1 > 360)
printf("Error! Value Entered is not within valid range");
else
printf("sin(%d) = %g", Deg1, sin(Deg1 * PI / 180));
break;
The sine function (and the rest of the trig functions) in C works in radians, not degrees. You'll have to convert from degrees from radians before passing the value to sine.
Right now you also have a problem with the format in your printf, since you're passing a double, but telling printf to expect an int. You need to use %f instead of %d.
In addition, your if statement doesn't currently make much sense, and almost certainly doesn't mean what you think. What you apparently want is if (Deg1 < 0.0 || Deg1 > 360.0)

Cannot complete dividing calculation in case. Quotient is 0.0. is there a way I can change int to float?

// 2.cpp : Sample Program with a menu
//Write a program that will use a menu
#include "stdafx.h"
#include<stdio.h>
#define Pi 3.14159
int main(void)
{
int digit1;
int choice;
int a, b, c; //user input for choice 3 & 4
int a1, a2;// User input for Choice 1
int divi;
int divisor;
/*Menu*/
printf("***** MENU *****\n");
printf(" 1 - Greater Than, Less Than, Or Equal?\n");
printf("\t In this selection, you will enter two integers. \n");
printf("\t The program will return'Greater Than' if the first\n");
printf("\t integer entered is less than the second,'Less than'\n");
printf("\t if the first integer is greater than the second, or\n");
printf("\t'Equal' if the two integers entered are equal\n");
printf(" 2 - Evenly Divisible\n");
printf("\t In this slection, you will enter two integers. \n");
printf("\t The program will test if the the first integer\n");
printf("\t is evenly divisible by the second. The program\n");
printf("\t will then return its result and display\n");
printf("\t the quotient rounded to the nearest thousandth\n");
printf(" 3 - Calculations with 2 integers\n");
printf(" 4 - Calculations with 3 integers\n");
printf(" 5 - Calculations with circles\n");
printf(" 6 - Quit\n");
printf("Please enter your choice: ");
scanf("%i",&choice);
printf("\n\n");
switch(choice)
{
case 1: //Greater than, less than, or equal
printf("Please Enter two integers: \n");
scanf("%i %i", &a1, &a2);
if (a1<a2)
printf("Greater Than\n");
else if (a1>a2)
printf("Less Than\n");
else if (a1=a2)
printf("Equal\n");
break;
case 2: //Equally Divisible
I need help with this part of the code. Getting 0.000 for the quotient.Why? What about these cases is making it not receive the integers? I tried to localize the integers with the curly brackets. What am I doing wrong?
printf("Please Enter two integers: \n");
{
int divi;
int divisor;
scanf("%i %i", &divi, &divisor);
float modQuotient = divi%divisor;
if (modQuotient!=0)
{
printf("%i is not evenly divisible by %i\n", divi, divisor);
printf("The quotient is %.3f\n", divi/divisor);
}
else
{
printf("%i is evenly divisible by %i\n", divi, divisor);
printf("The quotient is %.3f\n", divi/divisor);
}
}
break;
/*case 3: /*Calculations with 2 integers*/
case 4: /*Calculations with 3 integers*/
printf(" You have selected to do some calculations with 2
integers\n");
printf("Please enter your 3 integers: ");
scanf("%i%i%i",&a, &b, &c);
printf("The average of %i, %i and %i is %g\n\n",a, b, c, (a+b+c)/3.0);
break;
case 5: /*Calculations with circles*/
float radius;
printf("Please enter the radius of a circle: ");
scanf ("%g", &radius);
printf("\nThe diameter of a circle with a radius of %g units is %.2f units\n\n",
radius, radius*2);
printf("The circumference is of the circle with a radius of %g
units is %.2f units\n\n", radius, Pi*radius*2);
printf("The area of a circle with a radius of %g units is %.2f
square units\n\n", radius, Pi*radius*radius);
break;
case 6: /*Quit*/
printf("Thank you. Good bye\n\n");
break;
default: /*Invalid Entry*/
printf("Invalid Entry...\n");
break;
}
printf("The program will now end. Have a great day!\n");
return 0;
}
The modulus of two ints is another int. Also, dividing by zero causes undefined behaviour. So you should write:
if ( divisor == 0 )
exit(EXIT_FAILURE); // or some other error handling
int modQuotient = divi % divisor;
and remember to use %i or %d if you are printf'ing modQuotient.
In the line:
printf("The quotient is %.3f\n", divi/divisor);
there is a problem. divi and divisor are both int, therefore the result of binary operation on them is also int. With printf it does not do any conversion of arguments to match the format specifier - you have to manually ensure the match yourself. So this code causes undefined behaviour by using %f to try and print an int.
If you want to do a floating point division then you must make at least one of the operands be a float, e.g. (and separating into two lines for clarity):
float quot = (float)divi / divisor;
printf("The quotient is %.3f\n", quot);
NB. You have a logic error in this code:
else if (a1=a2)
printf("Equal\n");
The = operator means assignment. This line changes a2 to be equal to a1. The operator to compare for equality is ==.
printf("The quotient is %.3f\n", divi/divisor);
In the above code you are telling the program the format the output as a float with precision out to three decimal places. So when the division occurs, if the answer were to be 0, it will instead output as 0.000.

if statement not working properly? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
When i run the program, if i enter 0 for the "d" option, it doesn't print what i set the if statement to print when a 0 is entered as u can see almost at the end of the program. The comments in this program are part of the program i just made it like this so you can see the code that is not in comment form is my problem.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
// David Brand
int result, num, num1, calc, calc1, mark;
char A1, A2, ch;
float num2, num3, result1;
printf("\n\n\tMenu");
printf("\n\n\ta. Addition");
printf("\n\tb. Subtraction");
printf("\n\tc. Multiplication");
printf("\n\td. Division");
printf("\n\n\tSelect a, b, c or d: ");
ch = getch();
if (ch == 'a')
{
printf("\n\n\tEnter a number ");
scanf("%d", &num);
printf("\n\n\tEnter another number ");
scanf("%d", &num1);
printf("\n\n\tPlease add the two numbers ");
scanf("%d", &result);
if (result == num + num1)
printf("\n\n\tCorrect");
if (result != num + num1)
printf("\n\n\tWrong");
}
if (ch == 'b')
{
printf("\n\n\tEnter a number ");
scanf("%d", &num);
printf("\n\n\tEnter another number ");
scanf("%d", &num1);
printf("\n\n\tPlease subtract from the first number ");
scanf("%d", &result);
if (result == num - num1)
printf("\n\n\tCorrect");
if (result != num - num1)
printf("\n\n\tWrong");
}
if (ch == 'c')
{
printf("\n\n\tEnter a number ");
scanf("%d", &num);
printf("\n\n\tEnter another number ");
scanf("%d", &num1);
printf("\n\n\tPlease multiply the two ");
scanf("%d", &result);
if (result == num * num1)
printf("\n\n\tCorrect");
if (result != num * num1)
printf("\n\n\tWrong");
}
if (ch == 'd')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if (num2 != 0)
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
if (num2 == 0)
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
if (result1 == num2 / num3)
printf("\n\n\tCorrect");
if (result1 != num2 / num3)
printf("\n\n\tWrong");
getch();
system("cls");
}
Change your code to
if(ch=='d')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if(num2==0)
{
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
else if(num2!=0)
{
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
}
}
Here is that section of code after running through auto-layout:
if(ch=='d')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if(num2!=0)
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
if(num2==0)
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
It doesn't print anything after you enter 0 because it falls straight through to another scanf. If you want more than one statement to be conditionally executed with if, you need to enclose those statements in curly brackets.
I expect you wanted something like this:
if(ch=='d')
{
printf("\n\n\tEnter a number ");
scanf("%f", &num2);
if(num2!=0) {
printf("\n\n\tEnter another number ");
scanf("%f", &num3);
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
} else {
printf("\n\n\tZero divisor");
printf("\n\tHit a key to end the program");
getch();
system("cls");
exit(0);
}
}
I don't have a c compiler at work but I think this should fix most of the issues your having
Also don't you need to check for num3 to be 0 not num2? 0 / 5 is fine 5 / 0 is not
You don't need to check for == and then immediately check for != you know from the == that the opposite of that using else is !=
You should be able to input the two numbers or even the answer as well into a single line to make it easier on the user using a single scanf line also you could make part of this into functions as there is alot of repeated code
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
main()
{
//David Brand
int result,num,num1,calc,calc1,mark;
char A1,A2,ch;
float num2,num3,result1;
printf("\n\n\tMenu");
printf("\n\n\ta. Addition");
printf("\n\tb. Subtraction");
printf("\n\tc. Multiplication");
printf("\n\td. Division");
printf("\n\n\tSelect a, b, c or d: ");
ch=getch();
if(ch=='a')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
scanf("%d %d", &num, &num1);
printf("\n\n\tPlease add the two numbers ");
scanf("%d", &result);
if (result == num + num1)
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
else if(ch=='b')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
scanf("%d %d", &num, &num1);
printf("\n\n\tPlease subtract from the first number ");
scanf("%d", &result);
if(result == num-num1)
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
else if(ch=='c')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5 7");
scanf("%d %d", &num, &num1);
printf("\n\n\tPlease multiply the two ");
scanf("%d", &result);
if(result == num * num1)
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
else if(ch=='d')
{
printf("\n\n\tEnter two numbers separated by a space i.e. 5.7 7.5");
scanf("%f %f", &num2, &num3);
if(num3 == 0)
{
printf("\n\n\tZero divisor");
}
else
{
printf("\n\n\tPlease divide the two numbers ");
scanf("%f", &result1);
if(result1 == (num2 / num3))
printf("\n\n\tCorrect");
else
printf("\n\n\tWrong");
}
}
printf("\n\tHit a key to end the program");
getch();
exit(0);
}

Resources