Help with loops please!!

This is a discussion on Help with loops please!! within the C Programming forums, part of the General Programming Boards category; I need help writing a program with this loop. I do not need help with the math part of it. ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    20

    Unhappy Help with loops please!!

    I need help writing a program with this loop. I do not need help with the math part of it. The question is, A perfect # is a # which is the sum of all its divisors except itself. Six is the first perfect #; the only #s which divide 6 evenly are 1,2,3,6 and 6=1+2+3. An abundant # is one which is one less than the sum of its divisors
    (12 <1+2+3+4+6); a deficient # is greater than the sum of its divisors (9>1+3).
    Write a complete 'C' program which classifies each of the first N integers (where N is entered by the user) as either perfect, abundant, or deficient. The output should be formatted so that the program generates a table.

    Here is what I have so far....
    # include <stdio.h>
    main()
    {
    int count, abundant, perfect, deficient, sum=0, num;

    printf("Enter a number\n");
    scanf("%d", &num);

    printf("ABUNDANT PERFECT DEFICIENT\n");
    printf("-------- ------- ---------\n");

    count=1;
    while (count <= 20)
    {
    printf("%d ",count);
    ++count;
    }

    for (count=1; count <= 20; count++);
    {
    sum=0;

    I cannot figure out what to do after this. I am stuck. Please help in anyway possible. I am not asking for anyone to write the program for me. Thanks in advance,
    Kristina

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    46
    Well, the first question I have is, is there an upper bound to this problem? In other words, is there a maximum number that will be input or do we have to account for all numbers?

    My general idea for this would be
    *given n is your number
    *iterate through all numbers less than n and determine whether n is divisible by the number or not (use a for loop and modulus, %, for this)
    *if the n is divisible by this new number, add it to a "counting number".... in your example, we determine that 1, 2, 3 divide six... as each number is encountered, we add it to this counting number....
    *after the loop terminates, compare the counting number to the origional number. if the counting number equals the origional number, it is a perfect number... if the counting number is one greater than the origional number, it is an abundant number, and if the counting number is less than the origional number, it is a deficient number.
    *Output accordingly =)

    -Maxthecat

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    20
    Supposed to go up to 20. and, I mainly need help getting the output to generate a table. which lists...

    NUMBER TYPE
    2 Deficient
    3 Deficient
    etc. etc.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Code:
    include <stdio.h>
    
    enum {deficient, perfect, abundant};
    char * text [] = {"Deficient", "Perfect", "Abundant"};
    
    int main (void)
    {
     int i;
     printf ("%20s%20s\n", "Number", "Type");
     printf ("%20s%20s\n", "------", "----");
    
     for (i = 0; i < 21; i++)
     printf ("%20d%20s\n", i, text[isPerfect(i)]);
    
     return 0;
    }
    The function isPerfect() will do all the math, and it's return will be 0, 1, or 2, based on whether the number is deficient, perfect, or abundant.
    Callou collei we'll code the way
    Of prime numbers and pings!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple thread for loops
    By lehe in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2009, 01:01 PM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. recoursion or loops?
    By Mecnels in forum C++ Programming
    Replies: 2
    Last Post: 01-14-2002, 12:09 PM
  4. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  5. for loops - newbie q's
    By Narciss in forum C Programming
    Replies: 8
    Last Post: 09-26-2001, 03:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21