Finding largest prime factor of a composite number in c

Posted in dotnet, vb, vb.net | No Comments »

I am accepting a composite number as an input. I want to print all its factors and also the largest prime factor of that number. I have written the following code. It is working perfectly ok till the number 51. But if any number greater than 51 is inputted, wrong output is shown. how can I correct my code?

#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=1; j<=i; j++)
   {
    if(i%j > 0)
    {
     b = i;
    }
    if(b%3==0)
     b = 3;
    else if(b%2==0)
     b = 2;
    else if(b%5==0)
     b = 5;
   }
  }
 }
 printf("%dnLargest prime factor: %dn", c, b);
}

Similar:

  1. recursive func to find prime factors i made a recursive function to find the prime factors of a number but it has a bug which makes turbo c quit. please help...
  2. Scanf problem { DWORD dw; int i; char ch; printf(“Enter first value=”); scanf(“%x”,&dw); printf(“nEnter second value=”); scanf(“%d”,&i); printf(“nEnter single character=”); //control not stop here…… scanf(“%c”,&ch); } Here...
  3. CodeGolf: Work out the prime factors of a given number Inspired by http://codegolf.com/prime-factors, try to solve this problem using the least number of keystrokes. Sadly, you can only compete with Perl/PHP/Python/Ruby and I would love...
  4. User defined array sizes in C I’m reading through “Illustrated C” and the first exercise question asks: Program MATMUL multiplies matrices of fixed size. Make the program deal with any specified...
  5. Is this mingw bug ? Hi, I have been trying to execute this program on my migw ,through code::blocks, #include <string.h> #include <math.h> #include <stdio.h> #define N 100 int p[N...

Leave a Reply