14 Replies - 1257 Views - Last Post: 28 October 2014 - 09:47 PM Rate Topic: -----

#1 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Perfect number program help PLEASE

Posted 28 October 2014 - 07:10 PM

import java.util.Scanner;
public class pdonahue_Perfect
{
    public static void main (String args[])
{
        int test;
        int value=0;
        Scanner input = new Scanner(System.in);

    do
{
    System.out.print("How many numbers would you like to test? ");
    test = input.nextInt();
}
    while(test<1);

   // int counter = 0;
    do
{
    System.out.print("Please enter a possible perfect number: ");
    value = input.nextInt();
    testPerfect(value);
    printFactors(value);
   // counter++;
}
    while(value<test);
}

    public static boolean testPerfect(int value)
{
    int sum=0;
    int test=0;
    for(int i = 0; i < test ; i++)
{
    if(value % i==0)
{
    sum += i;
}
}
    if(sum==value)
{
    return true;
}
    else
{
    return false;
}
}
    public static void printFactors(int value)
{
    int x;
    int sum=0;
//  int factor;

     Scanner input= new Scanner(System.in);

    for(int factor=1; factor < value; factor++)
{
    x = value % factor;

    if (x==0)
{
    sum = sum + factor;
}
}
    if (sum!= value)
{
    System.out.printf("%d:NOT PERFECT",value);
}

    if(sum==value)
{
    System.out.printf("%d: ",value);

    for(int factor=1; factor < value; factor++)
{
    x=value%factor;
    if(x==0)
{
    System.out.printf("%d ",factor);
}
}
}
    System.out.print("\n");
    sum=0;
}
}


i have just a couple of issues when running my program.
-First, When i enter 0 it should print..

Please enter a possible perfect number: 0
0:NOT PERFECT

-Second, when i get a perfect number. it should show the answer in descending order..

Please enter a possible perfect number: 6
6:3 2 1

mine shows:
Please enter a possible perfect number: 6
6: 1 2 3

**everything else compiles correctly with no errors

Thanks!!

Is This A Good Question/Topic? 0
  • +

Replies To: Perfect number program help PLEASE

#2 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 07:48 PM

when i put 0 in for my input i get
Please enter a possible perfect number: 0
0:

This is incorrect
Was This Post Helpful? 0
  • +
  • -

#3 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 07:58 PM

okay now i just have the 0 problem. I fixed the other issue.
Was This Post Helpful? 0
  • +
  • -

#4 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:08 PM

ill post my revised code
Was This Post Helpful? 0
  • +
  • -

#5 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:17 PM

import java.util.Scanner;
public class pdonahue_Perfect
{
    public static void main (String args[])
    {
        int test;
        int value=0;
        Scanner input = new Scanner(System.in);

    do
{
    System.out.print("How many numbers would you like to test? ");
    test = input.nextInt();
}
    while(test<1);

   // int counter = 0;
    do
{
    System.out.print("Please enter a possible perfect number: ");
    value = input.nextInt();
    testPerfect(value);
    printFactors(value);
   // counter++;
}
    while(value<test);
    }

    public static boolean testPerfect(int value){
    int sum=0;
    int test=0;
    for(int i = 0; i < test ; i++)
  {
    if(value % i==0)
    {
    sum += i;
    }
  }
    if(sum==value)
{
    return true;
}
    else
    {
    return false;
    }
}
    public static void printFactors(int value){
    int x;
    int sum=0;
//    int factor;

    Scanner input= new Scanner(System.in);

    for(int factor = value - 1; factor >0; factor--)
{
    x = value % factor;

    if (x==0)
    {
    sum = sum + factor;
    }
}
    if (sum!= value)
    {
    System.out.printf("%d:NOT PERFECT",value);
    }

    if(sum==value)
    {
    System.out.printf("%d: ",value);

    for(int factor = value - 1; factor > 0; factor--)
{
    x=value%factor;
    if(x==0)
        {
    System.out.printf("%d ",factor);
        }
}
    }
    System.out.print("\n");
    sum=0;
}
}


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:29 PM

I'm glad you figured out your issue and thank you for sharing your solution. However, as I've asked in each of your previous threads, please make an honest effort to post readable and cleanly formatted code. Frankly, your code is quite difficult to read/follow, and I've made the point numerous times that it is difficult for us to help you if we can't follow your code easily.
Was This Post Helpful? 0
  • +
  • -

#7 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:34 PM

dang-it, i tried to line up the curly brackets from the open to close. and i made sure to put the code tags around correctly also. i'm trying to indent as you would like. just going to take more practice.

I just found another issue..

whenever i input 16. it stops my loop.
How many numbers would you like to test? 10
Please enter a possible perfect number: 16
16:NOT PERFECT
***then it starts all over, even though i should be able to input 9 more inputs.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:42 PM

