Solved

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

Posted on 2014-11-21
8
22 Views
Last Modified: 2016-07-18
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
0
Comment
Question by:kartikeya Bajpayee
  • 4
8 Comments
 
LVL 20

Expert Comment

by:Sathish David Kumar N
ID: 40459387
Can you tell me clearly
0
 
LVL 32

Expert Comment

by:awking00
ID: 40459564
Just to clarify. Are you trying to find numbers whose divisors other than the number itself add up to the number itself? So your example of 6 would be such a number because its divisors are 1,2, and 3, which add up to 6, but 8 would not be such a number because its divisors are 1,2, and 4 which only add up to 7, correct?
0
 
LVL 32

Expert Comment

by:awking00
ID: 40459584
Furthermore, may I assume you want an array containing those divisors?
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Author Comment

by:kartikeya Bajpayee
ID: 40460291
Guys,

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
0
 
LVL 32

Accepted Solution

by:
awking00 earned 500 total points
ID: 40872414
>>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<<

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
0
 
LVL 32

Expert Comment

by:awking00
ID: 41717121
I see no reason why my comment https:#a40872414 (not sure I did this right) should not be accepted.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Python Assistance 7 81
MySQL  on Tomcat 8 46
Android development question 2 37
jsp login check 12 32
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question