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
14 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 31

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 31

Expert Comment

by:awking00
ID: 40459584
Furthermore, may I assume you want an array containing those divisors?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 31

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 31

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

19 Experts available now in Live!

Get 1:1 Help Now