Thread: Finding the highest prime factor of a number with a C program

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    10

    Finding the highest prime factor of a number with a C program

    I created a program which finds the highest prime factor of a number.
    But there is a problem in it. It is working fine till 9 digit numbers. After that it is showing weird numbers as answers.

    Code:
    #include<stdio.h>
    
    int largestprimefactor(unsigned long a)
    {
        int i =2 ,largeprimefactor = 2;
        
        while(a!=1)
        {
            if(a%i==0)
            {
                while(a%i==0)
                {
                    a = a/i;
                    
                    printf("%d ",i);
                    
                    if(i>largeprimefactor)
                    {
                        largeprimefactor = i;
                    }
                }
            } 
            
            i++;
        }
        
        return largeprimefactor;
    }
    
    main()
    {
        unsigned long inputnumber;
        
        printf("Enter a number : ");
        scanf("%d",&inputnumber);
        
        printf("\nThe largest prime factor of %d is %d",inputnumber,largestprimefactor(inputnumber));
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,428
    9 digits is about all you can store in an unsigned long number.
    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.

  3. #3
    Registered User
    Join Date
    May 2018
    Posts
    10
    Then which data type I should use? Should I use long keyword 2 times?
    Last edited by Zeeking99; 05-27-2018 at 11:11 PM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,166
    What is the range of numbers that you wish to handle?

    If they are within the range of an unsigned long long (guaranteed to have a maximum at least equal to the maximum for a 64 bit unsigned integer in the range starting from 0), and if your compiler supports that, you could use that instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,428
    Be careful if you're on windows, even if you're using code::blocks or MinGW as your compiler.
    Size Specification
    Depending on how old your C runtime library is on your machine, you may need to use alternative format specifiers to print 64-bit integers.
    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.

  6. #6
    Registered User
    Join Date
    May 2018
    Posts
    10
    Okay.

  7. #7
    Registered User
    Join Date
    May 2018
    Posts
    10
    I want to use numbers from 0 to 9 digit numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding The Biggest Prime Factor Of a Number
    By thedardwhie in forum C Programming
    Replies: 9
    Last Post: 02-11-2015, 12:58 PM
  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. C program for finding highest common factor!!
    By visham in forum C Programming
    Replies: 11
    Last Post: 08-02-2007, 07:10 AM
  5. Replies: 3
    Last Post: 03-29-2005, 04:24 PM

Tags for this Thread