Largest Among three Numbers

 Largest Among three Numbers


C Program
1.Type 1
#include <stdio.h>
#include <stdlib.h>
  int main()
{
    int a,b,c,max;
    printf("Enter first number:");
    scanf("%d",&a);
    printf("Enter Second number:");
    scanf("%d",&b);
    printf("Enter Third number:");
    scanf("%d",&c);
    if(a>b)
    {
        if(a>c)
        {
            max=a;
        }
        else
        {
            max=c;
        }
    }
    else if(b>a)
    {
        if(b>c)
        {
            max = b;
        }
        else
        {
            max = c;
        }
    }
   printf("max is : %d",max);
}

2.Type 2
#include <stdio.h>
#include <stdlib.h>
int main()
{
    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("max is %d",a);
    }
    else if(b>a && b>c)
    {
        printf("max is %d",b);
    }
       else if(c>a && c>b)
    {
        printf("max is %d",c);
    }
}

3.Ternary Operator
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a,b,c,max;
    printf("Enter first number:");
    scanf("%d",&a);
    printf("Enter Second number:");
    scanf("%d",&b);
    printf("Enter Third number:");
    scanf("%d",&c);
    max=(a>b)?(a>c)?a:c : (b>c)?b:c;
    printf("Max is %d",max);
}


Java Program
1.type 1
import java.util.Scanner;

public class Largest {

	public static void main(String[] args)
	{
		Scanner  s = new Scanner(System.in);
		System.out.print("Enter First Number:");
		int a = s.nextInt();
		System.out.print("Enter First Number:");
		int b = s.nextInt();
		System.out.print("Enter First Number:");
		int c = s.nextInt();
		int max = 0;
		if(a>b)
	    {
	        if(a>c)
	        {
	            max=a;
	        }
	        else
	        {
	            max=c;
	        }
	    }
	    else if(b>a)
	    {
	        if(b>c)
	        {
	            max = b;
	        }
	        else
	        {
	            max = c;
	        }
	    }
		System.out.print("Max is "+ max);
		
	}
		   
}

2.type 2
import java.util.Scanner;

public class Largest {

	public static void main(String[] args)
	{
		Scanner  s = new Scanner(System.in);
		System.out.print("Enter First Number:");
		int a = s.nextInt();
		System.out.print("Enter First Number:");
		int b = s.nextInt();
		System.out.print("Enter First Number:");
		int c = s.nextInt();
		 if(a>b && a>c)
		    {
		        System.out.print("max is " + a);
		    }
		    else if(b>a && b>c)
		    {
		       System.out.print("max is " + b);
		    }
		       else if(c>a && c>b)
		    {
		        System.out.print("max is " + c);
		    }
		
	}
		   
}
3.Ternary
import java.util.Scanner;

public class Largest {

	public static void main(String[] args)
	{
		Scanner  s = new Scanner(System.in);
		System.out.print("Enter First Number:");
		int a = s.nextInt();
		System.out.print("Enter First Number:");
		int b = s.nextInt();
		System.out.print("Enter First Number:");
		int c = s.nextInt();
		 int max=(a>b)?(a>c)?a:c : (b>c)?b:c;
		 System.out.print("Max is : " + max);
	}
		   
}

0 Comments:

Post a Comment