Thread: C Program Output?

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    3

    Question C Program Output?

    Heya,

    I recently tried my hand at coding n have started with C language.

    Was finding out problems online to solve on C.

    Here's the question: Add 2 Numbers Without Using the Addition Operator.


    This is the code I ran using this c compiler , m unable to get the desired solution for the operators used.
    C Program Output?-c-program-compiler-jpg

    I would really appreciate your help.

  2. #2
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    What exactly is not working? It is not exactly clear what your input and what the output from the program is from your screenshot.

    Just quickly wrote a similar code, almost identical to what you have. It seems to work for me, the first operation does give expected output. That even increases my confusion about what exactly your issue is about.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Your scanf format is "%d %d" which tells scanf to read an integer (in decimal), followed by whitespace, followed by another integer. Your input has a comma between the two integers, so scanf stops at the comma and leaves the second int untouched. If it was left uninitialized (e.g., it's an automatic variable defined like int b;), then the results are undefined.

    The easiest solution is to type in the two integers with a space between them--no commas or any other characters (other whitespace like tab, newline, etc are ok).

    By the way, method 3 works only for a positive sum. Try adding 1 and -2, for example.

  4. #4
    Registered User
    Join Date
    Sep 2021
    Posts
    3
    Basically, the result should be 7 using all operators, given that the custom input is 3, 4.
    As per question, we are supposed to add two numbers without using the addition operator.

    I guess I had put the wrong format for the custom input, separating the numbers by space instead of comma, gave me the desired result: 7

  5. #5
    Registered User
    Join Date
    Sep 2021
    Posts
    3
    Thank you so much for responding, ghoul and christop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-14-2016, 08:27 AM
  2. why the output of program is this?
    By san12345 in forum C Programming
    Replies: 2
    Last Post: 02-21-2016, 06:17 AM
  3. Output of program
    By Stuart Dickson in forum C Programming
    Replies: 7
    Last Post: 09-04-2010, 05:27 AM
  4. what is the program output
    By sunil17 in forum C Programming
    Replies: 1
    Last Post: 08-29-2010, 07:08 AM
  5. Output of the program
    By karthik537 in forum C Programming
    Replies: 4
    Last Post: 09-16-2009, 08:59 AM

Tags for this Thread