Thread: Help with if statement

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    24

    Help with if statement

    This is an assignment i'm working on:
    Write a C program that indicates which of the two given numbers is greater. The program
    should receive two float numbers as input from the user. If the first number is greater, the output
    should be “The greater number is 1” otherwise the output should be “The greater
    number is 2”. Save your program in a file called p3.c. If they are equal, the output should be "The greater number is 1".
    Code:
     #include <stdio.h>
     int main(void)
    {
      float a, b;
      printf("Enter Number 1: ");
      scanf("&#37;f", &a);
      printf("Enter Number 2: ");
      scanf("%f", &b);
    
      if (a > b)
      printf("The Greater Number is 1");
      else if (a == b)
      printf("The Greater Number is 1");
      else
      printf("The Greater Number is 2");
    
     return 0;
    }
    Can someone help me out with this? When I run the program I choose two numbers and regardless of the numbers it prints The Greater Number is 1.
    Last edited by nafix; 09-14-2007 at 05:27 PM.

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    it seems to work for me. what 2 numbers are you trying that doesnt work?

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    i tried a= 2 and b= 3. i've tried multiple conditions where b is bigger or c is equal, but i always get "The Greater Number is 1"

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    It works properly for me also. There is probably a difference between what you are actually using and what you posted. BTW, you can combine "a > b" and "a == b" into a single condition "a >= b".

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    2 and 3 work.. it seems to work completely for me. if your using an IDE, make sure to save the project, restart the program, recompile it and run it. if your using command line make sure your entering the proper commands and executing the most recently compiled program.

  6. #6
    Registered User
    Join Date
    Sep 2007
    Posts
    24
    Oh, I had put in &a again for the second scanf instead of &b. Now it works. Thanks for the help guys.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. If Else statement problem
    By doofusboy in forum C Programming
    Replies: 2
    Last Post: 11-09-2005, 07:18 AM
  4. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM