# lab7

```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 Lab7_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.nextInt();
System.out.print("Enter a year: ");
int year = input.nextInt();
int numberOfDaysInMonth = 0;
Source code:
if(month ==1){
System.out.print("January " + year);
numberOfDaysInMonth = 31;
}
else if (month ==2){
System.out.print("February " + year);
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)) {
numberOfDaysInMonth = 29;
}
else {
numberOfDaysInMonth = 28;
}
}
else if(month==3){
System.out.print("March " + year);
numberOfDaysInMonth = 31;
}
else if(month ==4){
System.out.print("April " + year);
numberOfDaysInMonth = 30;
}
else if(month ==5){
System.out.print("May " + year);
numberOfDaysInMonth = 31;
}
else if(month ==6){
System.out.print("June " + year);
numberOfDaysInMonth = 30;
}
Source code:
else if(month ==7){
System.out.print("July " + year);
numberOfDaysInMonth = 31;
}
else if(month ==8){
System.out.print("August " + year);
numberOfDaysInMonth = 31;
}
else if(month ==9){
System.out.print("September " + year);
numberOfDaysInMonth = 30;
}
else if(month ==10){
System.out.print("October " + year);
numberOfDaysInMonth = 31;
}
else if(month ==11){
System.out.print("November " + year);
numberOfDaysInMonth = 30;
}
else if(month ==12){
System.out.print("December " + year);
numberOfDaysInMonth = 31;
}
System.out.println(" has " + numberOfDaysInMonth + "
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 lab7_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.nextInt();
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 Lab7_3 {
public static void main(String[] args) {
Scanner input=new Scanner (System.in);
System.out.print("Enter the first number: ");
double x = input.nextDouble();
System.out.print("Enter the second number: ");
double y = input.nextDouble();
System.out.print("Enter the third number: ");
double z = input.nextDouble();
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:
```