# Thread: Perfect number and divisors

1. ## Perfect number and divisors

Hi,

I have to write a program that displays the nature of a number (perfect, defficient, or abundant) and DISPLAY ITS DIVISORS. and this is actually the bad part (lol). i have to use a while loop. i'll post it here what i've written so far. but it always displays a huge sam enumber as a divisor.
Could anybody correct me?? It's due on monday.
Thanks a lot.

Code:
``` #include <stdio.h>
int main (void) {
int n, cnt, sum_div;
char ans;
do{
printf("-------------------\n");
printf("Enter a number:\n");
scanf("%d", &n);
while((n%cnt==0) && (cnt<=n/2));{
printf("Divisors: %d,\n", cnt);
sum_div+=cnt;
if(sum_div==n)
printf("---%d is Perfect\n", n);
else if(sum_div>n)
printf("---%d is Abundant\n", n);
else
printf("---%d is Defficient\n", n);
}
printf("Continue? <Y/N>:\n");
scanf(" %c", &ans);
while(ans=='Y'||ans=='y')
}
printf("Bye:>_\n");
return(0);
}```

2. And why do you think cnt isn't 123409857 when you start the program? What else is it supposed to be?

3. My help is due Monday or your Homework assignment is due on Monday?

4. Homework is due MOnday. actually i assigned 1 to cnt( counter)
cnt=1 before the while, i just forgot to write it

5. Just copy and paste next time, por favor -_-. And lets go ahead and make this time no exception.

6. You need to make cnt do something.
sum_div needs to be initialized to zero.
The "Continue? <Y/N>" loop needs some help.

7. actually the continue look includes all the program, as the user has to chose whether to conitue or end there.
i now assigned 1 to cnt and 0 to sum_div.
is there any pblm with the braces?

8. *continue loop

9. Can you copy the code that you can compile it and paste it, please. Tabs and [code][/code] tags are prefered by most of us here.

10. ok here's the final thing. but the display of divisors is wrong.
Code:
```#include <stdio.h>
int main (void) {
int n, cnt, sum_div;
char ans;
do{
printf("-------------------\n");
printf("Enter a number:");
scanf("&#37;d", &n);
cnt=2;
sum_div=0;
while(n%cnt==0&&cnt<=n/2){
printf("Divisors: %d,\n", cnt);
sum_div+=cnt;
if(sum_div==n)
printf("---%d is Perfect\n", n);
else if(sum_div>n)
printf("---%d is Abundant\n", n);
else
printf("---%d is Defficient\n", n);
}
printf("Continue? <Y/N>:\n");
scanf(" %c", &ans);
while(ans=='Y'||ans=='y')
printf("Bye:>_\n");
return(0);
}```

11. Code:
```#include <stdio.h>
#include <math.h>
#include <ctype.h>

int main (void) {
float n, cnt, sum_div;
char ans;
do{
printf("-------------------\n");
printf("Enter a number:");
scanf("&#37;f", &n);

for(cnt = 2.0f, sum_div=0.0f; (fmod(n,cnt)==0)&&cnt<=n/2.0)
{
printf("Divisors: %f,\n", cnt);
sum_div+=cnt;

if(sum_div==n)
printf("---%f is Perfect\n", n);
else if(sum_div>n)
printf("---%f is Abundant\n", n);
else
printf("---%f is Defficient\n", n);
}
printf("Continue? <Y/N>:\n");
scanf(" %c", &ans);

} while(toupper(ans)=='Y');
printf("Bye:>_\n");
return(0);
}```
I changed some of the code around so we can read it. And now is it at least compileable?

12. that looks better. im using C free as a compiler and it's saying sth about C++ and ANSI and stuff like that. plus we haven't rlly done the for loop yet, and im suppose to do it with the while one.
well actually divisors have to be displayed like this for instance:
Divisors: 2, 3, 5, 6
and i just dont know how to have them all in one line, with the spaces.

13. Because I am a nice guy and because you are being less frustrating than the guy who I have posted the exact answer to his problem twice with no attempt to heed my words:

Example:
Code:
```#include <stdio.h>
#include <math.h>
#include <ctype.h>

int main (void) {
float n, cnt, sum_div;
char ans;
do{
printf("-------------------\n");
printf("Enter a number:");
scanf("&#37;f", &n);

cnt = 2.0f;
sum_div = 0.0f;

printf("Divisors");
ans = ':'; // Just trust me on this one. Your teacher may think you are a genious.

while((fmod(n,cnt)==0)&&cnt<=n/2.0)
{
printf("%c %f", ans, cnt);
sum_div+=cnt;
ans = ',';

if(sum_div==n)
printf("(Perfect)");
else if(sum_div>n)
printf("(Abundant)");
else
printf("(Defficient)");
}

printf("\nContinue? <Y/N>:\n");
scanf(" %c", &ans);

} while(toupper(ans)=='Y');
printf("Bye:>_\n");
return(0);
}```
P.S. The for loop was for my benefit, feel free to change it back to a while loop.

14. To be honest, I would ditch

Code:
```      if(sum_div==n)
printf("(Perfect)");
else if(sum_div>n)
printf("(Abundant)");
else
printf("(Defficient)");```
^ That code.

15. lol i know ur being veeery patient with me. im sorry im just a beginner :d:d
well i tried to compile ur code, it gave me a scary .exe, it's still running now btw while im writing u this. keeps writing 2.00000000000000000 abundant.