Sunday, July 30, 2023

Complete the code segment to find the largest among three numbers x,y, and z. You should use if-then-else construct in Java.

 




Week 1 : Programming Assignment 2


Complete the code segment to find the largest among three numbers x,y, and z. You should use if-then-else construct in Java.


Program :


import java.util.Scanner;  

public class Exercise1_2 {

       public static void main(String[] args) {

Scanner s = new Scanner(System.in); 

        int x = s.nextInt(); 

        int y = s.nextInt();

int z = s.nextInt();

int result = 0;

//Use if...else ladder to find the largest among 3 numbers and store the largest number in a variable called result.

if(x >= y && x >= z)

        {

            result=x;

        }

        else if(y >= z)

        {

            result=y;

        }

        else

        {

            result=z;

        }

     //Evaluation code 

System.out.print(result);

}

}

No comments:

Post a Comment