Greater than, less than

This is a discussion on Greater than, less than within the C Programming forums, part of the General Programming Boards category; Hi everyone: I have to list three inout numbers in ascending order and here's my code so far: Code: */ ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    68

    Greater than, less than

    Hi everyone:

    I have to list three inout numbers in ascending order and here's my code so far:
    Code:
    */
    Variables 
    
    	num1	The first number input by the user 
    	num2	The second number input by the user
    	num3	The third number input by the user
    
     */
    
    
    #include <stdio.h>
    
    	int main()
    
    {
    	
    	float	num1;
    
    	float	num2;
    
    	float	num3;
    	
    	float	largest;
    
    	float	middle;
    
    	float	smallest;
    
    	printf("Enter first number\n" );	 
    	scanf( "%f" ,  &num1 );			
    
    	printf("Enter second number\n" );
    
    	scanf("%f" , &num2);		
    
    	printf("Enter the third number\n" ); 
    
    	scanf("%f", &num3 );	
    	
    	if (num1 > num2 )
    
    	largest = num1;
    	
    	printf(The largest number is: %.2f\n, num1);
    	
    return 0;
    }
    Not perfect yet but my question is this:

    Instead of writing a HUGE if statements for all three numbers how can I write something like this:

    If num1 > num2 and >num3
    then the largest number is num1

    Any idea how this is done?
    Thank you very much.
    -Extro

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    How about putting all your numbers in array and using a loop to find the largest number in the array? Or you could sort the array, from largest to smallest, with the help of a function such as qsort.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    Quote Originally Posted by Extropian
    Instead of writing a HUGE if statements for all three numbers how can I write something like this:

    If num1 > num2 and >num3
    then the largest number is num1

    Any idea how this is done?
    Thank you very much.
    -Extro
    Code:
    if( num1 > num2 && num1 > num3 )
        largest = num1;
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    if (num1 > num2 && num1 > num3) largest_number = num1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help: How to write a prime number greater than one billion
    By kenryuakuma in forum C++ Programming
    Replies: 6
    Last Post: 12-19-2008, 07:55 AM
  2. factorial for values greater than 16
    By jas_atwal in forum C Programming
    Replies: 8
    Last Post: 01-21-2008, 05:46 PM
  3. Replies: 2
    Last Post: 01-27-2002, 09:40 AM
  4. Memory allocation greater than 64KB in TC 3.0
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 01-27-2002, 09:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21