0% found this document useful (0 votes)
9 views4 pages

Exp (1) - 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Exp (1) - 1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

EXP NO : 1

QUESTION :

A: Determine the Number of Days in a Month


a. Write a Java program that determines the number of days in a month.

B: Arrange Strings in Alphabetical Order


b. Write a java program that arranges the given set of strings in alphabetical order.
Supply the strings through command line arguments.

A: Determine the Number of Days in a Month


CODE:

import java.util.Scanner;

public class DaysInMonth {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter month (1-12): ");

int month = scanner.nextInt();

System.out.print("Enter year: ");

int year = scanner.nextInt();

int days = 0;

if (month < 1 || month > 12) {

System.out.println("Invalid month!");

} else {

switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case 12:

days = 31;

break;

case 4: case 6: case 9: case 11:

days = 30;

break;

case 2:

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {

days = 29;

} else {

days = 28;

break;

System.out.println("Number of days: " + days);

scanner.close();

INPUT :

Enter month (1-12): 2

Enter year: 2024

OUTPUT:

Number of days: 29
B: Arrange Strings in Alphabetical Order

CODE:

import java.util.Arrays;

public class SortStrings {

public static void main(String[] args) {

if (args.length == 0) {

System.out.println("No strings provided!");

return;

Arrays.sort(args);

System.out.println("Sorted strings:");

for (String str : args) {

System.out.println(str);

INPUT :

Command line input: java SortStrings banana apple cherry

OUTPUT:

Sorted strings:

apple

banana
cherry

You might also like