Вы находитесь на странице: 1из 8

import java.util.

GregorianCalendar;

import java.util.Scanner;

public class MachineProblem1 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String name;

System.out.print("Enter your name: ");

name = input.nextLine();

System.out.println("Hello " + name + "\n");

System.out.println("PRIMITIVE DATATYPES MANIPULATIONS"+

"\nTell me what you want me to do " + name +". Here are your
options:" +

"\n\n[1]Determine if a year is a LEAP YEAR or NOT(Input: any year in


integer form)"+

"\n[2]HEIGHT COMPARISON of 3 persons(Input: height of 3 person in


centimeter Output: Detemine the tallest)"+

"\n[3]Determine if a number is PRIME or NOT(Input: any integer)"+

"\n[4]Display Fibonacci Series(Input: any integer to determine the


limit)");
int choices = 0;

System.out.print("Enter your choice: ");

choices = input.nextInt();

switch(choices){

case 1: // leap year

GregorianCalendar cal = new GregorianCalendar();

String year;

System.out.println("Enter year");

year=input.next();

try {

int i = Integer.parseInt(year);

if (cal.isLeapYear(i)){

System.out.println("Leap year");

else {

System.out.println("Not Leap Year");

}
catch (NumberFormatException ex){

System.out.println("Error");

break; // end of leap year

case 2: //height comparison

String h1 , h2 , h3;

System.out.println("Enter 1st height");

h1=input.next();

System.out.println("Enter 2nd height");

h2=input.next();

System.out.println("Enter 3rd height");

h3=input.next();

try{

int height1 = Integer.parseInt(h1);

int height2 = Integer.parseInt(h2);

int height3 = Integer.parseInt(h3);


if(height1>=height2 && height1>=height3){

System.out.println(height1 + "cm is the


tallest");

else if(height2>=height1 && height2>=height3){

System.out.println(height2 + "cm is the


tallest");

else if(height3>=height1 && height3>=height2){

System.out.println(height3 + "cm is the


tallest");

catch (NumberFormatException ex){

System.out.println("Error");

break; // end of height comparison

case 3: //prime number

System.out.println("Enter Input");

int i,j=0,checker=0;
String number;

number = input.next();

try{

int number1 = Integer.parseInt(number);

j = number1/2;

if (number1==0 || number1 ==1 ){

System.out.println(number1 + " Is not a prime


number");

break;

else{

for(i = 2 ; i<=j ; i++){

if (number1%i==0)

System.out.println(number1 + "
Is not a prime number");

checker = 1;

break;

}
if (checker == 0){

System.out.println(number1 +" Is a prime


number");

catch(NumberFormatException ex){

System.out.println("Error");

break;//end of prime number

case 4://fibonacci

String fibonacci;

int n1=1,n2=1,sum=1;

System.out.println("Enter your number");

fibonacci=input.next();
try{

int limit = Integer.parseInt(fibonacci);

for ( int counter = 0; counter<limit ; counter++ ){

n1=n2;

n2=sum;

sum=n1+n2;

System.out.print(n1 +" ");

catch(NumberFormatException ex){

System.out.println("Error");

break;//end of fibonacci

}
}

Вам также может понравиться