Thread: new to C need a little help with a simple program

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    7

    new to C need a little help with a simple program

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    main() {
        int first, second, add, multiply, square;
    
    
        printf("Enter two numbers: \n");
        scanf("%d%d", &first, &second);
    
    
        add = first + second;
        multiply = first * second;
        square = first * first;
        square = second * second; 
    
    
        printf("Sum = %d\n", add);
        printf("Multiplication = %d\n", multiply);
        printf("Square first = %d\n", square);
        printf("Square second = %d\n", square);
    
    
        system("pause");
    
    
        return 0;
    
    
    }
    so the goal of this program is to input 2 numbers and output the sum the product, and then the square of the individual numbers. i'm having a problem with the square portion when i run the program it gives me the square of the second number as the answer for both, i have looked and cannot seem to find the answer i'm looking for on how i would get the square to function correctly for both inputs. any help with this would be greatly appreciated as i know it's something obvious and simple and yet i can't seem to get it and it's driving me insane lol.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Remember that statements are executed in order. First you assign the value of first * first to square. Then you assign the value of second * second to square, overwriting the previous value. Then you eventually print out the value of square twice.

    To fix it you could use two different variables for the answers, perhaps square1 and square2. Or if you wanted to use just one variable you would need to print out the first answer before reusing the variable to store and print the second answer.

    Alternatively, you could do it all without the answer variables by putting the calculations in the printf statements:
    Code:
        printf("Sum = %d\n", first + second);
        printf("Multiplication = %d\n", first * second);
        printf("Square first = %d\n", first * first);
        printf("Square second = %d\n", second * second);

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    7
    ooooooohhhhhhh thank you sooo much. i did try to differentiate my variables but i wrote them as "square 1" and "square 2" and this i guess the space between the word and number prompted an error telling me i needed to put a ";" after which i knew would close out the line so i just got confused after that. but the calculations in the printf is awesome thank you for that i will definitely remember that in the future.

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Main returns an integer.
    It's usually best to explicitly assign variables a value before you use them to avoid any garbage values being calculated.
    Try not to use system call functions to pause the console, use getchar() instead to catch the next key press.
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM

Tags for this Thread