Roll no: ___ Date: __________ Page No: ___
Practical Name: _________________________________________________ Practical No: ______
AIM: Creating Java Program Using Command Line Argument
THEORY: In Java, command-line arguments are the arguments passed to the main() method of a Java program when it
is executed from the command line. These arguments allow you to provide input data to the program at runtime, which
can be used to customize its behavior. Command-line arguments are passed as strings and can be accessed within the
main() method using the args parameter.
1(a)
Write a program that takes input from user through command line argument and then prints whether a number is prime
or not.
ALGORITHM:
1. Start by taking an integer input `n`.
2. Initialize a variable `count` to 0.
3. Iterate from 1 to `n`, inclusive.
4. For each iteration:
a) Check if `n` is divisible by the current iteration number `i`.
b) If `n` is divisible by `i`, increment the `count` variable.
5. After the loop:
a) If `count` is equal to 2, print `"Number n is Prime"`.
b) Otherwise, print `"Number n is Not Prime"`.
CODE:
import java.util.Scanner;
class prime
{
public static void main(String args[])
{
int n= Integer.parseInt(args[0]);
int count=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
{
count++;
}
}
if(count==2)
{
System.out.println("Number"+n+"is Prime");
}
ABES Engineering College Sign of Faculty with Date
Roll no: ___ Date: __________ Page No: ___
Practical Name: _________________________________________________ Practical No: ______
else
{
System.out.println("Number "+n+" is Not Prime");
}
}
}
OUTPUT:
1(b)
Write a Program to enter number through Command Line and Check whether it is Palindrome or not.
ALGORITHM:
1. Start by taking an integer input `num`.
2. Initialize three variables: `rem`, `sum`, and `temp`. Set `sum` to 0 and `temp` to the value of `num`.
3. Enter a loop that continues while `num` is greater than 0:
a) Extract the rightmost digit of `num` by taking its modulus with 10 and store it in `rem`.
b) Multiply the current value of `sum` by 10 and add `rem` to it.
c) Update `num` by removing its rightmost digit (divide by 10).
4. After the loop, check if the original number `temp` is equal to `sum`:
a) If they are equal, print "palindrome number".
b) Otherwise, print "not palindrome".
CODE:
class Palindrome{
public static void main(String args[]){
int rem, sum=0, temp;
ABES Engineering College Sign of Faculty with Date
Roll no: ___ Date: __________ Page No: ___
Practical Name: _________________________________________________ Practical No: ______
int num= Integer.parseInt(args[0]);
temp=num;
while(num>0){
rem=num%10;
sum=(sum*10)+r;
num=num/10; }
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
} }
OUTPUT:
ABES Engineering College Sign of Faculty with Date