int isamicable(int nNum)
{
int a, j, nSum=0, nSum2=0;
int nMax=nNum/2;
for( j=1; j%26lt;=nMax ; j++) {
if(nNum%j==0)
nSum+=j; }
for( a=1; a%26lt;=(nSum/2) ; a++) {
if(nSum%a==0)
nSum2+=a; }
if(nSum2==nSum){
printf("\n%d is perfect", nNum); }
else if(nSum2==nNum)
printf("\n%d is amicable with %d", nNum, nSum);
else
return 0;
}
int isprime(int nNum)
{
int nMax=nNum/2;
int j,a=0;
for(j=1; j%26lt;=nMax ;j++){
if(nNum%j==0)
{ a+=1; } }
if((a==2)||(a==1))
printf("\n%d number is prime", nNum);
else
return 0;
}
int isperfect(int nNum)
{
int nSum=0;
int nMax=nNum/2;
int j=2;
do {
if(nNum%j==0)
nSum+=j;
j++;
}while(j%26lt;=nMax);
if(nSum==nNum)
printf("\n%d is perfect", nNum);
else
return 0;
}
The main function asks input from the user and calls the three function. The problem is that when the program runs and I type '6' it displays" is perfect" two times instead of one.The same thing happens when i enter amicable number and prime number. PLS HELP
HELP with a C PROGRAM?
That's because you print it in two different functions: isamicable() and isperfect() at the lines:
if(nSum2==nSum){
printf("\n%d is perfect", nNum); }
and
if(nSum==nNum)
printf("\n%d is perfect", nNum);
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment