Thread: Need help with basic calculation program.

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    24

    Post 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;
    	/*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.
    Last edited by StateofMind; 03-04-2009 at 11:55 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by StateofMind View Post
    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;
    	/*Display the answer.*/
    	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;
    	/*Display the answer.*/
    	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;
       answer++;
    }

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by Adak View Post
    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;
       answer++;
    }
    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--;
     answer++;
    }
    ?

    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?
    Last edited by StateofMind; 03-05-2009 at 12:21 AM.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by StateofMind View Post
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Quote Originally Posted by vart View Post
    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. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by StateofMind View Post
    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
    Last edited by QuantumPete; 03-05-2009 at 03:01 AM.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    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;
    /*Display the answer.*/
    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;
    /*Display the answer.*/
    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);
    }
    Last edited by StateofMind; 03-05-2009 at 07:15 PM.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    24
    Weird, I thought I had a couple of more posts here but I don't see them.

    Anyway, many thanks to everyone for the help. I really appreciate it, probably couldn't have gotten it done without some of your suggestions/fixes.

    Here's the code that I ended up with:

    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);
    
    /*Terminate the program if the user assigns [3] to 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;
    /*Display the answer.*/
    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;
    /*Display the answer.*/
    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);
    }

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Here's the code that I ended up with:
    It still needs proper indentation
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Date calculation program
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 04-17-2009, 07:24 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  4. IDEA: A basic drawing program
    By ygfperson in forum Contests Board
    Replies: 0
    Last Post: 08-12-2002, 11:15 PM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM