Printing perfect numbers

This is a discussion on Printing perfect numbers within the C Programming forums, part of the General Programming Boards category; Hello, these are my first steps in C. I need some help in the following code: Code: #include <stdio.h> #include ...

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Loop Help

    Hello,
    these are my first steps in C.
    I need some help in the following code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
         int n,i,j,t;
         printf("Enter N:");
         scanf("%d", &n);
         i=2;
    
         while(i<=n)
         {
         j=2;
                        while(j<=i)
                        {
                                   if(i%j==0) 
                                   {
                                            t=i/j;
                                            printf("%d ",t);
                                   }
                                   j++;     
                        }              
         i++;
         }
         system("pause");
         return 0;
    }

    So far my vars are as:

    i j i%j i/j sum group by i
    2 2 0 1 1
    3 2 1
    3 3 0 1
    4 2 0 2 3
    4 3
    4 4 0 1
    5 2 1
    5 3
    5 4
    5 5 0 1
    6 2 0 3 6
    6 3 0 2
    6 4
    6 5
    6 6 0 1


    I have manage to display i/j, but I cant make it work for the last column, to add the result of the division where the mod is 0 grouped by i counter.

    Any help will be much apreciated.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So, is that the output you want, or the output you are getting? I can't see really why sometimes you have five numbers, sometimes four, sometimes three, and sometimes two.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    Hello,
    Sorry I forgot to mention:
    The output is when I run the program for n:6
    the final output should be:

    1


    3

    1



    1
    6

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'd use for loops here. I still don't know what it is you're trying to do, but I'd do it this way:
    Code:
    main()
    {
         int n,i,j,t;
         printf("Enter N:");
         scanf("%d", &n);
         i=2;
    
         for( j = 2; i <= n; j++ )
         {
              for( /* why are you only ever setting i once? */ ; j <= i; j++ )
              {
                   if(i%j==0) 
                   {
                        t=i/j;
                        printf("%d ",t);
                   }
              }              
         }
         system("pause");
         return 0;
    }
    You still probably want to be setting i to something in that inner loop. I guess. I don't know. You also may want to actually add a check there in that if to make sure j isn't zero.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    Printing perfect numbers

    Hello again I am trying to print the perfect numbers from 1 - 6
    6 is a perfect number:
    6/6+6/3+6/2=6 (do not divide with 6)
    28 is perfet: 1+2+4+7+17=28
    ......
    PS . this is not a a school asignment.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    What have you tried?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    1+2+4+7+17 = 31 by the way

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by anduril462 View Post
    Woah! Was I hallucinating when I wrote this or did somebody modify the Loop Help thread?

  10. #10
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    How did this happened ? I replied to my original thread :-S
    Maybe an admin splited my post ?

    Anyway

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Anyhow, on to business, the idea is that, for some number n, you run through all the numbers from 1 to n/2, and if n is divisible by that number, then add it to sum. Once you've reached the end of that loop, if the sum equals the number, then it's a perfect number. This should involve 1 for loop for any given n. Use the modulo (%) operator to test divisibility. Hope that gets you going in the right direction.

  12. #12
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    OK figured it out:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    main()
    {
         int n,i,j,t;
         printf("Enter N:");
         scanf("%d", &n);
         i=2;
    
         while(i<=n)
         {
         j=2;
         t=0;
                        while(j<=i)
                        {
                                   if(i%j==0) 
                                   {
                                            t=t+i/j;
                                            
                                   }
                                   j++;     
                        }
                        if(t==i)
                                {
                                printf("%d ",t);
                                }              
         i++;
         }
         system("pause");
         return 0;
    }
    It will print all perfect numbers from 1 to N :-)
    Thank you all people.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    *magically merges threads back together again*

    It would be easier to read if you wrote it as:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int n, i, j, t;
        printf("Enter N:");
        scanf("%d", &n);
    
        for (i = 2; i <= n; i++)
        {
            t = 0;
            for (j = 2; j <= i; j++)
            {
                if (i % j == 0)
                {
                    t += i / j;
                }
            }
    
            if (t == i)
            {
                printf("%d ", t);
            }
        }
    
        system("pause");
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Perfect Numbers
    By iLike in forum C++ Programming
    Replies: 2
    Last Post: 10-18-2009, 01:27 PM
  2. My perfect number assignment...
    By Ninestar in forum C Programming
    Replies: 8
    Last Post: 11-16-2005, 02:47 AM
  3. printing only prime numbers
    By Micko in forum C Programming
    Replies: 30
    Last Post: 06-10-2004, 11:23 PM
  4. Perfect Number Problem
    By TrazPFloyd in forum C++ Programming
    Replies: 21
    Last Post: 10-20-2002, 12:09 PM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM

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