This really isn't rocket science to format your code.
    do
{
    System.out.print("How many numbers would you like to test? ");
    test = input.nextInt();
}
    while(test<1);



Should really be formatted as:
    do {
       System.out.print("How many numbers would you like to test? ");
       test = input.nextInt();
    }while(test<1);



Once you have properly formatted code, I will be happy to take a look. But I'm not going to try and weed through the mess you've posted, especially since I've taken the time in two of your other threads to explain in detail good practices. Please meet us halfway.
Was This Post Helpful? 0
  • +
  • -

#9 infernorthor  Icon User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:44 PM

you should put your code in at least a text editor to get proper indentation and copy it over

You shouldn't be surprised of the output, read the code you wrote!
while(value<test);
Was This Post Helpful? 1
  • +
  • -

#10 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:49 PM

how about this...

import java.util.Scanner;
public class pdonahue_Perfect {
    public static void main(String args[]) {
        int test;
        int value = 0;
        Scanner input = new Scanner(System.in);

        do {
            System.out.print("How many numbers would you like to test? ");
            test = input.nextInt();
        }
        while (test < 1);

        int counter = 0;
        do {
            System.out.print("Please enter a possible perfect number: ");
            value = input.nextInt();
            testPerfect(value);
            printFactors(value);
            counter++;
        }
        while (value < test);
    }

    public static boolean testPerfect(int value) {
        int sum = 0;
        int test = 0;
        for (int i = 0; i < test; i++) {
            if (value % i == 0) {
                sum += i;
            }
        }
        if (sum == value) {
            return true;
        } else {
            return false;
        }
    }
    public static void printFactors(int value) {
        int x;

        int sum = 0;
        //    int factor;

        Scanner input = new Scanner(System.in);

        for (int factor = 1; factor < value; factor++) {
            x = value % factor;

            if (x == 0) {
                sum = sum + factor;
            }
        }
        if (sum != value) {
            System.out.printf("%d:NOT PERFECT", value);
        }

        if (sum == value) {
            System.out.printf("%d: ", value);

            for (int factor = value - 1; factor > 0; factor--) {
                x = value % factor;
                if (x == 0) {
                    System.out.printf("%d ", factor);
                }
            }
        }
        System.out.print("\n");
        sum = 0;
    }
}

Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:51 PM

Perfect! That really looks a lot better. Thank you for cleaning up your code. And in your second loop, I think you want counter < test, not value < test.
Was This Post Helpful? 1
  • +
  • -

#12 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:53 PM

im glad i got that correct now lol :bananaman:

i will change it and try it now.
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12147
  • View blog
  • Posts: 45,157
  • Joined: 27-December 08

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 08:55 PM

And it's an important skill to have correct! Better to make a few folks on the internet happy now, than to piss your coworkers off in a few years when they can't make heads or tails of your code. And yes- they will be working with your code (and vice versa). :)
Was This Post Helpful? 1
  • +
  • -

#14 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 09:13 PM

thank you very much!! that got rid of the loop ending etc. However, when i input 0 it says:

Please enter a possible perfect number: 0
0:

but, its supposed to say:

Please enter a possible perfect number: 0
0:NOT PERFECT

Thats the last issue i am having.
Was This Post Helpful? 0
  • +
  • -

#15 pdonahue  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 67
  • Joined: 26-September 14

Re: Perfect number program help PLEASE

Posted 28 October 2014 - 09:47 PM

its something with my if statements at the very end method. i changed the last method to this...

public static void printFactors(int value) {
  int x;
  int sum = 0;

  Scanner input = new Scanner(System.in);

  for (int factor = 1; factor < value; factor++) {
      x = value % factor;

      if (x == 0) {
          sum = sum + factor;
      }
  }
  if (sum != value) {
    System.out.printf("%d:NOT PERFECT", value);
  }
  if (sum == 0){
    System.out.printf("%d:NOT PERFECT", value);
  }
  if (sum == value || sum != 0) {
    System.out.printf("%d: ", value);

    for (int factor = value - 1; factor > 0; factor--) {
      x = value % factor;
      if (x == 0) {
          System.out.printf("%d ", factor);
      }
    }
  }
  System.out.print("\n");
  sum = 0;
  }
}




i get this now..

Please enter a possible perfect number: 0
0:NOT PERFECT0:

i got it!!!

i needs to be:
if (sum != value || sum ==0) {
    System.out.printf("%d:NOT PERFECT", value);
  }
  if (sum == value && sum != 0) {
    System.out.printf("%d: ", value);



thanks to everyone that helped me out with this program, i appreciate it very much! God night y'all. :rockon:

good***
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1