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;
/*Display the answer.*/
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;
/*Display the answer.*/
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.