# Thread: Need help with basic calculation program.

1. ## Need help with basic calculation program.

I'm trying to write a program for my C class and I'm having some trouble with it. We were told to create a calculation program that performs addition, subtraction, multiplication, and division...using only addition and subtraction.

I understand the concept of multiplication and division as repeated addition and subtraction (respectively), but I don't know how to translate that into C. We've just started talking about if statements and while loops, and the instructor told us that our program should end up with the two inside each other.

Here's all I've been able to come up with so far:

Code:
```#include<stdio.h>

int main(void)
{
/*Create variables.*/
int number1;
int number2;
int operation;
int total;

/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

/*Perform addition sequence if the user inputs [1].*/
if(operation = 1)
{
/*Set total to zero.*/
total = 0;
/*Assign value to the first number.*/
printf("Enter the first interger that you'd like to add.\n");
scanf("%i", &number1);
/*Assign value to the second number.*/
printf("Now input the interger that you'd like to add to %d.\n", number1);
scanf("%i", &number2);
/*Add the numbers to find their sum.*/
total = number1 + number2;
printf("The sum of %d and %d is %d.\n", number1,number2,total);

printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

}

/*Perform subtraction sequence if the user inputs [2].*/
if(operation = 2)
{
/*Set total to zero.*/
total = 0;
/*Assign value to the first number.*/
printf("Enter the interger you'd like to subtract from.\n");
scanf("%i", &number1);
/*Assign value to the second number.*/
printf("Now input the interger that you'd like to subtract from %d.\n", number1);
scanf("%i", &number2);
/*Subtract the second number from the first to find the difference.*/
total = number1 - number2;
printf("The difference of %d and %d is %d.\n", number1,number2,total);

printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

}

/*Perform the multiplication sequence if the user inputs [3].*/
if(operation = 3)
{
/*Set total to zero.*/
total = 0;
printf("Enter the interger that you'd like to multiply.\n");
scanf("%i", &number1);
printf("Now input the interger that you'd like to multiply %d by.\n", number1);
scanf("%i", &number2);

printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

}

/*Perform the division sequence if the user inputs [4].*/
if(operation = 4)
{
/*Set total to zero.*/
total = 0;
printf("Enter the interger that you'd like to divide.\n");
scanf("%i", &number1);
printf("Now input the interger that you'd like to divide %d by.\n", number1);
scanf("%i", &number2);

printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

}

return(0);
}```
Thanks in advance for any help. I really just want to understand how to do this. Been reading over the textbook for hours but it's just not clicking.

2. Originally Posted by StateofMind
I'm trying to write a program for my C class and I'm having some trouble with it. We were told to create a calculation program that performs addition, subtraction, multiplication, and division...using only addition and subtraction.

I understand the concept of multiplication and division as repeated addition and subtraction (respectively), but I don't know how to translate that into C. We've just started talking about if statements and while loops, and the instructor told us that our program should end up with the two inside each other.

Here's all I've been able to come up with so far:

Code:
```#include<stdio.h>

int main(void)
{
/*Create variables.*/
int number1;
int number2;
int operation;
int total;

/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division]\n");
scanf("%i", &operation);

/*Perform addition sequence if the user inputs [1].*/
if(operation = 1)   //should have two equal signs. One = is for assignment.
{
/*Set total to zero.*/
total = 0;
/*Assign value to the first number.*/
printf("Enter the first interger that you'd like to add.\n");   //interger??
scanf("%i", &number1);
/*Assign value to the second number.*/
printf("Now input the interger that you'd like to add to %d.\n", number1); //interger??
scanf("%i", &number2);
/*Add the numbers to find their sum.*/
total = number1 + number2;
printf("The sum of %d and %d is %d.\n", number1,number2,total);
}

/*Perform subtraction sequence if the user inputs [2].*/
if(operation = 2)
{
/*Set total to zero.*/
total = 0;
/*Assign value to the first number.*/
printf("Enter the interger you'd like to subtract from.\n");
scanf("%i", &number1);
/*Assign value to the second number.*/
printf("Now input the interger that you'd like to subtract from %d.\n", number1);
scanf("%i", &number2);
/*Subtract the second number from the first to find the difference.*/
total = number1 - number2;
printf("The difference of %d and %d is %d.\n", number1,number2,total);
}

/*Perform the multiplication sequence if the user inputs [3].*/
if(operation = 3)
{
/*Set total to zero.*/
total = 0;
printf("Enter the interger that you'd like to multiply.\n");
scanf("%i", &number1);
printf("Now input the interger that you'd like to multiply %d by.\n", number1);
scanf("%i", &number2);

}

/*Perform the division sequence if the user inputs [4].*/
if(operation = 4)
{
/*Set total to zero.*/
total = 0;
printf("Enter the interger that you'd like to divide.\n");
scanf("%i", &number1);
printf("Now input the interger that you'd like to divide %d by.\n", number1);
scanf("%i", &number2);

}

return(0);
}```
Thanks in advance for any help. I really just want to understand how to do this. Been reading over the textbook for hours but it's just not clicking.
If you know you need to subtract in a loop to handle division, then where's the subtraction code at?

