My perfect number assignment...
Hey, I've had to create a program that figures out the upper limits of perfect numbers, so here is my program.
Code:
#include <stdio.h>
#include <stdlib.h>
int perfect(int x);
int main(char *argv[],int argc)
{
int x=0;
int y=0;
int z;
int a;
printf("Enter a number up to 10000 to discover the perfect numbers within the limit:\n\n");
scanf("%d", &y);
printf("\n");
/* The above code asks the user to input a limit and the assigns this number to the variable, "y" */
for (z=1;z<=y;z++)
/* This is a loop which runs the function "perfect" for each number in turn, up to the number inputted by the user */
{
a = perfect(z);
if(a==1)
{
a==1;
printf("%d is a perfect number\n\n",z);
/* The program then prints each perfect number up to this limit */
}
}
system("PAUSE");
return 0;
}
int perfect(int x)
{
int y = 0;
int m = 0;
int p;
for(p=1;x>p;p++)
/* This will loop whist "x" is more than "p" */
{
m=x%p;
/* This piece of code uses the 'MOD' calculation to determine if there's a remainder */
if(m==0)
/* "p" is added to "y" if there is no remainder */
{
y=y+p;
}
}
if(y==x)
/* "x" is perfect number if it is equal to "y" */
{
x=1;
}
else
{
x=0;
}
return x;
}
Now, I used "int main(char *argv[],int argc)" as I read in the help file about the debugging values, but we have been taught to use void main(void) and so I feel like I'm somewhat cheating. Does anyone know how to improve this?
For the second part of my assignment I have to modify this program so that the program asks the user to input a number and the program should print the nearest perfect number on the screen, in short I haven't a clue where to start, help would be appreciated, thanks.