ANURAG BASU
CLASS – 11
SECTION – D
ROLL NUMBER – 7
REGISTRATION NUMBER – 2015/253
SESSION - 2025-2026
TOPIC – PROGRAMS USING JAVA FOR ISC
SUBMISSION DATE – 18/08/2025
COMPUTER PROJECT
Question 1
Armstrong Number
Logic:
An Armstrong number of n digits is a number that is equal to the sum of its own digits each
raised to the power n.
Example:
153 → 1³ + 5³ + 3³ = 153
Class Name: ArmstrongNumber
Data Members:
int number — stores the input number
Constructors:
Default constructor — initializes number to 0
Parameterized constructor — takes an integer and sets number
Member Methods:
void inputNumber() — reads the number from the keyboard
void rangeNumber(int x, int y) — displays all the Armstrong numbers between the
range provided
boolean isArmstrong() — checks and returns whether the number is an Armstrong
number
void display() — prints the result
Main():
Create an object, input a number or range of numbers and display if it’s Armstrong.
Algorithm: ArmstrongNumber
Step 1 – Start
Step 2 – Class Initialization
1. Declare a private integer data member number to store a number for checking.
2. Create a default constructor:
o Set number = 0.
3. Create a parameterized constructor:
o Accept an integer n and assign it to number.
Step 3 – Input a Single Number (inputNumber() method)
1. Create a Scanner object to read user input.
2. Prompt the user: "Enter a number:".
3. Read the integer from the keyboard and store it in number.
Step 4 – Check Armstrong Numbers in a Range (rangeNumber(x, y) method)
1. Display: "Armstrong numbers between x and y:".
2. Loop from i = x to i = y:
o Call isArmstrong(i):
If it returns true, print i.
3. After the loop, print a newline.
Step 5 – Check if a Number is Armstrong (isArmstrong(num) method)
1. Store num in dup (duplicate copy).
2. Initialize sum = 0.
3. Find the number of digits:
o Convert num to a string and take its length (count).
4. While dup > 0:
o Extract the last digit: digit = dup % 10.
o Add digit^count to sum (Math.pow(digit, count)).
o Remove the last digit: dup = dup / 10.
5. After the loop, return true if sum == num, else return false.
Step 6 – Display Result for Single Number (display() method)
1. Print "The number is: " + number.
2. If isArmstrong(number) returns true:
o Print "number is an Armstrong number."
3. Else:
o Print "number is not an Armstrong number."
Step 7 – Main Method Execution (main())
1. Create a Scanner object.
2. Prompt: "Enter a value:" and read integer N.
3. Create an object: ArmstrongNumber armstrong = new ArmstrongNumber(N).
4. Call inputNumber() to allow the user to re-enter a single number.
5. Call display() to check and print Armstrong status of number.
6. Prompt for range start (x) and range end (y).
7. Call rangeNumber(x, y) to display all Armstrong numbers in the given range.
Step 8 – End
ANSWER 1
import java.util.*;
public class ArmstrongNumber {
// Instance variable to store a number
private int number;
// Default constructor: initializes number to 0
public ArmstrongNumber() {
number = 0;
// Parameterized constructor: sets number to given value
public ArmstrongNumber(int n) {
number = n;
// Method to take a single number as input from the user
void inputNumber() {
Scanner sc = new Scanner(System.in); // Scanner for input
System.out.print("Enter a number: ");
number = sc.nextInt(); // Store the entered number
// Method to display Armstrong numbers in a given range
void rangeNumber(int x, int y) {
System.out.println("Armstrong numbers between " + x + " and " + y + ":");
// Loop through the range
for (int i = x; i <= y; i++) {
if (isArmstrong(i)) { // Check if the number is Armstrong
System.out.print(i + " "); // Print if true
System.out.println(); // New line after printing all numbers
// Method to check if a given number is Armstrong
boolean isArmstrong(int num) {
int dup = num; // Duplicate copy of the number
int sum = 0; // Variable to store sum of powered digits
int digit; // Variable to store each digit
// Count number of digits in the number
int count = String.valueOf(num).length();
// Loop until all digits are processed
while (dup > 0) {
digit = dup % 10; // Extract last digit
sum += Math.pow(digit, count); // Add digit^count to sum
dup /= 10; // Remove last digit
}
// Return true if sum of powered digits equals original number
return sum == num;
// Method to display the Armstrong check result for the stored number
void display() {
System.out.println("The number is: " + number);
// Check Armstrong status and display result
if (isArmstrong(number))
System.out.println(number + " is an Armstrong number.");
else
System.out.println(number + " is not an Armstrong number.");
// Main method: program entry point
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Scanner object for input
// Input a single number and check if it is Armstrong
System.out.print("Enter a value: ");
int N = sc.nextInt(); // Read number from user
// Create ArmstrongNumber object with given number
ArmstrongNumber armstrong = new ArmstrongNumber(N);
// Let user input number again (optional based on your logic)
armstrong.inputNumber();
// Display whether the entered number is Armstrong
armstrong.display();
// Input range from user
System.out.print("Enter range start: ");
int x = sc.nextInt();
System.out.print("Enter range end: ");
int y = sc.nextInt();
// Display all Armstrong numbers in the given range
armstrong.rangeNumber(x, y);
}
Question 2 – Automorphic Number
Logic:
A number is called Automorphic if its square ends with the number itself.
Example: 25, because 25² = 625, and 25 is at the end → Automorphic.
Class Name: AutomorphicNumber
Data Members:
int number
Constructors:
AutomorphicNumber() – Default constructor
AutomorphicNumber(int n) – Parameterized constructor
Member Methods:
void readNumber() – Reads the number from the keyboard
void rangeNumber(int x, int y) – Finds all the automorphic numbers between the
range x and y
boolean isAutomorphic() – Square number, check if original is suffix of square
void display()
Main():
Input number or a range and display whether it's automorphic.
Algorithm for Automorphic Number
Step 1: Start
Step 2: Input Choice
Ask the user whether they want to:
1. Check a single number
2. Check numbers in a range
Step 3: If Single Number Selected
1. Read the number → store in variable number.
2. Square the number → square = number * number.
3. Find the length of the number → digits = count of digits in number.
4. Get last 'digits' digits from the square →
o suffix = square % (10^digits)
5. Check if suffix equals number:
o If yes → Display "number is Automorphic".
o If no → Display "number is NOT Automorphic".
Step 4: If Range Selected
1. Read lower limit x and upper limit y.
2. For each number n from x to y:
o Square the number → square = n * n.
o Count digits in n → digits = count of digits in n.
o Get suffix → suffix = square % (10^digits).
o If suffix equals n → print n as Automorphic.
Step 5: End
ANSWER 2
import java.util.*; // Import the Scanner class for taking user input
// Class to check and display Automorphic Numbers
class AutomorphicNumber {
// Data member to store a single number
private int number;
// Scanner object for reading input
private Scanner sc = new Scanner(System.in);
// Default constructor - sets number to 0
public AutomorphicNumber() {
number = 0;
// Parameterized constructor - sets number to a given value
public AutomorphicNumber(int n) {
number = n;
// Method to input a single number from the user
public void inputNumber() {
System.out.print("Enter a number: ");
number = sc.nextInt(); // Read user input into 'number'
}
// Method to display all automorphic numbers between two given limits
public void rangeNumber(int x, int y) {
System.out.println("Automorphic numbers between " + x + " and " + y + ":");
// Loop through all numbers from x to y
for (int i = x; i <= y; i++) {
if (isAutomorphic(i)) { // Check if current number is automorphic
System.out.print(i + " "); // Print the automorphic number
System.out.println(); // Move to next line after printing range results
// Method to check whether a number is automorphic
boolean isAutomorphic(int num) {
int square = num * num; // Calculate square of the number
int count = String.valueOf(num).length(); // Count digits in the number
// Get the last 'count' digits of the square and compare with the number
return num == square % (int) Math.pow(10, count);
// Method to display whether the stored 'number' is automorphic or not
public void display() {
System.out.println("The number is: " + number);
if (isAutomorphic(number)) // If number is automorphic
System.out.println(number + " is an Automorphic number.");
else // If number is not automorphic
System.out.println(number + " is not an Automorphic number.");
}
// Main method - program execution starts here
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Create Scanner for main method input
// Take input for a single number
System.out.print("Enter a value: ");
int N = sc.nextInt();
// Create an object of AutomorphicNumber with the given value
AutomorphicNumber automorphic = new AutomorphicNumber(N);
// Ask the user to enter a number (overrides the constructor value)
automorphic.inputNumber();
automorphic.display(); // Display if it's automorphic
// Take input for a range of numbers
System.out.print("Enter range start: ");
int x = sc.nextInt();
System.out.print("Enter range end: ");
int y = sc.nextInt();
// Display all automorphic numbers in the given range
automorphic.rangeNumber(x, y);
// Close the Scanner to free resources
sc.close();
}
Question 3 – Harshad Number (Niven Number)
Logic:
A number is Harshad if it is divisible by the sum of its digits.
Example: 18 is a Harshad number because 1 + 8 = 9, and 18 is divisible by 9.
Class Name: HarshadNumber
Data Members:
int number
Constructors:
HarshadNumber() – Default constructor
HarshadNumber(int n) – Parameterized constructor
Member Methods:
void readData() – read a number from the keyboard
void rangeNumber(int x, int y) – Finds all the Harshad numbers between the range x
and y
int digitSum() — sum of digits
boolean isHarshad() — check if number is divisible by digit sum
void display()
Main(): Test Harshad number logic by calling the different methods as mentioned in the
question above.
Algorithm for Harshad Number Program
Step 1: Start
Step 2: Initialize variables
Create an integer variable number to store the number to be tested.
Create an object of the Scanner class to read input from the user.
Step 3: Define constructors
Default constructor: Set number = 0.
Parameterized constructor: Assign the given value to number.
Step 4: Define method inputNumber()
Prompt the user to enter a number.
Store the input value in the variable number.
Step 5: Define method digitSum(num)
Input: integer num.
Initialize sum = 0.
Repeat until num becomes 0:
o Extract the last digit: digit = num % 10.
o Add digit to sum.
o Remove the last digit: num = num / 10.
Return the sum of digits.
Step 6: Define method isHarshad(num)
Input: integer num.
Call digitSum(num) to get the sum of its digits.
Check if num % digitSum(num) == 0:
o If yes, return true (Harshad number).
o Else, return false.
Step 7: Define method display()
Display the value of number.
Call isHarshad(number) and:
o If true, print that number is a Harshad number.
o Else, print that it is not a Harshad number.
Step 8: Define method rangeNumber(x, y)
Input: integers x (start) and y (end).
Display message: "Harshad numbers between x and y are:".
Loop from i = x to y:
o If isHarshad(i) returns true, print i.
Step 9: Main program execution
1. Create Scanner object for reading input in main().
2. Prompt user to enter a number (N) and store it.
3. Create HarshadNumber object using parameterized constructor with N.
4. Call inputNumber() method to read a number from the user.
5. Call display() method to check and display if it is a Harshad number.
6. Prompt user to enter starting and ending range (x and y).
7. Call rangeNumber(x, y) to display all Harshad numbers in the given range.
8. Close the Scanner object.
Step 10: End
ANSWER 3
import java.util.*; // Importing the Scanner class for taking input from the user
// Class to check whether a number is a Harshad (Niven) number
class HarshadNumber {
// Instance variable to store the number
private int number;
// Scanner object for reading user input
private Scanner sc = new Scanner(System.in);
// Default constructor - initializes number to 0
public HarshadNumber() {
number = 0;
// Parameterized constructor - initializes number with given value
public HarshadNumber(int n) {
number = n;
// Method to take input for 'number' from the user
public void inputNumber() {
System.out.print("Enter a number: ");
number = sc.nextInt();
}
// Method to display all Harshad numbers in the given range [x, y]
public void rangeNumber(int x, int y) {
System.out.println("Harshad numbers between " + x + " and " + y + ":");
// Loop through each number in the range
for (int i = x; i <= y; i++) {
// If the number is Harshad, print it
if (isHarshad(i)) {
System.out.print(i + " ");
System.out.println(); // Move to the next line after printing
// Method to calculate sum of digits of a number
public int digitSum(int num) {
int sum = 0;
// Loop until all digits are processed
while (num > 0) {
sum += num % 10; // Add last digit to sum
num /= 10; // Remove last digit
return sum;
// Method to check if a number is Harshad
boolean isHarshad(int num) {
// A number is Harshad if it is divisible by sum of its digits
return (num % digitSum(num) == 0);
}
// Method to display whether the stored 'number' is Harshad
public void display() {
System.out.println("The number is: " + number);
if (isHarshad(number))
System.out.println(number + " is a Harshad number.");
else
System.out.println(number + " is not a Harshad number.");
// Main method - program execution starts here
public static void main(String[] args) {
Scanner sc = new Scanner(System.in); // Scanner for main method input
// Taking input for the parameterized constructor
System.out.print("Enter a value: ");
int N = sc.nextInt();
// Creating object using parameterized constructor
HarshadNumber harshad = new HarshadNumber(N);
// Taking new number input using method
harshad.inputNumber();
// Displaying if the number is Harshad or not
harshad.display();
// Taking range input
System.out.print("Enter range start: ");
int x = sc.nextInt();
System.out.print("Enter range end: ");
int y = sc.nextInt();
// Displaying all Harshad numbers in the given range
harshad.rangeNumber(x, y);
// Closing the Scanner
sc.close();
}
Question 4
Class: MutualPrimeFactors
Problem:
Accept two numbers and display common prime factors between them.
Constructors:
MutualPrimeFactors(int a, int b)
Methods:
boolean isPrime(int n)
int[] getPrimeFactors(int n)
void displayCommonPrimeFactors()
Algorithm: MutualPrimeFactors
Step 1 – Start
Step 2 – Class Definition
Create a class named MutualPrimeFactors.
Declare two integer data members num1 and num2 to store the two input numbers.
Step 3 – Constructor
Define a parameterized constructor MutualPrimeFactors(int a, int b):
o Assign a to num1.
o Assign b to num2.
Step 4 – Check if a Number is Prime (isPrime(int n) method)
1. If n <= 1, return false (numbers less than 2 are not prime).
2. If n == 2 or n == 3, return true (2 and 3 are prime).
3. If n is divisible by 2 or 3, return false.
4. Loop from i = 5 to √n, incrementing by 6:
o If n is divisible by i or i + 2, return false.
5. If no divisor found, return true.
Step 5 – Find Prime Factors of a Number (getPrimeFactors(int n) method)
1. Convert n to its absolute value (ignore negative sign).
2. Create an empty list/array to store prime factors.
3. While n is divisible by 2:
o Add 2 to the list (if not already added).
o Divide n by 2.
4. For odd p from 3 to √n, step 2:
o While n is divisible by p:
Add p to the list (if not already added).
Divide n by p.
5. If after the loop n > 1, add n to the list.
6. Return the list of unique prime factors.
Step 6 – Display Common Prime Factors (displayCommonPrimeFactors() method)
1. Get the list of prime factors of num1 using getPrimeFactors(num1).
2. Get the list of prime factors of num2 using getPrimeFactors(num2).
3. Compare both lists to find common elements.
4. If there are no common factors:
o Print "No common prime factors".
5. Else:
o Print "Common prime factors:" followed by the list of common prime factors.
Step 7 – Main Execution Flow
1. Create a Scanner object for input.
2. Prompt the user to enter two integers.
3. Read the two integers and store them as a and b.
4. Create an object of MutualPrimeFactors using the constructor:
MutualPrimeFactors mpf = new MutualPrimeFactors(a, b).
5. Call mpf.displayCommonPrimeFactors() to display the result.
Step 8 – End
ANSWER 4
import java.util.Scanner;
public class MutualPrimeFactors {
private int a, b;
public MutualPrimeFactors(int a, int b) {
this.a = a;
this.b = b;
// Check if a number is prime
boolean isPrime(int n) {
if (n < 2) return false;
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) return false;
return true;
// Get all prime factors of n (no duplicates, no HashSet)
int[] getPrimeFactors(int n) {
int[] temp = new int[n / 2 + 1]; // Maximum possible number of factors
int count = 0;
n = Math.abs(n);
for (int i = 2; i <= n; i++) {
if (n % i == 0 && isPrime(i)) {
// Check for duplicates before adding
boolean exists = false;
for (int j = 0; j < count; j++) {
if (temp[j] == i) {
exists = true;
break;
if (!exists) {
temp[count++] = i;
// Copy to result array of correct size
int[] result = new int[count];
for (int i = 0; i < count; i++) {
result[i] = temp[i];
return result;
// Display common prime factors
void displayCommonPrimeFactors() {
int[] factorsA = getPrimeFactors(a);
int[] factorsB = getPrimeFactors(b);
System.out.print("Common prime factors of " + a + " and " + b + ": ");
boolean found = false;
for (int i = 0; i < factorsA.length; i++) {
for (int j = 0; j < factorsB.length; j++) {
if (factorsA[i] == factorsB[j]) {
System.out.print(factorsA[i] + " ");
found = true;
break; // Avoid duplicate printing
if (!found) {
System.out.print("None");
System.out.println();
// Main method for testing
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = sc.nextInt();
System.out.print("Enter second number: ");
int b = sc.nextInt();
MutualPrimeFactors mpf = new MutualPrimeFactors(a, b);
mpf.displayCommonPrimeFactors();
sc.close();
}
}
Question 5
Class: FactorDifferenceChecker
Problem:
Accept a number and find the difference between the largest and smallest prime factor.
Constructors:
FactorDifferenceChecker() – Default constructor
FactorDifferenceChecker(int n) – Parameterized constructor
Methods:
void readNumber(int n) – reads the value of n from keyboard
int smallestPrimeFactor() – returns the smallest prime factor
int largestPrimeFactor() – returns the largest prime factor
int differenceOfFactors() – returns the difference between the largest and the
smallest
void display() – display the difference with all the details.
Algorithm: FactorDifferenceChecker
Step 1 – Start
Step 2 – Input the number
Prompt the user to enter a positive integer n.
Read and store it in the variable number.
Step 3 – Find the smallest prime factor
1. Start checking from i = 2 (smallest prime number).
2. While i <= number:
o If number % i == 0 and i is prime:
Store i as the smallest prime factor.
Stop the search.
o Else, increment i by 1.
Step 4 – Find the largest prime factor
1. Start checking from i = number down to 2.
2. While i >= 2:
o If number % i == 0 and i is prime:
Store i as the largest prime factor.
Stop the search.
o Else, decrement i by 1.
Step 5 – Calculate the difference
difference = largestPrimeFactor – smallestPrimeFactor
Step 6 – Display the results
Print:
o The original number.
o The smallest prime factor.
o The largest prime factor.
o The difference between them.
Step 7 – End
ANSWER 5
import java.util.Scanner;
public class FactorDifferenceChecker {
// Default constructor
public FactorDifferenceChecker() {
// Parameterized constructor
public FactorDifferenceChecker(int n) {
// Method to get the smallest prime factor
public static int smallestPrimeFactor(int n) {
n = Math.abs(n);
for (int i = 2; i <= n; i++) {
if (n % i == 0) {
int factorcount = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0)
factorcount++;
if (factorcount == 2) {
return i;
}
}
return -1;
// Method to get the largest prime factor
public static int largestPrimeFactor(int n) {
n = Math.abs(n);
for (int i = n; i >= 2; i--) {
if (n % i == 0) {
int factorcount = 0;
for (int j = 1; j <= i; j++) {
if (i % j == 0)
factorcount++;
if (factorcount == 2) {
return i;
return -1;
// Method to get the difference between largest and smallest prime factor
public static int differenceOfFactors(int n) {
int smallest = smallestPrimeFactor(n);
int largest = largestPrimeFactor(n);
return largest - smallest;
}
// Method to display the result
public static void display(int n) {
int smallest = smallestPrimeFactor(n);
int largest = largestPrimeFactor(n);
int difference = differenceOfFactors(n);
if (smallest == -1 || largest == -1) {
System.out.println("No prime factors found for " + n);
} else {
System.out.println("Smallest Prime Factor: " + smallest);
System.out.println("Largest Prime Factor: " + largest);
System.out.println("Difference: " + difference);
// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
FactorDifferenceChecker factordiff = new FactorDifferenceChecker(num);
factordiff.display(num);
sc.close(); // Close the Scanner