?
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
Medium Priority
?
34 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 2000 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

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 will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

770 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