EXPERIMENT NO.
CODE : THEORY :
import [Link]; : imports the
1. Write a java program to check whether Scanner class to take input from the user.
number is prime or not. Scanner sc = new Scanner([Link]); :
creates an object to read input from the
keyboard.
int num = [Link](); : stores the user-
entered number in the variable num.
boolean isPrime = true; : initializes a flag
assuming the number is prime initially.
if (num <= 1) : checks the base condition
since prime numbers must be greater than 1.
for (int i = 2; i <= num / 2; i++) : iterates
from 2 to half of the number to check
divisibility.
if (num % i == 0) : checks whether the
number is divisible by any value other than 1
and itself.
isPrime = false; break; : marks the number as
not prime and exits the loop if a divisor is
OUTPUT : found.
if (isPrime) : prints that the number is prime
if no divisor is found.
else : prints that the number is not prime
import [Link]; : imports Scanner
2. Write a java program to check whether a given
class for user input.
number is palindrome or not.
int num = [Link](); : reads the number
entered by the user.
int original = num; : stores the original
number for comparison after reversal.
int reverse = 0; : initializes a variable to store
the reversed number.
while (num > 0) : runs the loop until all
digits of the number are processed.
int digit = num % 10; : extracts the last digit
of the number.
reverse = reverse * 10 + digit; : builds the
reversed number digit by digit.
num = num / 10; : removes the last digit from
the number.
OUTPUT : if (original == reverse) : compares the
original number with the reversed number.
[Link](...) : prints whether the
number is palindrome or not.
3. Write a java program to calculate the Area and import [Link]; : imports Scanner
Perimeter of Square and Rectangle and Area class to take user input.
and Circumference of a Circle and Cylinder. double side = [Link](); : reads the
side of the square as a decimal value.
side * side : calculates the area of the square
using the formula side².
4 * side : calculates the perimeter of the
square.
double length, breadth : stores dimensions of
the rectangle.
length * breadth : calculates the area of the
rectangle.
2 * (length + breadth) : calculates the
perimeter of the rectangle using correct
operator precedence.
double radius = [Link](); : reads the
radius of the circle.
[Link] * radius * radius : calculates the
area of the circle using πr².
2 * [Link] * radius : calculates the
circumference of the circle.
double r, h : stores radius and height of the
cylinder.
2 * [Link] * r * (r + h) : calculates the total
surface area of the cylinder.
2 * [Link] * r : calculates the circumference
of the circular base of the cylinder.
[Link]() : displays all calculated
values clearly on the console.
OUTPUT :