Code:
```answer= 0;
while (numerator > 0)  {
numerator -= denominator;
}```

3. Originally Posted by Adak
If you know you need to subtract in a loop to handle division, then where's the subtraction code at?

Code:
```answer= 0;
while (numerator > 0)  {
numerator -= denominator;
}```
I didn't even know where to start, I'm very new to this. :/

Thank you very much, I see what you've done and I'm kind of irritated that I didn't think of it myself.

Would multiplication work like this:

Code:
```answer = 0;
while (number2 > 0) {
number1 += number1;
number2--;
}```
?

edit: er, nvm. That wouldn't work at all. How can I add the first number to itself (number2 amount of times) and get an answer while retaining the value (number1 entered by the user) to find the answer in the first place?

4. Originally Posted by StateofMind
How can I add the first number to itself (number2 amount of times) and get an answer while retaining the value (number1 entered by the user) to find the answer in the first place?
Simple - do not add it to itself - add it to answer

5. Originally Posted by vart
Simple - do not add it to itself - add it to answer
Ah,

Code:
```	total = 0;
while(number2 > 0)
{
total += number1;
number2--;
}```
Thanks so much!

6. Originally Posted by StateofMind
Ah,

Code:
```	total = 0;
while(number2 > 0)
{
total += number1;
number2--;
}```
Thanks so much!
(Your indentation sucks!) But yeah, that'll work. Though you'll have to take care if number2 is negative, as that loop will basically run forever in that case (There are various ways of fixing that).
Here is a small alteration to make the loop neater:
Code:
```int total = 0;
while (number2--)
{
total += number1;
}```
QuantumPete

7. Originally Posted by QuantumPete
Code:
```int total = 0;
while (number2--)
{
total += number1;
}```
QuantumPete
I don't really understand this. The while(number2--) part is what I don't get...

I see what you're saying about negative numbers though. I do need to fix that for both multiplication and division...

8. Originally Posted by QuantumPete
Though you'll have to take care if number2 is negative, as that loop will basically run forever in that case (There are various ways of fixing that).
a negative number is not > 0 and the loop won't run.

Originally Posted by StateofMind
I don't really understand this. The while(number2--) part is what I don't get...

I see what you're saying about negative numbers though. I do need to fix that.
It checks the value of number2 for the while condition (It will run for any non zero value) and then subtracts 1 from number2

actually, It looks like Pete's suggestion is the one that will always run if number2 is negative

I may be wrong though

9. Originally Posted by StateofMind
I don't really understand this. The while(number2--) part is what I don't get...
When you do number2-- it will use the current value of number2, run the loop and then decrement it.
As you probably know, any number, except 0 is considered "true" in C.
The last thing is that a number by itself is still considered a statement and the result of this statement is basically number2 (because it's post-decremented). Since while() takes a boolean arguments (i.e. either true or false), number2 is considered as a boolean statement. When number2 is not 0, it returns true. So when number2 == 2, it is true, the loop runs, number2 == 1, still true, the loop runs, number2 == 0, this is considered false, so the loop is no longer run. This is a short-hand way of not having to write (while x != 0).
Though, as it has been pointed out, this will not work for negative numbers, since -1 is also considered true.

Originally Posted by ಠ_ಠ
actually, It looks like Pete's suggestion is the one that will always run if number2 is negative
Yep, sorry, I was already thinking ahead to my alteration when I wrote that. Only my suggestion will run forever if number2 is -ve. For StateOfMind's code, it won't execute the loop at all as you've correctly pointed out.

QuantumPete

10. Can anyone help me get this to loop correctly?

Code:
```#include<stdio.h>

int main(void)
{
/*Create variables.*/
int number1;
int operation;
int total;
int cont;
int result;

/*Ask the user if they'd like to start a new calculation.*/
printf("Would you like to perform a new calculation? (Enter 1 for [Yes] or 2 for [No].\n");
scanf("%i", &cont);

/*If yes, set the total to zero.*/
if(cont == 1)
{
total = 0;

while(cont == 1)
{
/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

/*Perform addition sequence if the user inputs [1].*/
if(operation == 1)
{
/*Set total to zero.*/
total = 0;
/*Assign value to the number.*/
printf("Enter the integer that you'd like to add.\n");
scanf("%i", &number1);
/*Add the numbers to find their sum.*/
result = total + number1;
printf("The sum is %d.\n", result);
total = result;
}

/*Perform subtraction sequence if the user inputs [2].*/
if(operation == 2)
{
/*Set total to zero.*/
total = 0;
/*Assign value to the number.*/
printf("Enter the integer you'd like to subtract.\n");
scanf("%i", &number1);
/*Perform the operation.*/
result = total - number1;
printf("The difference is %d.\n", result);
total = result;
}

/*Perform the multiplication sequence if the user inputs [3].*/
if(operation == 3)
{
/*Set total to zero.*/
total = 0;
/*Ask for the integer to multiply.*/
printf("Enter the integer that you'd like to multiply.\n");
scanf("%i", &number1);
/*Perform the operation.*/
result = 0;
while(number1 > 0)
{
result = total + total;
number1--;

}
printf("The product is %d.\n", result);
total = result;
}

/*Perform the division sequence if the user inputs [4].*/
if(operation == 4)
{
/*Set total to zero.*/
total = 0;
/*Ask for the value to divide.*/
printf("Enter the integer that you'd like to divide.\n");
scanf("%i", &number1);
/*Perform the operation.*/
result = 0;
while(total > 0)
{	total -= number1;
result++;
}
printf("The quotient is %d.\n", result);
total = result;
}
/*Ask the user whether or not to start a new calculation.*/
printf("Would you like to perform a new calculation? (Enter 1 for [Yes] or 2 for [No].\n");
scanf("%i", &cont);
/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

} /*Close the while loop for cont[1].*/
}

/*If the user does not want to start a new operation, start the following sequence.*/
if(cont == 2)
{
while(cont == 2)
{

/*If the operation is [1], begin the addition sequence.*/
if(operation == 1)
{
/*Assign value to the number.*/
printf("Enter the integer that you'd like to add.\n");
scanf("%i", &number1);
/*Add the numbers to find their sum.*/
result = total + number1;
printf("The sum is %d.\n", result);
total = result;
}

/*Start the subtraction block if [2] is entered for operation.*/
if(operation == 2)
{	/*Assign value to the number.*/
printf("Enter the integer you'd like to subtract.\n");
scanf("%i", &number1);
/*Perform the subtraction operation.*/
result = total - number1;
printf("The difference is %d.\n", result);
total = result;
}

/*If multiplication is chosen, start this block.*/
if(operation == 3)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to multiply.\n");
scanf("%i", &number1);
/*Perform the multiplication sequence.*/
result = 0;
while(number1 > 0)
{
result += total;
number1--;
}

/*Display the result.*/
printf("The product is %d.\n", result);
total = result;

}

/*Division block for operation == 4.*/
if(operation == 4)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to divide by.\n");
scanf("%i", &number1);
/*Perform the division operation.*/
while(total > 0)
{ total -= number1;
result++;
}
/*Display the result.*/
printf("The quotient is %d.\n", result);
total = result;
}
/*Ask the user whether or not to start a new calculation.*/
printf("Would you like to perform a new calculation? (Enter 1 for [Yes] or 2 for [No].\n");
scanf("%i", &cont);
/*Ask user to assign a value to operation.*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);
}
}

return(0);
}```
I'm trying to make it to where after an operation is performed, the sequence starts over from the beginning and the user is asked whether or not to start over and then which operation they'd like to perform. I think I've just created a bunch of redundant code trying to do this...

Also, how on Earth do I fix the negative integer problem with multiplication and division? This stuff is so much harder than I expected, but I love it!

11. Since your while loop starts with the asking for the operation, you need to stop your while loop at that point (don't ask again at the bottom).

12. Originally Posted by tabstop
Since your while loop starts with the asking for the operation, you need to stop your while loop at that point (don't ask again at the bottom).
Changed to this:

Code:
```#include<stdio.h>

int main(void)
{
/*Create variables.*/
int number1;
int operation;
int total;
int cont;
int result;

/*Ask the user if they'd like to start a new calculation.*/
printf("Would you like to perform a new calculation? (Enter 1 for [Yes] or 2 for [No].\n");
scanf("%i", &cont);

/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

/*If yes, set the total to zero.*/
if(cont == 1)
{
total = 0;

while(cont == 1)
{

/*Perform addition sequence if the user inputs [1].*/
if(operation == 1)
{
/*Set total to zero.*/
total = 0;
/*Assign value to the number.*/
printf("Enter the integer that you'd like to add.\n");
scanf("%i", &number1);
/*Add the numbers to find their sum.*/
result = total + number1;
printf("The sum is %d.\n", result);
total = result;
}

/*Perform subtraction sequence if the user inputs [2].*/
if(operation == 2)
{
/*Set total to zero.*/
total = 0;
/*Assign value to the number.*/
printf("Enter the integer you'd like to subtract.\n");
scanf("%i", &number1);
/*Perform the operation.*/
result = total - number1;
printf("The difference is %d.\n", result);
total = result;
}

/*Perform the multiplication sequence if the user inputs [3].*/
if(operation == 3)
{
/*Set total to zero.*/
total = 0;
/*Ask for the integer to multiply.*/
printf("Enter the integer that you'd like to multiply.\n");
scanf("%i", &number1);
/*Perform the operation.*/
result = 0;
while(number1 > 0)
{
result = total + total;
number1--;

}
printf("The product is %d.\n", result);
total = result;
}

/*Perform the division sequence if the user inputs [4].*/
if(operation == 4)
{
/*Set total to zero.*/
total = 0;
/*Ask for the value to divide.*/
printf("Enter the integer that you'd like to divide.\n");
scanf("%i", &number1);
/*Perform the operation.*/
result = 0;
while(total > 0)
{	total -= number1;
result++;
}
printf("The quotient is %d.\n", result);
total = result;
}
}
} /*Close the while loop for cont[1].*/

/*If the user does not want to start a new operation, start the following sequence.*/
if(cont == 2)
{
while(cont == 2)
{

/*If the operation is [1], begin the addition sequence.*/
if(operation == 1)
{
/*Assign value to the number.*/
printf("Enter the integer that you'd like to add.\n");
scanf("%i", &number1);
/*Add the numbers to find their sum.*/
result = total + number1;
printf("The sum is %d.\n", result);
total = result;
}

/*Start the subtraction block if [2] is entered for operation.*/
if(operation == 2)
{	/*Assign value to the number.*/
printf("Enter the integer you'd like to subtract.\n");
scanf("%i", &number1);
/*Perform the subtraction operation.*/
result = total - number1;
printf("The difference is %d.\n", result);
total = result;
}

/*If multiplication is chosen, start this block.*/
if(operation == 3)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to multiply.\n");
scanf("%i", &number1);
/*Perform the multiplication sequence.*/
result = 0;
while(number1 > 0)
{
result += total;
number1--;
}

/*Display the result.*/
printf("The product is %d.\n", result);
total = result;

}

/*Division block for operation == 4.*/
if(operation == 4)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to divide by.\n");
scanf("%i", &number1);
/*Perform the division operation.*/
while(total > 0)
{ total -= number1;
result++;
}
/*Display the result.*/
printf("The quotient is %d.\n", result);
total = result;
}
}
}

return(0);
}```
Now it gets stuck on the first operation that I choose. For instance, choosing addition keeps it asking for another number to add each time it finds a sum.

I want to just use goto but apparently that's a forbidden practice...

13. Originally Posted by QuantumPete
Yep, sorry, I was already thinking ahead to my alteration when I wrote that. Only my suggestion will run forever if number2 is -ve. For StateOfMind's code, it won't execute the loop at all as you've correctly pointed out.

QuantumPete
lol, your solution to a loop that might not execute was a loop that might execute forever?

I guess throwing your code into an If statement with it decrementing if its >0 and incrementing if it's < 0 would work

Code:
```  int total = 0;

if(number2 > 0) {
while (number2--)
{
total += number1;
}
}else {
while (number2++)
{
total += number1;
}
}```

14. How do I get my code to go back to the top and evaluate cont after it runs through either set of if statements? It just keeps terminating...

Code:
```#include<stdio.h>

int main(void)
{
/*Create variables.*/
int number1;
int operation;
int total;
int cont;
int result;
int loop = -1;

while(loop == -1)
{
/*Ask the user if they'd like to start a new calculation.*/
printf("Would you like to perform a new calculation? (Enter 1 for [Yes] or 2 for [No].)\nEnter 3 to [Terminate].\n");
scanf("%i", &cont);

if(cont == 3)
{
return(0);
}
/*If yes, set the total to zero.*/
if(cont == 1)
{
total = 0;
}

/*Ask user to assign a value to "operation"*/
printf("Please choose an operation. (Enter 1 for [Addition], 2 for [Subtraction], 3 for [Multiplication], or 4 for [Division])\n");
scanf("%i", &operation);

/*If the operation is [1], begin the addition sequence.*/
if(operation == 1)
{
/*Assign value to the number.*/
printf("Enter the integer that you'd like to add.\n");
scanf("%i", &number1);
/*Add the numbers to find their sum.*/
result = total + number1;
printf("The sum is %d.\n", result);
total = result;
}

/*Start the subtraction block if [2] is entered for operation.*/
else if(operation == 2)
{
/*Assign value to the number.*/
printf("Enter the integer you'd like to subtract.\n");
scanf("%i", &number1);
/*Perform the subtraction operation.*/
result = total - number1;
printf("The difference is %d.\n", result);
total = result;
}

/*If multiplication is chosen, start this block.*/
else if(operation == 3)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to multiply.\n");
scanf("%i", &number1);
/*Perform the multiplication sequence.*/
result = 0;
if(number1 > 0)
{
while(number1 > 0)
{
result += total;
number1--;
}
}
if(number1 < 0)
{
while(number1 < 0)
result -= total;
number1++;
}

/*Display the result.*/
printf("The product is %d.\n", result);
total = result;
}

/*Division block for operation == 4.*/
else if(operation == 4)
{
/*Ask for a value to be assigned.*/
printf("Enter the integer that you'd like to divide by.\n");
scanf("%i", &number1);
/*Perform the division operation.*/
result = 0;
if(total > 0)
{

if(number1 > 0)
{
while(total > 0)
{
total -= number1;
result++;
}
}
else if(number1 < 0)
{
while(total > 0)
{
total += number1;
result--;
}
}

}
else if(total < 0)
{

if(number1 > 0)
{
while(total < 0)
{
total += number1;
result++;
}
}
else if(number1 < 0)
{
while(total < 0)
{
total -= number1;
result++;
}
}

}
/*Display the result.*/
printf("The quotient is %d.\n", result);
total = result;
}

else
{
printf("That command is not recognized.\n");
}
}

return(0);
}```

15. You're right, goto's are strong medicine, and have to be used, very sparingly.

Here's an example of when to use a goto:

Code:
```while (variable > 0)
{
if(variable mod 10 == 0)
{
for(i = 0; i < j; i++)
{
for(j = 10000; j > 0; j--)
{
if(j % 288 == 0 && j < 599)
{
goal = 1;
goto Done;
}
}
}
}
}
Done:
;```
Because a simple "break" will not cleanly exit from all the levels of the loops, etc.

Other than something like this, goto's lead to ........ty code, which quickly degenerates into
an unmanageable "can of worms".

I'll take a look at your program, in just a bit.

Popular pages Recent additions