Friday, August 10, 2018

JAVA PROGRAMS PART 3 FOR BEGINNERS

PROGRAM TO PRINT THE MONTH OF THE GIVEN DATA:

STATEMENT:


A date in DD-MM-YYYY format is passed as the input. The program must print the calendar month.
01 - January, 02 - February and so on till 12 - December.

PROGRAM;

import java.util.*;

public class Hello {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        String months[] = {"January", "February", "March", "April", "May", "June", "July", "August",

"September", "October", "November", "December"};

        String dateInput = sc.nextLine();

        String DDMMYYYArray[] = dateInput.split("[-]");

        int month = Integer.parseInt(DDMMYYYArray[1]); //Second string is the month


        /* Subtract 1 as array index starts from zero */

        System.out.println(months[month - 1]);

    }
}

Input:
23-12-2016

Output:
December

 PROGRAM TO PRINT THE ODD INTEGERS IN A GIVEN NUMBER:

STATEMENT:

The program must accept two integers X and Y and print the odd integers between them.


STATEMENT:

import java.util.*;

public class Hello {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int X = sc.nextInt();

        int Y = sc.nextInt();

        int from = X, to = Y;

        while (++from < to) {

            if (from % 2 != 0) {

                System.out.print(from);

                System.out.print(" ");
            }
        }
    }
}

Input:
24
30

Output:
25 27 29

No comments:

Post a Comment