0% found this document useful (0 votes)
7 views2 pages

Java Programs for Palindrome and Prime Check

The document contains two Java programs: one checks if a number is a palindrome and the other checks if a number is prime. The palindrome program reverses the digits of the input number and compares it to the original. The prime program checks for factors of the input number to determine if it is prime or composite.

Uploaded by

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

Java Programs for Palindrome and Prime Check

The document contains two Java programs: one checks if a number is a palindrome and the other checks if a number is prime. The palindrome program reverses the digits of the input number and compares it to the original. The prime program checks for factors of the input number to determine if it is prime or composite.

Uploaded by

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

Experiment – 1

Ans a)
import [Link].*;
public class Pallindrome {
static boolean isPallindrome(int n){
int r = 0;
int k = n;
while (n != 0){
r *= 10;
r += n % 10;
n = n / 10;
}
return k == r;
}
public static void main(String[] args) {
Scanner scan = new Scanner([Link]);
[Link]("Enter a number to test: ");
int n = [Link]();
if (isPallindrome(n)){
[Link]("This number is pallindrome");}
else {
[Link]("This number is not
pallindrome");
}
[Link]();
}
}
Ans b)

import [Link].*;

public class Prime {

static boolean isPrime(int n){

for (int i = 2; i <= [Link](n); i++){


if (n % i == 0) return false;
}
return true;

public static void main(String[] args) {


Scanner scan = new Scanner([Link]);

[Link]("Enter a number to test: ");


int n = [Link]();

if (isPrime(n))
[Link]("This is prime");
else
[Link]("This is composite");

[Link]();

}
}

You might also like