I was solving a puzzle, where im required to find the largest Prime Factor of a composite number entered by the user. I thought of something and have tried it out, but it doesn't manage to detect the largest prime factor amongst the factors of the composite number.
I'm appending my code below, I'd be grateful if anyone could help me out here to get to detect the largest prime no. amongst the factors and print it.
// Accept a composite number from user and print its largest prime factor.
#include<stdio.h>
void main()
{
int i,j,b=2,c;
printf("\nEnter a composite number: ");
scanf("%d", &c);
printf("Factors: ");
for(i=1; i<=c/2; i++)
{
if(c%i==0)
{
printf("%d ", i);
for(j=2; j<=i/2; j++) //since a numbr cand be divisible by a number greated than its half
{ if(i%j > 0)
b = i;
else if(i==3)
b = 3;
}
}
}
printf("%d\nLargest prime factor: %d\n", c, b);
}