what's the problem with this?

August 16, 2011 at 21:46:13
Specs: Windows XP
#include<stdio.h>
#include<conio.h>

void main(void)
{
int x, a ,b;
clrscr();

printf("Enter a positive ineteger: "); scanf("%d", &x);
printf("The factors are: \n");

if(x<= 1)
{
return;
}

b = 0;
a = 1;
while(x % a == 0)
{
b++;
printf("\t %i", a);
}
a++;

if(b<=3)
{
printf("\n %d is prime", x);
}
else
{
printf("\n %d is composite", x);
}
getch();
return;
}


See More: whats the problem with this?

Report •


#1
August 16, 2011 at 22:13:29
I have some background in C++, very little, so I hope I'm not making a fool out of myself.

while(x % a == 0)
{
b++;
printf("\t %i", a);
}

This while loop seems to be an infinite loop, because only b is being incremented, nothing happens to the variable a, and since a = 1, any int modulo 1 will be 0, so it never gets out of the loop. I'm thinking that you meant x % b == 0 as the test condition for the while loop?


Report •
Related Solutions


Ask Question