Thread: Finding The Biggest Prime Factor Of a Number

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    23

    Question 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
    }
    Last edited by Salem; 02-11-2015 at 12:53 PM. Reason: sympathy edit

  2. #2
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    Thanks All Boys . . .

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    1. Cutting and pasting error
    2. Laziness based on your lack of effort to fix the posted code.
    Announcements - General Programming Boards

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    please help me and tell me more about my program problem

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    Jan 2015
    Posts
    23
    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

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by thedardwhie View Post
    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;
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    i mean what should i do to make my program complete and it give me the true answer
    O_o

    If you'd followed any of the advice you've been given in response to your shotgun approach to requesting help, you would actually be getting help instead being teased about your ridiculous threads.

    Care to try, for example, posting formatted code?

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. biggest prime factor
    By robinnbastar in forum C Programming
    Replies: 28
    Last Post: 08-13-2012, 09:36 AM
  2. help with prime factor for 12 digit number
    By nick2 in forum C Programming
    Replies: 15
    Last Post: 06-19-2009, 04:39 AM
  3. Calculating prime factor of a huge number.
    By Bakster in forum C Programming
    Replies: 15
    Last Post: 02-20-2009, 12:06 PM
  4. Finding the biggest number?
    By Tarento in forum C Programming
    Replies: 2
    Last Post: 05-17-2006, 09:18 PM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM

Tags for this Thread