Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote a code to which takes a number as input and displays the largest prime factor of it. The program is working fine till 9 digit numbers. After 9 digit numbers it is behaving weirdly showing negative numbers.

#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));
}


What I have tried:

I thought it might be a data type problem so I tried using unsigned long long but it doesn't help.
Posted
Updated 27-May-18 22:13pm

While inputnumber is a long, your scanf still only expects an integer. You can accept an unsigned long like so:
unsigned long inputnumber;
scanf("%lu",&inputnumber);

Or an unsigned long long:
unsigned long long inputnumber;
scanf("%llu",&inputnumber);

For reference of the formats, see scanf - C++ Reference[^]

When showing the result, you should modify the printf format accordingly.
 
Share this answer
 
v2
Comments
Zeeking99 28-May-18 8:36am    
Thanks it works.
Quote:
Finding the largest prime factor of a number using a C program

If you input 1, your program will say that the largest prime factor is 2. I suspect an error.
You also need to handle the result for 0 and negative numbers to be complete.

You can simplify your code
C++
while(a!=1)
{
    if(a%i==0) // you can remove the test because
    {
        while(a%i==0) // the condition is the same
        {
            a = a/i;

            printf("%d ",i);

            if(i>largeprimefactor)
            {
                largeprimefactor = i;
            }
        }
    }

    i++;
}

[Update]
You can even simplify more.
C++
while(a!=1)
{
    if(a%i==0) // you can remove the test because
    {
        while(a%i==0) // the condition is the same
        {
            a = a/i;

            printf("%d ",i);

            if(i>largeprimefactor)  // because i is never lower than largeprimefactor
            {
                largeprimefactor = i;
            }
        }
    }

    i++;
}
 
Share this answer
 
v3
Comments
nv3 28-May-18 5:44am    
I doubt that will work if you leave the i++ out of the loop :-)
Patrice T 28-May-18 5:55am    
Read the code more carefully. :)
nv3 28-May-18 6:07am    
Got it. Sorry.
Patrice T 28-May-18 6:09am    
You're welcome :)
Zeeking99 28-May-18 8:34am    
Sometimes i is less than largeprimefactor.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900