0% found this document useful (0 votes)
13 views4 pages

Prgs

The document describes two Java programs. The first program checks for non-prime palindromic numbers within a given range. It uses methods to check if a number is prime and if it is a palindrome. The second program checks if two numbers are co-prime by finding their highest common factor.

Uploaded by

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

Prgs

The document describes two Java programs. The first program checks for non-prime palindromic numbers within a given range. It uses methods to check if a number is prime and if it is a palindrome. The second program checks if two numbers are co-prime by finding their highest common factor.

Uploaded by

Dulari Murmu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Program:

Class Name: NonPrimePalin

import java.util.Scanner;

public class NonPrimePalin {


int x, y;

NonPrimePalin() {
x = 0;
y = 0;
}

void getnum(int xx, int yy) {


x = xx;
y = yy;
}

void calNonPrimePalin() {
for (int i = x; i <= y; i++) {
int res1 = nonPrime(i);
int res2 = palindrome(i);
if (res1 == 1 && res2 == 1) {
System.out.println(i);
}
}
}

int nonPrime(int p) {
int tf = 0;
for (int i = 1; i <= p; i++) {
if (p % i == 0) {
tf += 1;
}
}
if (tf == 2) {
return 0;
} else {
return 1;
}
}

int palindrome(int p) {
int rev = 0, temp = p, d = 0;
while (temp > 0) {
d = temp % 10;
rev = rev * 10 + d;
temp /= 10;
}
if (rev == p) {
return 1;
} else {
return 0;
}
}

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
int a, b;
System.out.println("Enter the lower and upper limit: ");
System.out.print("a = : ");
a = in.nextInt();
System.out.print("b = : ");
b = in.nextInt();
NonPrimePalin obj = new NonPrimePalin();
obj.getnum(a, b);
obj.calNonPrimePalin();
}
}

Output:
2. Program: CoPrime Numbers:

Class name: CoPrime

import java.util.Scanner;

public class CoPrime {


int a, b;

void getInput() {
Scanner in = new Scanner(System.in);
System.out.println("Enter a: ");
a = in.nextInt();
System.out.println("Enter b: ");
b = in.nextInt();
}

void checkCoPrime() {
int hcf = 1;
for (int i = 1; i <= a && i <= b; i++) {
if (a % i == 0 && b % i == 0) {
hcf = i;
}
}
if (hcf == 1) {
System.out.println(a + " and " + b + " is Co Prime.");
} else {
System.out.println(a + " and " + b + " are not Co Prime.");
}
}

public static void main(String[] args) {


CoPrime obj = new CoPrime();
obj.getInput();
obj.checkCoPrime();
}

You might also like