This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Hi

I want your help in understanding a java problem which iam encountering i know that you are a busy lad but it wont take a few minutes for you to guide me

The problem is suppose i entered a positive integer 8 and i have written a program to find out its divisors now

which are 1,2,4

I want to put this value in an array so suppose we have an array int [] num=i

then num[0]=1,num[1]=2 and num[2]=4

Can you please tell me how to do this

Please help me

import java.util.ArrayList;

import java.util.Scanner;

public class perfectnumber {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Please Enter a Positive number");

int num=scan.nextInt();

for(int i=1;i<num;i++){

int k=num%i;

if(k==0){

System.out.println("i="+i);

}

}

}

Regards

Kartik

"Sir,

Thanks for your Message since iam a Java beginner iam asking you these trivial issues so please excuse me for that ,Actually what i wanted to say here is if you see my program it is a program which will yield divisors but i wish to extend my program and make it into a program for evaluating perfect number (A perfect number is a algorithm in which the exact divisors of a particular number excluding the number itself totals to a particular number say for example 6 is one such number),Now my problem is here is i want to convert this variable "i" which is being calculated here into an Array so that if we calculate the divisors of fo ex somehow i[0] should be 1,i[1] should be 2 and i[2] should be 3.Can you tell me how to achieve this?"

perfectnumber.java

I want your help in understanding a java problem which iam encountering i know that you are a busy lad but it wont take a few minutes for you to guide me

The problem is suppose i entered a positive integer 8 and i have written a program to find out its divisors now

which are 1,2,4

I want to put this value in an array so suppose we have an array int [] num=i

then num[0]=1,num[1]=2 and num[2]=4

Can you please tell me how to do this

Please help me

import java.util.ArrayList;

import java.util.Scanner;

public class perfectnumber {

public static void main(String[] args) {

Scanner scan = new Scanner(System.in);

System.out.println("Please

int num=scan.nextInt();

for(int i=1;i<num;i++){

int k=num%i;

if(k==0){

System.out.println("i="+i)

}

}

}

Regards

Kartik

"Sir,

Thanks for your Message since iam a Java beginner iam asking you these trivial issues so please excuse me for that ,Actually what i wanted to say here is if you see my program it is a program which will yield divisors but i wish to extend my program and make it into a program for evaluating perfect number (A perfect number is a algorithm in which the exact divisors of a particular number excluding the number itself totals to a particular number say for example 6 is one such number),Now my problem is here is i want to convert this variable "i" which is being calculated here into an Array so that if we calculate the divisors of fo ex somehow i[0] should be 1,i[1] should be 2 and i[2] should be 3.Can you tell me how to achieve this?"

perfectnumber.java

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Thanks for your Response

As i told you a perfect number is a program where the sum of divisors add up to the number itself

For Ex:6 has divisors as 1,2,3 other then 6 itself and the sum of these numbers accumulate to give "6" as summation

6=1+2+3

I have made a program which gives positive divisors of a certain number this number is contained in "i" My idea is to Transform i into an array so that i[0]=1,i[1]=2,i[3]=3 so that i can make a sum of it

Iam struggling a bit here on two accounts

1>I do want a fixed size for this Array

2>I want this program where i have calculated i to be transformed into an Array of integers

Can you please tell me how to do that please don't give any hints because iam a java beginner i cannot understand them explicitly

Can somebody enhance this program in this way please

Sorry but iam a beginner and have to disturb you guys because of these trivial questions

Regards

kartik

1>I do want a fixed size for this Array

2>I want this program where i have calculated i to be transformed into an Array of integers<<

1> How can you have a fixed size for this Array? Since 6 is a perfect number, the array would look like [1,2,3]. 28 is also a perfect number, but its array would look like [1,2,4,7,14] so the Array size can not be fixed.

2> Since the Array size can not be fixed, I think you will have to use an ArrayList instead. With an ArrayList, you can just use the add method.

Incidentally, a perfect number is further defined by some prime number, p, such that the formula 2^(p -1) * (2^p - 1) results in that number.

For 6, the prime number is 2, so 2^(2-1) = 2^1 = 2 times 2^2 - 1 = 4 - 1 = 3

For 28, the prime number is 3, 2^(3-1) = 4 times 2^3 - 1 = 7

For 496, the prime number is 5, 2^4 * (2^5 - 1) = 16 * 31 = 496

The attached file should demonstrate how you might approach the problem using your method of identifying the divisors. (I actually modified your loop a little to only loop until i <= num/2 since any number greater than that can not be evenly divisible).

perfect-nums.txt

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
I see no reason why my comment https:#a40872414 (not sure I did this right) should not be accepted.

Java

From novice to tech pro — start learning today.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with Premium.
Start your 7-day free trial.