# Perfect number and divisors

Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last
• 10-17-2008
Lissa
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);
}

• 10-17-2008
tabstop
And why do you think cnt isn't 123409857 when you start the program? What else is it supposed to be?
• 10-17-2008
master5001
My help is due Monday or your Homework assignment is due on Monday?
• 10-17-2008
Lissa
Homework is due MOnday. actually i assigned 1 to cnt( counter)
cnt=1 before the while, i just forgot to write it
• 10-17-2008
master5001
Just copy and paste next time, por favor -_-. And lets go ahead and make this time no exception.
• 10-17-2008
nonoob
You need to make cnt do something.
sum_div needs to be initialized to zero.
The "Continue? <Y/N>" loop needs some help.
• 10-17-2008
Lissa
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?
• 10-17-2008
Lissa
*continue loop
• 10-17-2008
master5001
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-17-2008
Lissa
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);
}

• 10-17-2008
master5001
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?
• 10-17-2008
Lissa
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.
• 10-17-2008
master5001
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.
• 10-17-2008
master5001
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.
• 10-17-2008
Lissa
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. :(:(:(
Show 80 post(s) from this thread on one page
Page 1 of 3 123 Last