Thread: Help with if statement

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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