COMPUTER PROGRAMMING Lab7 Exercise 2 Find the number
COMPUTER PROGRAMMING Lab(7)
Exercise 2 (Find the number of days in a month) Write a program that prompts the user to enter the month and year and displays the number of days in the month. For example, if the user entered month 2 and year 2012, the program should display that February 2012 had 29 days. If the user entered month 3 and year 2015, the program should display that March 2015 had 31 days.
Source code: import java. util. Scanner; public class Lab 7_1 { public static void main(String[] args) { Scanner input = new Scanner(System. in); // Prompt the user to enter input System. out. print("Enter a month in the year (e. g. , 1 for Jan): "); int month = input. next. Int(); System. out. print("Enter a year: "); int year = input. next. Int(); int number. Of. Days. In. Month = 0;
Source code: if(month ==1){ System. out. print("January " + year); number. Of. Days. In. Month = 31; } else if (month ==2){ System. out. print("February " + year); if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) { number. Of. Days. In. Month = 29; } else { number. Of. Days. In. Month = 28; } } else if(month==3){ System. out. print("March " + year); number. Of. Days. In. Month = 31; } else if(month ==4){ System. out. print("April " + year); number. Of. Days. In. Month = 30; } else if(month ==5){ System. out. print("May " + year); number. Of. Days. In. Month = 31; } else if(month ==6){ System. out. print("June " + year); number. Of. Days. In. Month = 30; }
Source code: else if(month ==7){ System. out. print("July " + year); number. Of. Days. In. Month = 31; } else if(month ==8){ System. out. print("August " + year); number. Of. Days. In. Month = 31; } else if(month ==9){ System. out. print("September " + year); number. Of. Days. In. Month = 30; } else if(month ==10){ System. out. print("October " + year); number. Of. Days. In. Month = 31; } else if(month ==11){ System. out. print("November " + year); number. Of. Days. In. Month = 30; } else if(month ==12){ System. out. print("December " + year); number. Of. Days. In. Month = 31; } System. out. println(" has " + number. Of. Days. In. Month + " days"); } }
Output:
Exercise 2 (Decimal to hex) Write a program that prompts the user to enter an integer between 0 and 15 and displays its corresponding hex number. Here are some sample runs:
Source code: import java. util. Scanner; public class lab 7_1 { public static void main(String args[]) { Scanner input = new Scanner(System. in); System. out. print("Enter a decimal value (0 to 15): "); int decimal = input. next. Int(); if (decimal > 15 || decimal < 0) System. out. println("Invalid input"); else if (decimal < 10) System. out. println("The hex value is " + decimal); else System. out. println("The hex value is " + (char)('A' + decimal - 10)); } }
Output:
Exercise 3 (Find the Largest Number) The process of finding the largest value is used frequently in computer applications. Write a program that reads 3 numbers from the console and print the largest number between them. Here is a sample run:
Source code: import java. util. Scanner; public class Lab 7_3 { public static void main(String[] args) { Scanner input=new Scanner (System. in); System. out. print("Enter the first number: "); double x = input. next. Double(); System. out. print("Enter the second number: "); double y = input. next. Double(); System. out. print("Enter the third number: "); double z = input. next. Double(); if ( x > y && x > z ){ System. out. println("The first number is the largest"); System. out. println("Which is: "+x); } else if ( y > x && y > z ){ System. out. println("The second number is the largest"); System. out. println("Which is: "+y); } else if ( z > x && z > y ){ System. out. println("The third number is the largest"); System. out. println("Which is: "+z); } else{ System. out. println("Entered numbers are not distinct. "); System. out. println("The numbers are "+x+", "+y+", "+z+". "); } } }
Output:
- Slides: 12