# Thread: Finding The Biggest Prime Factor Of a Number

1. ## Finding The Biggest Prime Factor Of a Number

Hello i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five

This is my Code please tell me what is my program bug
Code:
```#include <stdio.h>              // main functions
#include <math.h>               // for sqrt function
int main()
{
int i, j, k, f;               // F = Flag;

printf("Enter K\n\n");        // The Number which we want to scan
scanf("%d", &k);              // Scanning Number
for (i = 2; i < k; i++)       // loop for factors
{

if (k % i == 0) {
for (j = 1; j < sqrt(i); j++) //prime loop
if (i % j == 0) {
printf("%d\n\n", i);
}

}

}

return 0;                     //Check if the program has work correctly & Finish
}```

2. Thanks All Boys . . .

3. 1. Cutting and pasting error
2. Laziness based on your lack of effort to fix the posted code.
Announcements - General Programming Boards

Tim S.

4. > This is my Code please tell me what is my program bug
Based on what is posted, I'd say it's on line 1.

6. i am not a english speaker iam just a iranian person i mean what should i do to make my program complete and it give me the true answer

7. Originally Posted by thedardwhie
what should i do to make my program complete and it give me the true answer
I don't like handing out complete solutions, but in your case, I'll make an exception.

Code:
```#include <stdio.h>

int main()
{
printf("42\n");
return 0;
}```

8. i mean what should i do to make my program complete and it give me the true answer
O_o

Care to try, for example, posting formatted code?

Soma

9. > int i, j, k, f; // F = Flag;
I suggest you change the code to be this then.
int i, j, k, flag;

> printf("%d\n\n", i);
If your intent is to find the biggest, shouldn't you be doing something like checking all the factors, then printing out the biggest one found?

An idea might be to run your inner loop in reverse, then print the first one (hence the largest).