Java 1 (A, B)
Java 1 (A, B)
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");
}
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: ______
OUTPUT: