Monday, January 23, 2017

// // Leave a Comment

Exercise 3.11 — Find the Number of Days in a Month

Java program that prompts the user to enter a 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.

/**
 *
 * @Author: Aghatise Osazuwa
 * Website: www.cscprogrammingtutorials.com
 */

import java.util.Scanner;

public class Ex03_11 {

    public static void main(String[] args) {
        int month, year, days;
        Scanner input = new Scanner(System.in);
        System.out.print("Enter month eg February = 2: ");
        month = input.nextInt();
        System.out.print("Enter year: ");
        year = input.nextInt();
        
        if (month == 1)
            System.out.println("January " + year + " had 31 days");
        if (month == 2 && (year % 4 != 0))
            System.out.println("February " + year + " had 28 days");
        if (month == 2 && (year % 4 == 0))
            System.out.println("February " + year + " had 29 days");
        if (month == 3)
            System.out.println("March " + year + " had 31 days");
        if (month == 4)
            System.out.println("April " + year + " had 30 days");
        if (month == 5)
            System.out.println("May " + year + " had 31 days");
        if (month == 6)
            System.out.println("June " + year + " had 30 days");
        if (month == 7)
            System.out.println("July " + year + " had 31 days");
        if (month == 8)
            System.out.println("August " + year + " had 31 days");
        if (month == 9)
            System.out.println("September " + year + " had 30 days");
        if (month == 10)
            System.out.println("October " + year + " had 31 days");
        if (month == 11)
            System.out.println("November " + year + " had 30 days");
        if (month == 12)
            System.out.println("December " + year + " had 31 days");
    }
}
Liang Introduction to Java Programming exercise 3.11 solution
Sample run
Click here to see other solutions to Introduction to Java Programming.

0 comments:

Post a Comment


DISCLAIMER: Opinions expressed in comments are those of the comment writers alone and does not reflect or represent the views of the post author. We reserve the right to delete any post deemed inappropriate or offensive and/or spammy. Please do not use abusive words/hate speech.

I Appreciate your valuable Feedback. So, Please DO NOT SPAM - Spam comments will be deleted immediately.

Don't use brand name in name field and you're not allowed to use links in comments unless it's necessary. Such comments will be removed immediately. To get notified of replies or follow-up comments, click the box next to notify me.

Thanks.