# Printing perfect numbers

• 12-06-2010
DRBILOUKOS
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.
• 12-06-2010
tabstop
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.
• 12-06-2010
DRBILOUKOS
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
• 12-06-2010
quzah
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.
• 12-06-2010
DRBILOUKOS
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.
• 12-06-2010
laserlight
What have you tried?
• 12-06-2010
anduril462
• 12-06-2010
whiteflags
1+2+4+7+17 = 31 by the way
• 12-06-2010
anduril462
Quote:

Originally Posted by anduril462

Woah! Was I hallucinating when I wrote this or did somebody modify the Loop Help thread?
• 12-07-2010
DRBILOUKOS
How did this happened ? I replied to my original thread :-S
Maybe an admin splited my post ?

Anyway
• 12-07-2010
anduril462
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-07-2010
DRBILOUKOS
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.
• 12-07-2010
laserlight
*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; }```