Thread: largest number form three number's

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    largest number form three number's

    I want to find largest number form three number

    Code:
    #include <stdio.h>
    int main(void)
    {
        int a, b, c, max;
        max = a;
      
        printf(" enter three numbers \n ");
        scanf(" %d %d %d ",&a,&b,&c);
       
        if(max < b)
     {
            max = b;
     }
        else if(max < c)
     {
            max = c;
     }
     
        printf("\n largest %d ",max);
        
     return 0;
    }
    Result
    enter three numbers
    5
    6
    9
    8
    largest 2965504

    What's the wrong in program ?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You assigned a to max before a was entered in.

    else if is also probably wrong here, if max is actually less than b then max is assigned b and c is never compared. Instead, compare two numbers, and then compare the third to the largest of the first comparison.

    Also the white space you put into a scanf() does matter, try the "%d %d %d" string, so that you don't have to type something random.
    Last edited by whiteflags; 12-04-2017 at 11:25 PM.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    I have tried to print the value of max but there is error

    hello.c: In function 'main':
    hello.c:11:4: error: stray '\302' in program
    A max = a;
    ^
    hello.c:11:5: error: stray '\240' in program
    A max = a;
    ^

    Code:
    #include "stdio.h"
    int main(void)
    {
       int a,b,c,max;
       printf("Please input an integer value: ");
       scanf("%d", &a);
       printf("Please input an integer value: ");
       scanf("%d", &b);
       printf("Please input an integer value: ");
       scanf("%d", &c);
       
       max = a;
       printf("print max %d \n", max); 
    return 0;
    }

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Do you write code in your word processor? These strange numbered characters usually come from copying code from elsewhere and pasting it into your editor. Either way you need to remove those characters before continuing.

    The original program was fine, by the way, I only recommended small changes.

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by whiteflags View Post
    Do you write code in your word processor? These strange numbered characters usually come from copying code from elsewhere and pasting it into your editor. Either way you need to remove those characters before continuing.

    The original program was fine, by the way, I only recommended small changes.

    I write programs in Notepad++. after that I check the program using command promote. I had already made some programs. I copy and paste them to make a new program because it take less time. I do not have to type repeatedly
    Last edited by vead; 12-05-2017 at 01:05 AM.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Ah luckily notepad++ is something I know a thing or two about.

    I copy and paste them to make a new program because it take less time. I do not have to type repeatedly
    Either way, notepad should be able to show you the strange characters so that you can remove them. It was not meant as an affront to you, it is just a thing that can happen from copying and pasting to different programs.

    Try View -> Show Symbol -> Show all characters
    If you see anything that isn't normal line endings or spaces, remove it.

  7. #7
    Banned
    Join Date
    Aug 2017
    Posts
    861
    this does not fully work. It is not suppose to. It is to only help give another point of view on some logic that might help to figure this out. So do not completely rely on this code because I took some out, so just apply the Proper logic instead using process of elimination. Where ? is the var holding the max value if true.
    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        int a = 0, b = 0, c = 0, max = 0;
        
        printf("enter three whole numbers\n");
        scanf(" %d%d%d", &a,&b,&c);
        
        if ( /* some value checks go here */ )
            max = ?;
        else if ( /* some value checks go here */ )
            max = ?;
        else if ( /* some value checks go here */ )
            max = ?;
            
        printf("max number is %d\n"
                "out of these three\n"
                "a %d, b %d c %d\n", max, a,b,c);
        
        
        
    return 0;    
    }
    if one is bigger then the other two then that is the largest number, so how would you check one unknown number against two other unknown numbers to find the largest number?

    This code is in lue of using an array and a loop to do it. As it looks like that is what your are working on. Not using an array and loop to find largest number.

    ( it is not that much to type, so why copy paste? but yeah you got a watch out for that copy paste and getting hidden formatting codes in text when you do that).
    Last edited by userxbw; 12-05-2017 at 10:25 AM.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What he had before was fine, like I keep saying.
    Code:
    max = a;
    if(max < b)
     {
            max = b;
     }
     
    if(max < c)
     {
            max = c;
     }
    max is correct after this. As I said before, I only recommended small changes.

  9. #9
    Banned
    Join Date
    Aug 2017
    Posts
    861
    yeah I can see how that works too,
    slight adjustment
    Code:
    scanf(" %d%d%d", &max,&a,&b);
        
    //max = a;
    if(max < a)
     {
            max = a;
     }
      
    if(max < b)
     {
            max = b;
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-30-2013, 09:30 AM
  2. how to get largest number from the set
    By AlexTank853 in forum C Programming
    Replies: 1
    Last Post: 09-29-2012, 05:42 PM
  3. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 01:44 AM
  4. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM

Tags for this Thread