ICSE Class 9 – Java Number Programs
Using for Loop
1. Prime Number
Definition: A prime number is a number that has exactly two factors: 1 and itself.
Example: 7 is a prime number because it is only divisible by 1 and 7.
Java Program:
import [Link];
public class PrimeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int count = 0;
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
count = count + 1;
}
}
if (count == 2)
[Link](n + " is a Prime number.");
else
[Link](n + " is NOT a Prime number.");
}
}
2. Niven Number
Definition: A Niven number is a number that is divisible by the sum of its digits.
Example: 18 is a Niven number because 1 + 8 = 9 and 18 is divisible by 9.
Java Program:
import [Link];
public class NivenCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
int temp = n;
for (int t = temp; t > 0; t = t / 10) {
sum = sum + t % 10;
}
if (n % sum == 0)
[Link](n + " is a Niven number.");
else
[Link](n + " is NOT a Niven number.");
}
}
3. Armstrong Number
Definition: An Armstrong number is a number in which the sum of cubes of its digits is
equal to the number itself.
Example: 153 is an Armstrong number because 1³ + 5³ + 3³ = 153.
Java Program:
import [Link];
public class ArmstrongCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
int temp = n;
for (int t = temp; t > 0; t = t / 10) {
int d = t % 10;
sum = sum + (d * d * d);
}
if (sum == n)
[Link](n + " is an Armstrong number.");
else
[Link](n + " is NOT an Armstrong number.");
}
}
4. Perfect Number
Definition: A perfect number is a number whose sum of its proper divisors (excluding the
number itself) equals the number.
Example: 28 is a perfect number because 1 + 2 + 4 + 7 + 14 = 28.
Java Program:
import [Link];
public class PerfectCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0;
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum = sum + i;
}
}
if (sum == n)
[Link](n + " is a Perfect number.");
else
[Link](n + " is NOT a Perfect number.");
}
}
5. Palindrome Number
Definition: A palindrome number is a number that remains the same when its digits are
reversed.
Example: 121 is a palindrome number.
Java Program:
import [Link];
public class PalindromeCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int rev = 0;
int temp = n;
for (int t = temp; t > 0; t = t / 10) {
rev = rev * 10 + (t % 10);
}
if (rev == n)
[Link](n + " is a Palindrome number.");
else
[Link](n + " is NOT a Palindrome number.");
}
}
6. Spy Number
Definition: A spy number is a number where the sum of its digits is equal to the product of
its digits.
Example: 123 is a spy number because 1 + 2 + 3 = 6 and 1 * 2 * 3 = 6.
Java Program:
import [Link];
public class SpyCheck {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter a number: ");
int n = [Link]();
int sum = 0, prod = 1;
int temp = n;
for (int t = temp; t > 0; t = t / 10) {
int d = t % 10;
sum = sum + d;
prod = prod * d;
}
if (sum == prod)
[Link](n + " is a Spy number.");
else
[Link](n + " is NOT a Spy number.");
}
}