1 / 12

Computer Programming

Computer Programming. Lab(7). Exercise 2.

moswen
Download Presentation

Computer Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computer Programming Lab(7)

  2. 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 2and year 2012, the program should display that February 2012 had 29 days. If the user entered month 3and year 2015, the program should display that March 2015 had 31 days.

  3. Source code: importjava.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): "); intmonth = input.nextInt(); System.out.print("Enter a year: "); intyear = input.nextInt(); intnumberOfDaysInMonth = 0;

  4. Source code: 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; } 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; } }

  5. Source code: 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"); } } 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; }

  6. Output:

  7. Exercise 2 (Decimal to hex) Write a program that prompts the user to enter an integer between 0and 15and displays its corresponding hex number. Here are some sample runs:

  8. Source code: importjava.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): "); intdecimal = 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));   } }

  9. Output:

  10. Exercise 3 (Find the Largest Number) The process of finding the largest value is used frequently in computer applications. Write a program that reads3numbers from the console and print the largest number between them. Here is a sample run:

  11. Source code: 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+"."); } } } 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); }

  12. Output:

More Related