Saturday, August 18, 2018

INTERMEDIATE JAVA PROGRAMS FOR LEARNERS


PROGRAM TO PRINT THE DAY OF A GIVEN DATE:

STATEMENT:

The day corresponding to the first date of a given month is provided as input to the program. Then a specific date D of the month is provided. The program must  print the day (one among MON,TUE, WED, THU, FRI, SAT, SUN) of the date D.

PROGRAM;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class Hello {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String DAYS[] = new String[]{"SUN","MON","TUE","WED","THU","FRI","SAT"};
        List<String> DAYSLIST = Arrays.asList(DAYS);
        String dayone = sc.nextLine().trim();
        int date = sc.nextInt();
        int dayIndex = DAYSLIST.indexOf(dayone);
        for(int dateCounter=2; dateCounter <= date; dateCounter++){
            dayIndex++;
            if(dayIndex == 7){
                dayIndex=0;
            }
        }
     
        System.out.println(DAYS[dayIndex]);

    }

}

Input:
FRI
24

Output:
SUN

Explanation:
If it is Friday on 1st of the month, then 22nd will also be a Friday. Hence 24th of the month will be a Sunday. Hence SUN is printed.

REVERSE PATTERN PRINTING IN JAVA:

STATEMENT:

Based on the input value of N, the program must print the pattern described below.

Input Format:
First line will contain the value of N.

Output Format:
N lines will contain the number pattern as described below with each value separated by a single space.

PROGRAM::

import java.util.*;

public class Hello {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        for (int row = 1; row <= N; row++) {
            int printValue = N * (N + 1) / 2 - (row - 1);
            for (int col = 0; col < (N - row + 1); col++) {
                System.out.print(printValue+" ");
                printValue -= (N-col);
            }
            System.out.println("");
        }
    }

}

Input:
3

Output:
6 3 1
5 2
4

No comments:

Post a Comment