-1

how do I find highest number among three inputs? Can anyone help me to find out the solution? This is simple mistake but it is hard for me to find out the mistake.

  #include<stdio.h> 
    
    int main(){
        int a,b,c,d;
        printf("enter first number");
        scanf("%d",&a);
        printf("enter second number");
        scanf("%d",&b);
        printf("enter third number");
        scanf("%d",&c);
      
        if(a<=b>=c){
            printf("higest number is b %d",b);
        }
        else if(b<=c>=a){
             printf("higest number is c %d",c);
        }
        else if(c<=a>=b){
             printf("higest number isd  %d",a);
        }
       
        return 0;
    }
R_Dax
  • 706
  • 3
  • 10
  • 25
  • 3
    @4386427 Of course it's legal C syntax. It's equivalent to `if ((a <= b) >= c)`. It just doesn't do what OP expects it to do, and normally isn't what you'd want. – Tom Karzes Jun 29 '21 at 15:25
  • @Prasant Mhrzn: do you just need find the highest number or also which input is the highest number? – Kraego Jun 29 '21 at 15:50

4 Answers4

3

You need to separate your comparisons using the && operator:

    if(a<=b && b>=c){
        printf("highest number is b %d\n", b);
    }
    else if(b<=c && c>=a){
        printf("highest number is c %d\n", c);
    }
    else if(c<=a && a>=b){
        printf("highest number is a %d\n", a);
    }
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
R_Dax
  • 706
  • 3
  • 10
  • 25
1

Try this short way

#include<stdio.h> 
        
        int main(){
            int higest,i;
            int n[3];
             
            printf("enter first number");
            scanf("%d",&n[0]);
            printf("enter second number");
            scanf("%d",&n[1]);
            printf("enter third number");
            scanf("%d",&n[2]); 
            
            higest = n[0];
            for (i= 0; i < 3; i++ ) {
                if(higest<n[i]){
                     higest = n[i];
                }
            }
           printf("higest number is b %d",higest);
            return 0;
        }
Rachid Loukili
  • 623
  • 6
  • 15
0

if(a<=b>=c){ is not doing what you seem to expect. To get what you want, you need to add a logical-AND between the comparison like if(a<=b && b>=c){

Two other minor things are: 1) You never use the variable d so delete it and 2) in your last printf correct isd to is a.

After correcting that, your code should be fine.

However, your logic can be simplified like:

    if(b>=a && b>=c){
        printf("higest number is b %d",b);
    }
    else if(c>=a){
         printf("higest number is c %d",c);
    }
    else{
         printf("higest number is a %d",a);
    }
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
-1

For starters it is unclear what the variable d is doing in the program.

int a,b,c,d;

Remove it.

Conditions in your if statements are incorrect. For example this if statement

if(a<=b>=c){

is equivalent to

if ( ( a <= b ) >=c ){

The result of the sub-expression a <= b is either 1 if a is less than or equal to b or 0 otherwise. So in fact you are trying to compare either 1 or 0 with c.

Also it is unclear why when b is equal to a and c you are trying to output b as the highest number instead of the number a.

The program can look the following way

#include <stdio.h>

int main(void) 
{
    int a, b, c;
    
    printf( "enter first number " );
    scanf( "%d", &a );
    
    printf( "enter second number " );
    scanf( "%d", &b );
    
    printf( "enter third number " );
    scanf( "%d", &c );
      
    if ( !( a < b ) && !( a < c ) )
    {
        printf( "highest number is a %d\n", a );
    }
    else if ( !( b < c ) )
    {
        printf( "highest number is b %d\n", b );
    }
    else
    {
        printf( "highest number is c %d\n", c );
    }
       
    return 0;
}

Its output might look like

enter first number 2
enter second number 3
enter third number 1
highest number is b 3
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335