0% found this document useful (0 votes)
5 views5 pages

ICSE Java Number Programs Updated

The document provides Java programs for checking various types of numbers, including Prime, Niven, Armstrong, Perfect, Palindrome, and Spy numbers. Each section includes a definition, an example, and a Java code snippet that implements the logic to determine if a given number falls into the respective category. The programs utilize loops and basic arithmetic operations to perform the checks.

Uploaded by

milindcontractor
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)
5 views5 pages

ICSE Java Number Programs Updated

The document provides Java programs for checking various types of numbers, including Prime, Niven, Armstrong, Perfect, Palindrome, and Spy numbers. Each section includes a definition, an example, and a Java code snippet that implements the logic to determine if a given number falls into the respective category. The programs utilize loops and basic arithmetic operations to perform the checks.

Uploaded by

milindcontractor
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

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.");
}
}

You might also like