Thread: Need help with basic calculation program.

Threaded 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.

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