Assignment 1
Write a program in Java that returns the maximum for three numbers
Assignment 2
Based on Assignment 1, write an algorithm for your implementation
Assignment1
import java.util.Scanner;
public class Assignment1 {
public static void main(String[] args) {
// 'Keyboard reader'
Scanner sc = new Scanner(System.in);
// Variables for three numbers and max value
int a, b, c, max;
// Read numbers
System.out.print("Please enter the first number: ");
a = sc.nextInt();
System.out.print("Please enter the second number: ");
b = sc.nextInt();
System.out.print("Please enter the third number: ");
c = sc.nextInt();
// Determine maximum of three numbers
// If 'a' greater than 'b' and 'c', then 'a' is max
if(a > b && a > c) max = a;
// Otherwise if 'b' greater than 'a' and 'c', then 'b' is max
else if(b > a && b > c) max = b;
// Otherwise 'c' is the 'max' value
else max = c;
// Print result
System.out.println("Max value=" + max);
sc.close();
}
}
Assignment2
Comments
Leave a comment