3
I need to create a program in C, which factors in any number the user enters. I wrote this code, but it doesn’t work completely because it only calculates once. I don’t want an answer ready, I want an explanation, why that happens.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num, i, j, cont=0, resp;
printf("digite um numero inteiro positivo:\n");
scanf("%i", &num);
for(i=1;i<10000;i++)
{
for(j=1;j<10000;j++)
{
if(i%j==0)
cont++;
if(cont==2)
{
resp=num/i;
printf("%i |%i=%i", num, i, resp);
}
}
if(resp==1)
break;
}
return 0;
}
very interesting even its method it is even smaller than mine in a line, I could show it but I can’t remember where it’s XD neither on PC nor git =P, but thanks @Tiago Cavalca
– Leonardo V. De Gasperin
This program does not provide prime factors, which is usually intended.
– pmg