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

Java Class for Perfect Number Array

The document outlines a Java program that defines an Array class with methods to input integers, check for perfect numbers, and display those perfect numbers. The program prompts the user to enter 10 integers and then identifies and prints the perfect numbers among them. It includes a detailed explanation of the methods and their functionalities, as well as a sample input and output.

Uploaded by

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

Java Class for Perfect Number Array

The document outlines a Java program that defines an Array class with methods to input integers, check for perfect numbers, and display those perfect numbers. The program prompts the user to enter 10 integers and then identifies and prints the perfect numbers among them. It includes a detailed explanation of the methods and their functionalities, as well as a sample input and output.

Uploaded by

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

Create a class array with data member int a{10] and create functions:

void input(): input data in the array


Boolean check(int v): return true if v is perfect otherwise false
void display(): display only perfect number from the array
void main(): to call all methods

Step 1: Create an instance of the Array class in the main() method.

Step 2: Call the input() method to read the array elements from the user.

Step 3: In the input() method:

Initialize a for loop to iterate 10 times (since the array size is 10).
In each iteration:
Prompt the user to enter an element using [Link]("Enter an element: ").
Read the user's input using [Link]() and store it in the array a at the current
index i.
Step 4: After reading all the array elements, call the display() method to print
the perfect numbers in the array.

Step 5: In the display() method:

Initialize a for loop to iterate 10 times (since the array size is 10).
In each iteration:
Check if the current array element a[i] is a perfect number by calling the
perfect(a[i]) method.
If a[i] is a perfect number, print it using [Link](a[i]).
Step 6: In the perfect(int v) method:

Initialize a variable s to 0 to store the sum of the proper divisors of v.


Initialize a for loop to iterate from 1 to v-1.
In each iteration:
Check if v is divisible by the current number i using v % i == 0.
If v is divisible, add i to the sum s.
Return true if s is equal to v, indicating that v is a perfect number; otherwise,
return false.

CODE:

import [Link].*;
class Array
{
int a[] = new int[10];
Scanner sc = new Scanner([Link]);

void input()
{
for (int i = 0; i < 10; i++)
{
[Link]("Enter an element: ");
a[i] = [Link]();
}
}
boolean perfect(int v)
{
int s = 0;
for (int i = 1; i < v; i++)
{
if (v % i == 0)
{
s = s + i;
}
}
return s == v;
}
void display()
{
for (int i = 0; i < 10; i++)
{
if (perfect(a[i]))
{
[Link](a[i]);
}
}
}
}

SAMPLE INPUT: Enter an element: 6 28 12 15 24 18 36 20 48 30


OUTPUT:6 28

VARIABLE LIST:

Variable Name Data Type Description


a int[] An array to store 10 integer elements
i int A loop counter variable
v int A variable to store the current element
being checked for perfection
s int A variable to store the sum of divisors of
a number

You might also like