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
21 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 76
where is session ID cache stored 1 46
DO we need Java installed on a Windows PC and WHY ? 13 97
Basic Java Case or If-Else statement... 3 43
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now