0% found this document useful (0 votes)
15 views

JAVA CMD

Uploaded by

Deepankar Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

JAVA CMD

Uploaded by

Deepankar Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

This program counts the number of words in a given input string passed as a
command-line argument.
public class CountWords {
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Please provide a string within quotes.");
return;
}

String input = args[0];


String[] words = input.split("\\s+");
System.out.println("Number of words: " + words.length);
}
}

2. This program checks if a number is even or odd based on command-line input.


public class EvenOdd {
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Please provide a number.");
return;
}
int num = Integer.parseInt(args[0]);
if (num % 2 == 0) {
System.out.println(num + " is even.");
} else {
System.out.println(num + " is odd.");
}
}
}
3. Java command-line program that adds two numbers provided as command-line
arguments.
public class AddNum {
public static void main(String[] args) {
// Check if exactly two arguments are provided
if (args.length != 2) {
System.out.println("Please provide exactly two numbers.");
return;
}

// Parse the command-line arguments as integers


int num1 = Integer.parseInt(args[0]);
int num2 = Integer.parseInt(args[1]);

// Calculate the sum


int sum = num1 + num2;

// Display the result


System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
}
}

You might also like