recursive func to find prime factors












0















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



#include<stdio.h>
#include<conio.h>
int prime(int num);
int primefactor(int num,int i);
void main(void)
{
int num;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
primefactor(num,i);
i=num
getch();
}
int primefactor(int num,int i)
{
if(i==2)
return 1;
if(num%i==0)
{
if(prime(num))
{
printf(",%d",num);
num=num/i;
i++;
}


}
i--;
primefactor(num,i);
return 0;
}
int prime(int num)
{
int i,flag;
for(i=2;i<num;i++)
{
if(num%i==0)
flag=0;
}
return flag;
}









share|improve this question




















  • 7





    Turbo C? As in, the 1980s-era C compiler?

    – James McNellis
    Jul 10 '10 at 23:02











  • On what line is the compiler reporting problems and/or crashing? You have given no information which might be useful for people to help resolve your problems.

    – James McNellis
    Jul 10 '10 at 23:03








  • 1





    borland turbo c 3 something ..i dont get why i am being taught it in the university :(

    – Fahad Uddin
    Jul 10 '10 at 23:10











  • You need to set flag = 1 in prime, and return it at the end. Or, better, when you find a factor, return 0; if you drop off the end of the loop, return 1. Note that you really only need to go as far as the square root of num to look for factors. This doesn't matter much when you've only fewer than 10 digits in the number, but it really does matter if you have many more digits.

    – Jonathan Leffler
    Jul 10 '10 at 23:41






  • 1





    @James: Turbo C was very much in action in academic world till at least 2003 :)

    – neal aise
    Jul 10 '10 at 23:58
















0















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



#include<stdio.h>
#include<conio.h>
int prime(int num);
int primefactor(int num,int i);
void main(void)
{
int num;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
primefactor(num,i);
i=num
getch();
}
int primefactor(int num,int i)
{
if(i==2)
return 1;
if(num%i==0)
{
if(prime(num))
{
printf(",%d",num);
num=num/i;
i++;
}


}
i--;
primefactor(num,i);
return 0;
}
int prime(int num)
{
int i,flag;
for(i=2;i<num;i++)
{
if(num%i==0)
flag=0;
}
return flag;
}









share|improve this question




















  • 7





    Turbo C? As in, the 1980s-era C compiler?

    – James McNellis
    Jul 10 '10 at 23:02











  • On what line is the compiler reporting problems and/or crashing? You have given no information which might be useful for people to help resolve your problems.

    – James McNellis
    Jul 10 '10 at 23:03








  • 1





    borland turbo c 3 something ..i dont get why i am being taught it in the university :(

    – Fahad Uddin
    Jul 10 '10 at 23:10











  • You need to set flag = 1 in prime, and return it at the end. Or, better, when you find a factor, return 0; if you drop off the end of the loop, return 1. Note that you really only need to go as far as the square root of num to look for factors. This doesn't matter much when you've only fewer than 10 digits in the number, but it really does matter if you have many more digits.

    – Jonathan Leffler
    Jul 10 '10 at 23:41






  • 1





    @James: Turbo C was very much in action in academic world till at least 2003 :)

    – neal aise
    Jul 10 '10 at 23:58














0












0








0








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



#include<stdio.h>
#include<conio.h>
int prime(int num);
int primefactor(int num,int i);
void main(void)
{
int num;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
primefactor(num,i);
i=num
getch();
}
int primefactor(int num,int i)
{
if(i==2)
return 1;
if(num%i==0)
{
if(prime(num))
{
printf(",%d",num);
num=num/i;
i++;
}


}
i--;
primefactor(num,i);
return 0;
}
int prime(int num)
{
int i,flag;
for(i=2;i<num;i++)
{
if(num%i==0)
flag=0;
}
return flag;
}









share|improve this question
















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



#include<stdio.h>
#include<conio.h>
int prime(int num);
int primefactor(int num,int i);
void main(void)
{
int num;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
primefactor(num,i);
i=num
getch();
}
int primefactor(int num,int i)
{
if(i==2)
return 1;
if(num%i==0)
{
if(prime(num))
{
printf(",%d",num);
num=num/i;
i++;
}


}
i--;
primefactor(num,i);
return 0;
}
int prime(int num)
{
int i,flag;
for(i=2;i<num;i++)
{
if(num%i==0)
flag=0;
}
return flag;
}






c recursion






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 '13 at 11:16









Ren

1,05941524




1,05941524










asked Jul 10 '10 at 22:59









Fahad UddinFahad Uddin

7,08653143233




7,08653143233








  • 7





    Turbo C? As in, the 1980s-era C compiler?

    – James McNellis
    Jul 10 '10 at 23:02











  • On what line is the compiler reporting problems and/or crashing? You have given no information which might be useful for people to help resolve your problems.

    – James McNellis
    Jul 10 '10 at 23:03








  • 1





    borland turbo c 3 something ..i dont get why i am being taught it in the university :(

    – Fahad Uddin
    Jul 10 '10 at 23:10











  • You need to set flag = 1 in prime, and return it at the end. Or, better, when you find a factor, return 0; if you drop off the end of the loop, return 1. Note that you really only need to go as far as the square root of num to look for factors. This doesn't matter much when you've only fewer than 10 digits in the number, but it really does matter if you have many more digits.

    – Jonathan Leffler
    Jul 10 '10 at 23:41






  • 1





    @James: Turbo C was very much in action in academic world till at least 2003 :)

    – neal aise
    Jul 10 '10 at 23:58














  • 7





    Turbo C? As in, the 1980s-era C compiler?

    – James McNellis
    Jul 10 '10 at 23:02











  • On what line is the compiler reporting problems and/or crashing? You have given no information which might be useful for people to help resolve your problems.

    – James McNellis
    Jul 10 '10 at 23:03








  • 1





    borland turbo c 3 something ..i dont get why i am being taught it in the university :(

    – Fahad Uddin
    Jul 10 '10 at 23:10











  • You need to set flag = 1 in prime, and return it at the end. Or, better, when you find a factor, return 0; if you drop off the end of the loop, return 1. Note that you really only need to go as far as the square root of num to look for factors. This doesn't matter much when you've only fewer than 10 digits in the number, but it really does matter if you have many more digits.

    – Jonathan Leffler
    Jul 10 '10 at 23:41






  • 1





    @James: Turbo C was very much in action in academic world till at least 2003 :)

    – neal aise
    Jul 10 '10 at 23:58








7




7





Turbo C? As in, the 1980s-era C compiler?

– James McNellis
Jul 10 '10 at 23:02





Turbo C? As in, the 1980s-era C compiler?

– James McNellis
Jul 10 '10 at 23:02













On what line is the compiler reporting problems and/or crashing? You have given no information which might be useful for people to help resolve your problems.

– James McNellis
Jul 10 '10 at 23:03







On what line is the compiler reporting problems and/or crashing? You have given no information which might be useful for people to help resolve your problems.

– James McNellis
Jul 10 '10 at 23:03






1




1





borland turbo c 3 something ..i dont get why i am being taught it in the university :(

– Fahad Uddin
Jul 10 '10 at 23:10





borland turbo c 3 something ..i dont get why i am being taught it in the university :(

– Fahad Uddin
Jul 10 '10 at 23:10













You need to set flag = 1 in prime, and return it at the end. Or, better, when you find a factor, return 0; if you drop off the end of the loop, return 1. Note that you really only need to go as far as the square root of num to look for factors. This doesn't matter much when you've only fewer than 10 digits in the number, but it really does matter if you have many more digits.

– Jonathan Leffler
Jul 10 '10 at 23:41





You need to set flag = 1 in prime, and return it at the end. Or, better, when you find a factor, return 0; if you drop off the end of the loop, return 1. Note that you really only need to go as far as the square root of num to look for factors. This doesn't matter much when you've only fewer than 10 digits in the number, but it really does matter if you have many more digits.

– Jonathan Leffler
Jul 10 '10 at 23:41




1




1





@James: Turbo C was very much in action in academic world till at least 2003 :)

– neal aise
Jul 10 '10 at 23:58





@James: Turbo C was very much in action in academic world till at least 2003 :)

– neal aise
Jul 10 '10 at 23:58












10 Answers
10






active

oldest

votes


















1














(little too sleepy to write good code.. so am sorry in advance for any bugs :p )



a simpler non recursive version



printPrimeFactors(int num) {

for (i = 2; i < sqrt(num); i=getNextPrime()) {
if (num %i)
printf("%d", i);
}

}


if you have to use recursion



printPrimeFactors(int num) {

if(isPrime(num)) {
printf ("%d ", num);
} else {
for(i=2; i < sqrt(num); i++) {
if(num%i ==0) {
printPrimeFactors(i);
printPrimeFactors(num/i);
}
}
}

}





share|improve this answer
























  • Can any1 explain that :(

    – Fahad Uddin
    Jul 14 '10 at 12:48











  • will put in some explanation soon. sorry about that

    – neal aise
    Jul 15 '10 at 10:26






  • 1





    are you still sleepy? or you should have corrected this code by now

    – SouvikMaji
    Apr 18 '15 at 5:57



















5














void main(void)
{
int num,i=num; // (*)
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
primefactor(num,i);
getch();
}


What value do you think i will have in (*)?



Not sure what you want i to start out as, but I'm pretty sure you don't want it to be something random. If you want it to start with the value of num, you need to assign num to it after you read it:



void main(void)
{
int num,i;
printf("Enter a number whose prime factors are to be calculated:");
scanf("%d",&num);
i = num; // assignment goes here.
primefactor(num,i);
getch();
}





share|improve this answer
























  • oh lol so stupid of me :P thanks a lot :)

    – Fahad Uddin
    Jul 10 '10 at 23:11











  • void main? =(

    – jamesdlin
    Jul 11 '10 at 0:24











  • i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better

    – Fahad Uddin
    Jul 14 '10 at 12:45











  • @fahad it has to be int main(void) in your case (the other case is when you have command line params) educate your teachers with this: en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

    – neal aise
    Jul 15 '10 at 10:25



















1














Full recursive solution in c++ (for c replace cout lines with printf):



void printPrimeFactors(int num)
{
static int divisor = 2; // 2 is the first prime number

if ( num == 1 ) //if num = 1 we finished
{
divisor = 2; //restore divisor, so it'll be ready for the next run
return;
}
else if ( num % divisor == 0 ) //if num divided by divisor
{
cout << divisor << " "; //print divisor
printPrimeFactors( num / divisor ); //call the function with num/divisor
}
else //if num not divided by divisor
{
divisor++; //increase divisor
printPrimeFactors( num );
}
}





share|improve this answer
























  • Why is the static a must in this case?

    – shinzou
    Dec 10 '14 at 19:48



















1














The best way to implement prime factorization with low overhead function calls would be . . .



void factors(int number)
{
int divisor = 2;
if (number == 1) { cout << "1"; return; }
while ((number % divisor) && (number > divisor)) divisor++;
cout << divisor << ", ";
factors(number / divisor);
}


The number of function calls (recursion) is equal to the number of prime factors, including 1.






share|improve this answer































    1














    I did this in C. Depending on the compiler, minor changes might be needed to make in the program.



    #include<stdio.h>
    int primefact(int);
    int main()
    {
    int n;
    printf("Enter a number whose prime factors are to be calculated : n");
    scanf_s("%d", &n);
    printf("Prime factors of %d are : ");
    primefact(n);
    printf("n");
    return 0;
    }
    int primefact(int n)
    {
    int i=2;
    while(n%i!=0)
    i++;
    printf("%d ", i);
    if(n==i)
    return 0;
    else
    primefact(n/i);
    }





    share|improve this answer

































      0














      Agree with IVlad - also, what happens in the case when num is prime? How many times will the recursive function be called for e.g. num = 7?






      share|improve this answer
























      • ...and - how does prime return it's value to the caller?

        – Will A
        Jul 10 '10 at 23:14











      • i used my concept of prime factor calculation and a prime factor program made without recursion for the help. pastebin.com/fVbjFGzQ

        – Fahad Uddin
        Jul 10 '10 at 23:19











      • I don't see a return statement anywhere in your prime function. Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...

        – Will A
        Jul 10 '10 at 23:21



















      0














      #include<stdio.h>
      #include<stdlib.h>

      int ar[10]={0};
      int i=0,j=2;

      void P(int n)
      {
      if(n<=1){
      return ;
      }

      else{
      if(n%j == 0){
      printf("%dt",j);
      n=n/j;

      }
      else{
      j++;
      }
      P(n);
      }
      }

      int main(void)
      {
      int n;
      printf("Enter n = ");
      scanf("%d",&n);
      P(n);
      printf("n");
      return 0;
      }





      share|improve this answer
























      • I suggest you don't include the corrected code, just an explanation of the error in question and the general direction on how to fix it.

        – Theocharis K.
        Jul 4 '14 at 18:10













      • There's no need to add an answer to an already properly answered question. And as @TheocharisK. said, please add also an explanation to your answer next time. And btw... Welcome to SO.

        – Pr0gr4mm3r
        Jul 4 '14 at 18:13



















      0














      // recursivePrime.cpp
      // Purpose: factor finding for an integer
      // Author: Ping-Sung Liao, Kaohsiung,TAIWAN
      // Date: 2017/02/02
      // Version : 1.0
      // Reference:
      // http://stackoverflow.com/questions/3221156/recursive-func-to-find-prime-factors

      #include<stdio.h>
      #include<stdlib.h>
      #include<math.h>


      int primefactor(int num,int i);
      int main(void)
      {
      int num, i;
      printf("Enter a number whose prime factors are to be calculated:");
      scanf("%d",&num);
      i=int ( sqrt (num) );
      primefactor(num,i);

      system("pause"); // instead of getch()
      }

      int primefactor(int num,int i)
      { printf("num %d i=%dn", num, i);
      if (i==1)
      printf("prime found= %dn", num); // prime appearing in he variuable num

      else if(num%i==0)
      { primefactor( int (num/i) , int ( sqrt(num/i) ) );
      primefactor( i , int (sqrt ( i ) ) );
      }
      else
      { i--;
      primefactor(num,i);
      }
      return 0;
      }





      share|improve this answer





















      • 1





        While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

        – Donald Duck
        Feb 2 '17 at 14:43



















      0














      #include  <`stdio.h`>

      void pf(int,int);

      int main()

      {

      int a,i=2;

      printf("Enter the Number:n");
      scanf("%d",&a);

      pf(a,i);
      }

      void pf(int x,int y)

      {

      if(x==1)

      return 1;

      else
      {
      if(x%y==0)
      {printf("%dt",y);
      pf(x/y,y);
      }

      else
      {
      y++;
      pf(x,y);
      }
      }
      }





      share|improve this answer

































        -1














        Implementation in java..



        public class PrimeFactor {

        public int divisor=2;
        void printPrimeFactors(int num)
        {

        if(num == 1)
        return;

        if(num%divisor!=0)
        {
        while(num%divisor!=0)
        ++divisor;
        }
        if(num%divisor==0){

        System.out.println(divisor);
        printPrimeFactors(num/divisor);
        }

        }
        public static void main(String args)
        {
        PrimeFactor obj = new PrimeFactor();
        obj.printPrimeFactors(90);
        }

        }





        share|improve this answer
























        • Beyond attempting to solve the original question, this isn't relevant to the Turbo C issue raised in the original question.

          – Sam Storie
          May 11 '15 at 20:40











        • A quick explanation as to why this is the right way to do it would be beneficial.

          – David
          May 11 '15 at 20:53











        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3221156%2frecursive-func-to-find-prime-factors%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        10 Answers
        10






        active

        oldest

        votes








        10 Answers
        10






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        1














        (little too sleepy to write good code.. so am sorry in advance for any bugs :p )



        a simpler non recursive version



        printPrimeFactors(int num) {

        for (i = 2; i < sqrt(num); i=getNextPrime()) {
        if (num %i)
        printf("%d", i);
        }

        }


        if you have to use recursion



        printPrimeFactors(int num) {

        if(isPrime(num)) {
        printf ("%d ", num);
        } else {
        for(i=2; i < sqrt(num); i++) {
        if(num%i ==0) {
        printPrimeFactors(i);
        printPrimeFactors(num/i);
        }
        }
        }

        }





        share|improve this answer
























        • Can any1 explain that :(

          – Fahad Uddin
          Jul 14 '10 at 12:48











        • will put in some explanation soon. sorry about that

          – neal aise
          Jul 15 '10 at 10:26






        • 1





          are you still sleepy? or you should have corrected this code by now

          – SouvikMaji
          Apr 18 '15 at 5:57
















        1














        (little too sleepy to write good code.. so am sorry in advance for any bugs :p )



        a simpler non recursive version



        printPrimeFactors(int num) {

        for (i = 2; i < sqrt(num); i=getNextPrime()) {
        if (num %i)
        printf("%d", i);
        }

        }


        if you have to use recursion



        printPrimeFactors(int num) {

        if(isPrime(num)) {
        printf ("%d ", num);
        } else {
        for(i=2; i < sqrt(num); i++) {
        if(num%i ==0) {
        printPrimeFactors(i);
        printPrimeFactors(num/i);
        }
        }
        }

        }





        share|improve this answer
























        • Can any1 explain that :(

          – Fahad Uddin
          Jul 14 '10 at 12:48











        • will put in some explanation soon. sorry about that

          – neal aise
          Jul 15 '10 at 10:26






        • 1





          are you still sleepy? or you should have corrected this code by now

          – SouvikMaji
          Apr 18 '15 at 5:57














        1












        1








        1







        (little too sleepy to write good code.. so am sorry in advance for any bugs :p )



        a simpler non recursive version



        printPrimeFactors(int num) {

        for (i = 2; i < sqrt(num); i=getNextPrime()) {
        if (num %i)
        printf("%d", i);
        }

        }


        if you have to use recursion



        printPrimeFactors(int num) {

        if(isPrime(num)) {
        printf ("%d ", num);
        } else {
        for(i=2; i < sqrt(num); i++) {
        if(num%i ==0) {
        printPrimeFactors(i);
        printPrimeFactors(num/i);
        }
        }
        }

        }





        share|improve this answer













        (little too sleepy to write good code.. so am sorry in advance for any bugs :p )



        a simpler non recursive version



        printPrimeFactors(int num) {

        for (i = 2; i < sqrt(num); i=getNextPrime()) {
        if (num %i)
        printf("%d", i);
        }

        }


        if you have to use recursion



        printPrimeFactors(int num) {

        if(isPrime(num)) {
        printf ("%d ", num);
        } else {
        for(i=2; i < sqrt(num); i++) {
        if(num%i ==0) {
        printPrimeFactors(i);
        printPrimeFactors(num/i);
        }
        }
        }

        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 10 '10 at 23:53









        neal aiseneal aise

        680412




        680412













        • Can any1 explain that :(

          – Fahad Uddin
          Jul 14 '10 at 12:48











        • will put in some explanation soon. sorry about that

          – neal aise
          Jul 15 '10 at 10:26






        • 1





          are you still sleepy? or you should have corrected this code by now

          – SouvikMaji
          Apr 18 '15 at 5:57



















        • Can any1 explain that :(

          – Fahad Uddin
          Jul 14 '10 at 12:48











        • will put in some explanation soon. sorry about that

          – neal aise
          Jul 15 '10 at 10:26






        • 1





          are you still sleepy? or you should have corrected this code by now

          – SouvikMaji
          Apr 18 '15 at 5:57

















        Can any1 explain that :(

        – Fahad Uddin
        Jul 14 '10 at 12:48





        Can any1 explain that :(

        – Fahad Uddin
        Jul 14 '10 at 12:48













        will put in some explanation soon. sorry about that

        – neal aise
        Jul 15 '10 at 10:26





        will put in some explanation soon. sorry about that

        – neal aise
        Jul 15 '10 at 10:26




        1




        1





        are you still sleepy? or you should have corrected this code by now

        – SouvikMaji
        Apr 18 '15 at 5:57





        are you still sleepy? or you should have corrected this code by now

        – SouvikMaji
        Apr 18 '15 at 5:57













        5














        void main(void)
        {
        int num,i=num; // (*)
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        primefactor(num,i);
        getch();
        }


        What value do you think i will have in (*)?



        Not sure what you want i to start out as, but I'm pretty sure you don't want it to be something random. If you want it to start with the value of num, you need to assign num to it after you read it:



        void main(void)
        {
        int num,i;
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        i = num; // assignment goes here.
        primefactor(num,i);
        getch();
        }





        share|improve this answer
























        • oh lol so stupid of me :P thanks a lot :)

          – Fahad Uddin
          Jul 10 '10 at 23:11











        • void main? =(

          – jamesdlin
          Jul 11 '10 at 0:24











        • i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better

          – Fahad Uddin
          Jul 14 '10 at 12:45











        • @fahad it has to be int main(void) in your case (the other case is when you have command line params) educate your teachers with this: en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

          – neal aise
          Jul 15 '10 at 10:25
















        5














        void main(void)
        {
        int num,i=num; // (*)
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        primefactor(num,i);
        getch();
        }


        What value do you think i will have in (*)?



        Not sure what you want i to start out as, but I'm pretty sure you don't want it to be something random. If you want it to start with the value of num, you need to assign num to it after you read it:



        void main(void)
        {
        int num,i;
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        i = num; // assignment goes here.
        primefactor(num,i);
        getch();
        }





        share|improve this answer
























        • oh lol so stupid of me :P thanks a lot :)

          – Fahad Uddin
          Jul 10 '10 at 23:11











        • void main? =(

          – jamesdlin
          Jul 11 '10 at 0:24











        • i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better

          – Fahad Uddin
          Jul 14 '10 at 12:45











        • @fahad it has to be int main(void) in your case (the other case is when you have command line params) educate your teachers with this: en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

          – neal aise
          Jul 15 '10 at 10:25














        5












        5








        5







        void main(void)
        {
        int num,i=num; // (*)
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        primefactor(num,i);
        getch();
        }


        What value do you think i will have in (*)?



        Not sure what you want i to start out as, but I'm pretty sure you don't want it to be something random. If you want it to start with the value of num, you need to assign num to it after you read it:



        void main(void)
        {
        int num,i;
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        i = num; // assignment goes here.
        primefactor(num,i);
        getch();
        }





        share|improve this answer













        void main(void)
        {
        int num,i=num; // (*)
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        primefactor(num,i);
        getch();
        }


        What value do you think i will have in (*)?



        Not sure what you want i to start out as, but I'm pretty sure you don't want it to be something random. If you want it to start with the value of num, you need to assign num to it after you read it:



        void main(void)
        {
        int num,i;
        printf("Enter a number whose prime factors are to be calculated:");
        scanf("%d",&num);
        i = num; // assignment goes here.
        primefactor(num,i);
        getch();
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 10 '10 at 23:07









        IVladIVlad

        37.3k1187164




        37.3k1187164













        • oh lol so stupid of me :P thanks a lot :)

          – Fahad Uddin
          Jul 10 '10 at 23:11











        • void main? =(

          – jamesdlin
          Jul 11 '10 at 0:24











        • i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better

          – Fahad Uddin
          Jul 14 '10 at 12:45











        • @fahad it has to be int main(void) in your case (the other case is when you have command line params) educate your teachers with this: en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

          – neal aise
          Jul 15 '10 at 10:25



















        • oh lol so stupid of me :P thanks a lot :)

          – Fahad Uddin
          Jul 10 '10 at 23:11











        • void main? =(

          – jamesdlin
          Jul 11 '10 at 0:24











        • i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better

          – Fahad Uddin
          Jul 14 '10 at 12:45











        • @fahad it has to be int main(void) in your case (the other case is when you have command line params) educate your teachers with this: en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

          – neal aise
          Jul 15 '10 at 10:25

















        oh lol so stupid of me :P thanks a lot :)

        – Fahad Uddin
        Jul 10 '10 at 23:11





        oh lol so stupid of me :P thanks a lot :)

        – Fahad Uddin
        Jul 10 '10 at 23:11













        void main? =(

        – jamesdlin
        Jul 11 '10 at 0:24





        void main? =(

        – jamesdlin
        Jul 11 '10 at 0:24













        i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better

        – Fahad Uddin
        Jul 14 '10 at 12:45





        i tried my best to get out of void main but this is what i am being taught :( int main() is many a times better

        – Fahad Uddin
        Jul 14 '10 at 12:45













        @fahad it has to be int main(void) in your case (the other case is when you have command line params) educate your teachers with this: en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

        – neal aise
        Jul 15 '10 at 10:25





        @fahad it has to be int main(void) in your case (the other case is when you have command line params) educate your teachers with this: en.wikipedia.org/wiki/Main_function_(programming)#C_and_C.2B.2B

        – neal aise
        Jul 15 '10 at 10:25











        1














        Full recursive solution in c++ (for c replace cout lines with printf):



        void printPrimeFactors(int num)
        {
        static int divisor = 2; // 2 is the first prime number

        if ( num == 1 ) //if num = 1 we finished
        {
        divisor = 2; //restore divisor, so it'll be ready for the next run
        return;
        }
        else if ( num % divisor == 0 ) //if num divided by divisor
        {
        cout << divisor << " "; //print divisor
        printPrimeFactors( num / divisor ); //call the function with num/divisor
        }
        else //if num not divided by divisor
        {
        divisor++; //increase divisor
        printPrimeFactors( num );
        }
        }





        share|improve this answer
























        • Why is the static a must in this case?

          – shinzou
          Dec 10 '14 at 19:48
















        1














        Full recursive solution in c++ (for c replace cout lines with printf):



        void printPrimeFactors(int num)
        {
        static int divisor = 2; // 2 is the first prime number

        if ( num == 1 ) //if num = 1 we finished
        {
        divisor = 2; //restore divisor, so it'll be ready for the next run
        return;
        }
        else if ( num % divisor == 0 ) //if num divided by divisor
        {
        cout << divisor << " "; //print divisor
        printPrimeFactors( num / divisor ); //call the function with num/divisor
        }
        else //if num not divided by divisor
        {
        divisor++; //increase divisor
        printPrimeFactors( num );
        }
        }





        share|improve this answer
























        • Why is the static a must in this case?

          – shinzou
          Dec 10 '14 at 19:48














        1












        1








        1







        Full recursive solution in c++ (for c replace cout lines with printf):



        void printPrimeFactors(int num)
        {
        static int divisor = 2; // 2 is the first prime number

        if ( num == 1 ) //if num = 1 we finished
        {
        divisor = 2; //restore divisor, so it'll be ready for the next run
        return;
        }
        else if ( num % divisor == 0 ) //if num divided by divisor
        {
        cout << divisor << " "; //print divisor
        printPrimeFactors( num / divisor ); //call the function with num/divisor
        }
        else //if num not divided by divisor
        {
        divisor++; //increase divisor
        printPrimeFactors( num );
        }
        }





        share|improve this answer













        Full recursive solution in c++ (for c replace cout lines with printf):



        void printPrimeFactors(int num)
        {
        static int divisor = 2; // 2 is the first prime number

        if ( num == 1 ) //if num = 1 we finished
        {
        divisor = 2; //restore divisor, so it'll be ready for the next run
        return;
        }
        else if ( num % divisor == 0 ) //if num divided by divisor
        {
        cout << divisor << " "; //print divisor
        printPrimeFactors( num / divisor ); //call the function with num/divisor
        }
        else //if num not divided by divisor
        {
        divisor++; //increase divisor
        printPrimeFactors( num );
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 12 '13 at 17:05









        yakiroyakiro

        66848




        66848













        • Why is the static a must in this case?

          – shinzou
          Dec 10 '14 at 19:48



















        • Why is the static a must in this case?

          – shinzou
          Dec 10 '14 at 19:48

















        Why is the static a must in this case?

        – shinzou
        Dec 10 '14 at 19:48





        Why is the static a must in this case?

        – shinzou
        Dec 10 '14 at 19:48











        1














        The best way to implement prime factorization with low overhead function calls would be . . .



        void factors(int number)
        {
        int divisor = 2;
        if (number == 1) { cout << "1"; return; }
        while ((number % divisor) && (number > divisor)) divisor++;
        cout << divisor << ", ";
        factors(number / divisor);
        }


        The number of function calls (recursion) is equal to the number of prime factors, including 1.






        share|improve this answer




























          1














          The best way to implement prime factorization with low overhead function calls would be . . .



          void factors(int number)
          {
          int divisor = 2;
          if (number == 1) { cout << "1"; return; }
          while ((number % divisor) && (number > divisor)) divisor++;
          cout << divisor << ", ";
          factors(number / divisor);
          }


          The number of function calls (recursion) is equal to the number of prime factors, including 1.






          share|improve this answer


























            1












            1








            1







            The best way to implement prime factorization with low overhead function calls would be . . .



            void factors(int number)
            {
            int divisor = 2;
            if (number == 1) { cout << "1"; return; }
            while ((number % divisor) && (number > divisor)) divisor++;
            cout << divisor << ", ";
            factors(number / divisor);
            }


            The number of function calls (recursion) is equal to the number of prime factors, including 1.






            share|improve this answer













            The best way to implement prime factorization with low overhead function calls would be . . .



            void factors(int number)
            {
            int divisor = 2;
            if (number == 1) { cout << "1"; return; }
            while ((number % divisor) && (number > divisor)) divisor++;
            cout << divisor << ", ";
            factors(number / divisor);
            }


            The number of function calls (recursion) is equal to the number of prime factors, including 1.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 24 '13 at 16:28









            IntrovertisIntrovertis

            111




            111























                1














                I did this in C. Depending on the compiler, minor changes might be needed to make in the program.



                #include<stdio.h>
                int primefact(int);
                int main()
                {
                int n;
                printf("Enter a number whose prime factors are to be calculated : n");
                scanf_s("%d", &n);
                printf("Prime factors of %d are : ");
                primefact(n);
                printf("n");
                return 0;
                }
                int primefact(int n)
                {
                int i=2;
                while(n%i!=0)
                i++;
                printf("%d ", i);
                if(n==i)
                return 0;
                else
                primefact(n/i);
                }





                share|improve this answer






























                  1














                  I did this in C. Depending on the compiler, minor changes might be needed to make in the program.



                  #include<stdio.h>
                  int primefact(int);
                  int main()
                  {
                  int n;
                  printf("Enter a number whose prime factors are to be calculated : n");
                  scanf_s("%d", &n);
                  printf("Prime factors of %d are : ");
                  primefact(n);
                  printf("n");
                  return 0;
                  }
                  int primefact(int n)
                  {
                  int i=2;
                  while(n%i!=0)
                  i++;
                  printf("%d ", i);
                  if(n==i)
                  return 0;
                  else
                  primefact(n/i);
                  }





                  share|improve this answer




























                    1












                    1








                    1







                    I did this in C. Depending on the compiler, minor changes might be needed to make in the program.



                    #include<stdio.h>
                    int primefact(int);
                    int main()
                    {
                    int n;
                    printf("Enter a number whose prime factors are to be calculated : n");
                    scanf_s("%d", &n);
                    printf("Prime factors of %d are : ");
                    primefact(n);
                    printf("n");
                    return 0;
                    }
                    int primefact(int n)
                    {
                    int i=2;
                    while(n%i!=0)
                    i++;
                    printf("%d ", i);
                    if(n==i)
                    return 0;
                    else
                    primefact(n/i);
                    }





                    share|improve this answer















                    I did this in C. Depending on the compiler, minor changes might be needed to make in the program.



                    #include<stdio.h>
                    int primefact(int);
                    int main()
                    {
                    int n;
                    printf("Enter a number whose prime factors are to be calculated : n");
                    scanf_s("%d", &n);
                    printf("Prime factors of %d are : ");
                    primefact(n);
                    printf("n");
                    return 0;
                    }
                    int primefact(int n)
                    {
                    int i=2;
                    while(n%i!=0)
                    i++;
                    printf("%d ", i);
                    if(n==i)
                    return 0;
                    else
                    primefact(n/i);
                    }






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jun 23 '15 at 19:44









                    Ivan

                    2,56151952




                    2,56151952










                    answered Jun 23 '15 at 19:00









                    user5041802user5041802

                    111




                    111























                        0














                        Agree with IVlad - also, what happens in the case when num is prime? How many times will the recursive function be called for e.g. num = 7?






                        share|improve this answer
























                        • ...and - how does prime return it's value to the caller?

                          – Will A
                          Jul 10 '10 at 23:14











                        • i used my concept of prime factor calculation and a prime factor program made without recursion for the help. pastebin.com/fVbjFGzQ

                          – Fahad Uddin
                          Jul 10 '10 at 23:19











                        • I don't see a return statement anywhere in your prime function. Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...

                          – Will A
                          Jul 10 '10 at 23:21
















                        0














                        Agree with IVlad - also, what happens in the case when num is prime? How many times will the recursive function be called for e.g. num = 7?






                        share|improve this answer
























                        • ...and - how does prime return it's value to the caller?

                          – Will A
                          Jul 10 '10 at 23:14











                        • i used my concept of prime factor calculation and a prime factor program made without recursion for the help. pastebin.com/fVbjFGzQ

                          – Fahad Uddin
                          Jul 10 '10 at 23:19











                        • I don't see a return statement anywhere in your prime function. Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...

                          – Will A
                          Jul 10 '10 at 23:21














                        0












                        0








                        0







                        Agree with IVlad - also, what happens in the case when num is prime? How many times will the recursive function be called for e.g. num = 7?






                        share|improve this answer













                        Agree with IVlad - also, what happens in the case when num is prime? How many times will the recursive function be called for e.g. num = 7?







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 10 '10 at 23:09









                        Will AWill A

                        22.2k13857




                        22.2k13857













                        • ...and - how does prime return it's value to the caller?

                          – Will A
                          Jul 10 '10 at 23:14











                        • i used my concept of prime factor calculation and a prime factor program made without recursion for the help. pastebin.com/fVbjFGzQ

                          – Fahad Uddin
                          Jul 10 '10 at 23:19











                        • I don't see a return statement anywhere in your prime function. Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...

                          – Will A
                          Jul 10 '10 at 23:21



















                        • ...and - how does prime return it's value to the caller?

                          – Will A
                          Jul 10 '10 at 23:14











                        • i used my concept of prime factor calculation and a prime factor program made without recursion for the help. pastebin.com/fVbjFGzQ

                          – Fahad Uddin
                          Jul 10 '10 at 23:19











                        • I don't see a return statement anywhere in your prime function. Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...

                          – Will A
                          Jul 10 '10 at 23:21

















                        ...and - how does prime return it's value to the caller?

                        – Will A
                        Jul 10 '10 at 23:14





                        ...and - how does prime return it's value to the caller?

                        – Will A
                        Jul 10 '10 at 23:14













                        i used my concept of prime factor calculation and a prime factor program made without recursion for the help. pastebin.com/fVbjFGzQ

                        – Fahad Uddin
                        Jul 10 '10 at 23:19





                        i used my concept of prime factor calculation and a prime factor program made without recursion for the help. pastebin.com/fVbjFGzQ

                        – Fahad Uddin
                        Jul 10 '10 at 23:19













                        I don't see a return statement anywhere in your prime function. Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...

                        – Will A
                        Jul 10 '10 at 23:21





                        I don't see a return statement anywhere in your prime function. Also - try going through the code with num = 7 (and the i = num assignment in the right place) and see what happens...

                        – Will A
                        Jul 10 '10 at 23:21











                        0














                        #include<stdio.h>
                        #include<stdlib.h>

                        int ar[10]={0};
                        int i=0,j=2;

                        void P(int n)
                        {
                        if(n<=1){
                        return ;
                        }

                        else{
                        if(n%j == 0){
                        printf("%dt",j);
                        n=n/j;

                        }
                        else{
                        j++;
                        }
                        P(n);
                        }
                        }

                        int main(void)
                        {
                        int n;
                        printf("Enter n = ");
                        scanf("%d",&n);
                        P(n);
                        printf("n");
                        return 0;
                        }





                        share|improve this answer
























                        • I suggest you don't include the corrected code, just an explanation of the error in question and the general direction on how to fix it.

                          – Theocharis K.
                          Jul 4 '14 at 18:10













                        • There's no need to add an answer to an already properly answered question. And as @TheocharisK. said, please add also an explanation to your answer next time. And btw... Welcome to SO.

                          – Pr0gr4mm3r
                          Jul 4 '14 at 18:13
















                        0














                        #include<stdio.h>
                        #include<stdlib.h>

                        int ar[10]={0};
                        int i=0,j=2;

                        void P(int n)
                        {
                        if(n<=1){
                        return ;
                        }

                        else{
                        if(n%j == 0){
                        printf("%dt",j);
                        n=n/j;

                        }
                        else{
                        j++;
                        }
                        P(n);
                        }
                        }

                        int main(void)
                        {
                        int n;
                        printf("Enter n = ");
                        scanf("%d",&n);
                        P(n);
                        printf("n");
                        return 0;
                        }





                        share|improve this answer
























                        • I suggest you don't include the corrected code, just an explanation of the error in question and the general direction on how to fix it.

                          – Theocharis K.
                          Jul 4 '14 at 18:10













                        • There's no need to add an answer to an already properly answered question. And as @TheocharisK. said, please add also an explanation to your answer next time. And btw... Welcome to SO.

                          – Pr0gr4mm3r
                          Jul 4 '14 at 18:13














                        0












                        0








                        0







                        #include<stdio.h>
                        #include<stdlib.h>

                        int ar[10]={0};
                        int i=0,j=2;

                        void P(int n)
                        {
                        if(n<=1){
                        return ;
                        }

                        else{
                        if(n%j == 0){
                        printf("%dt",j);
                        n=n/j;

                        }
                        else{
                        j++;
                        }
                        P(n);
                        }
                        }

                        int main(void)
                        {
                        int n;
                        printf("Enter n = ");
                        scanf("%d",&n);
                        P(n);
                        printf("n");
                        return 0;
                        }





                        share|improve this answer













                        #include<stdio.h>
                        #include<stdlib.h>

                        int ar[10]={0};
                        int i=0,j=2;

                        void P(int n)
                        {
                        if(n<=1){
                        return ;
                        }

                        else{
                        if(n%j == 0){
                        printf("%dt",j);
                        n=n/j;

                        }
                        else{
                        j++;
                        }
                        P(n);
                        }
                        }

                        int main(void)
                        {
                        int n;
                        printf("Enter n = ");
                        scanf("%d",&n);
                        P(n);
                        printf("n");
                        return 0;
                        }






                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Jul 4 '14 at 17:48









                        PrashantPrashant

                        1




                        1













                        • I suggest you don't include the corrected code, just an explanation of the error in question and the general direction on how to fix it.

                          – Theocharis K.
                          Jul 4 '14 at 18:10













                        • There's no need to add an answer to an already properly answered question. And as @TheocharisK. said, please add also an explanation to your answer next time. And btw... Welcome to SO.

                          – Pr0gr4mm3r
                          Jul 4 '14 at 18:13



















                        • I suggest you don't include the corrected code, just an explanation of the error in question and the general direction on how to fix it.

                          – Theocharis K.
                          Jul 4 '14 at 18:10













                        • There's no need to add an answer to an already properly answered question. And as @TheocharisK. said, please add also an explanation to your answer next time. And btw... Welcome to SO.

                          – Pr0gr4mm3r
                          Jul 4 '14 at 18:13

















                        I suggest you don't include the corrected code, just an explanation of the error in question and the general direction on how to fix it.

                        – Theocharis K.
                        Jul 4 '14 at 18:10







                        I suggest you don't include the corrected code, just an explanation of the error in question and the general direction on how to fix it.

                        – Theocharis K.
                        Jul 4 '14 at 18:10















                        There's no need to add an answer to an already properly answered question. And as @TheocharisK. said, please add also an explanation to your answer next time. And btw... Welcome to SO.

                        – Pr0gr4mm3r
                        Jul 4 '14 at 18:13





                        There's no need to add an answer to an already properly answered question. And as @TheocharisK. said, please add also an explanation to your answer next time. And btw... Welcome to SO.

                        – Pr0gr4mm3r
                        Jul 4 '14 at 18:13











                        0














                        // recursivePrime.cpp
                        // Purpose: factor finding for an integer
                        // Author: Ping-Sung Liao, Kaohsiung,TAIWAN
                        // Date: 2017/02/02
                        // Version : 1.0
                        // Reference:
                        // http://stackoverflow.com/questions/3221156/recursive-func-to-find-prime-factors

                        #include<stdio.h>
                        #include<stdlib.h>
                        #include<math.h>


                        int primefactor(int num,int i);
                        int main(void)
                        {
                        int num, i;
                        printf("Enter a number whose prime factors are to be calculated:");
                        scanf("%d",&num);
                        i=int ( sqrt (num) );
                        primefactor(num,i);

                        system("pause"); // instead of getch()
                        }

                        int primefactor(int num,int i)
                        { printf("num %d i=%dn", num, i);
                        if (i==1)
                        printf("prime found= %dn", num); // prime appearing in he variuable num

                        else if(num%i==0)
                        { primefactor( int (num/i) , int ( sqrt(num/i) ) );
                        primefactor( i , int (sqrt ( i ) ) );
                        }
                        else
                        { i--;
                        primefactor(num,i);
                        }
                        return 0;
                        }





                        share|improve this answer





















                        • 1





                          While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

                          – Donald Duck
                          Feb 2 '17 at 14:43
















                        0














                        // recursivePrime.cpp
                        // Purpose: factor finding for an integer
                        // Author: Ping-Sung Liao, Kaohsiung,TAIWAN
                        // Date: 2017/02/02
                        // Version : 1.0
                        // Reference:
                        // http://stackoverflow.com/questions/3221156/recursive-func-to-find-prime-factors

                        #include<stdio.h>
                        #include<stdlib.h>
                        #include<math.h>


                        int primefactor(int num,int i);
                        int main(void)
                        {
                        int num, i;
                        printf("Enter a number whose prime factors are to be calculated:");
                        scanf("%d",&num);
                        i=int ( sqrt (num) );
                        primefactor(num,i);

                        system("pause"); // instead of getch()
                        }

                        int primefactor(int num,int i)
                        { printf("num %d i=%dn", num, i);
                        if (i==1)
                        printf("prime found= %dn", num); // prime appearing in he variuable num

                        else if(num%i==0)
                        { primefactor( int (num/i) , int ( sqrt(num/i) ) );
                        primefactor( i , int (sqrt ( i ) ) );
                        }
                        else
                        { i--;
                        primefactor(num,i);
                        }
                        return 0;
                        }





                        share|improve this answer





















                        • 1





                          While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

                          – Donald Duck
                          Feb 2 '17 at 14:43














                        0












                        0








                        0







                        // recursivePrime.cpp
                        // Purpose: factor finding for an integer
                        // Author: Ping-Sung Liao, Kaohsiung,TAIWAN
                        // Date: 2017/02/02
                        // Version : 1.0
                        // Reference:
                        // http://stackoverflow.com/questions/3221156/recursive-func-to-find-prime-factors

                        #include<stdio.h>
                        #include<stdlib.h>
                        #include<math.h>


                        int primefactor(int num,int i);
                        int main(void)
                        {
                        int num, i;
                        printf("Enter a number whose prime factors are to be calculated:");
                        scanf("%d",&num);
                        i=int ( sqrt (num) );
                        primefactor(num,i);

                        system("pause"); // instead of getch()
                        }

                        int primefactor(int num,int i)
                        { printf("num %d i=%dn", num, i);
                        if (i==1)
                        printf("prime found= %dn", num); // prime appearing in he variuable num

                        else if(num%i==0)
                        { primefactor( int (num/i) , int ( sqrt(num/i) ) );
                        primefactor( i , int (sqrt ( i ) ) );
                        }
                        else
                        { i--;
                        primefactor(num,i);
                        }
                        return 0;
                        }





                        share|improve this answer















                        // recursivePrime.cpp
                        // Purpose: factor finding for an integer
                        // Author: Ping-Sung Liao, Kaohsiung,TAIWAN
                        // Date: 2017/02/02
                        // Version : 1.0
                        // Reference:
                        // http://stackoverflow.com/questions/3221156/recursive-func-to-find-prime-factors

                        #include<stdio.h>
                        #include<stdlib.h>
                        #include<math.h>


                        int primefactor(int num,int i);
                        int main(void)
                        {
                        int num, i;
                        printf("Enter a number whose prime factors are to be calculated:");
                        scanf("%d",&num);
                        i=int ( sqrt (num) );
                        primefactor(num,i);

                        system("pause"); // instead of getch()
                        }

                        int primefactor(int num,int i)
                        { printf("num %d i=%dn", num, i);
                        if (i==1)
                        printf("prime found= %dn", num); // prime appearing in he variuable num

                        else if(num%i==0)
                        { primefactor( int (num/i) , int ( sqrt(num/i) ) );
                        primefactor( i , int (sqrt ( i ) ) );
                        }
                        else
                        { i--;
                        primefactor(num,i);
                        }
                        return 0;
                        }






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Feb 2 '17 at 14:59

























                        answered Feb 2 '17 at 14:32









                        bsliaobsliao

                        11




                        11








                        • 1





                          While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

                          – Donald Duck
                          Feb 2 '17 at 14:43














                        • 1





                          While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

                          – Donald Duck
                          Feb 2 '17 at 14:43








                        1




                        1





                        While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

                        – Donald Duck
                        Feb 2 '17 at 14:43





                        While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

                        – Donald Duck
                        Feb 2 '17 at 14:43











                        0














                        #include  <`stdio.h`>

                        void pf(int,int);

                        int main()

                        {

                        int a,i=2;

                        printf("Enter the Number:n");
                        scanf("%d",&a);

                        pf(a,i);
                        }

                        void pf(int x,int y)

                        {

                        if(x==1)

                        return 1;

                        else
                        {
                        if(x%y==0)
                        {printf("%dt",y);
                        pf(x/y,y);
                        }

                        else
                        {
                        y++;
                        pf(x,y);
                        }
                        }
                        }





                        share|improve this answer






























                          0














                          #include  <`stdio.h`>

                          void pf(int,int);

                          int main()

                          {

                          int a,i=2;

                          printf("Enter the Number:n");
                          scanf("%d",&a);

                          pf(a,i);
                          }

                          void pf(int x,int y)

                          {

                          if(x==1)

                          return 1;

                          else
                          {
                          if(x%y==0)
                          {printf("%dt",y);
                          pf(x/y,y);
                          }

                          else
                          {
                          y++;
                          pf(x,y);
                          }
                          }
                          }





                          share|improve this answer




























                            0












                            0








                            0







                            #include  <`stdio.h`>

                            void pf(int,int);

                            int main()

                            {

                            int a,i=2;

                            printf("Enter the Number:n");
                            scanf("%d",&a);

                            pf(a,i);
                            }

                            void pf(int x,int y)

                            {

                            if(x==1)

                            return 1;

                            else
                            {
                            if(x%y==0)
                            {printf("%dt",y);
                            pf(x/y,y);
                            }

                            else
                            {
                            y++;
                            pf(x,y);
                            }
                            }
                            }





                            share|improve this answer















                            #include  <`stdio.h`>

                            void pf(int,int);

                            int main()

                            {

                            int a,i=2;

                            printf("Enter the Number:n");
                            scanf("%d",&a);

                            pf(a,i);
                            }

                            void pf(int x,int y)

                            {

                            if(x==1)

                            return 1;

                            else
                            {
                            if(x%y==0)
                            {printf("%dt",y);
                            pf(x/y,y);
                            }

                            else
                            {
                            y++;
                            pf(x,y);
                            }
                            }
                            }






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 1 at 12:26









                            Sumithran

                            8173822




                            8173822










                            answered Jan 1 at 5:47









                            Mayur AnklekarMayur Anklekar

                            1




                            1























                                -1














                                Implementation in java..



                                public class PrimeFactor {

                                public int divisor=2;
                                void printPrimeFactors(int num)
                                {

                                if(num == 1)
                                return;

                                if(num%divisor!=0)
                                {
                                while(num%divisor!=0)
                                ++divisor;
                                }
                                if(num%divisor==0){

                                System.out.println(divisor);
                                printPrimeFactors(num/divisor);
                                }

                                }
                                public static void main(String args)
                                {
                                PrimeFactor obj = new PrimeFactor();
                                obj.printPrimeFactors(90);
                                }

                                }





                                share|improve this answer
























                                • Beyond attempting to solve the original question, this isn't relevant to the Turbo C issue raised in the original question.

                                  – Sam Storie
                                  May 11 '15 at 20:40











                                • A quick explanation as to why this is the right way to do it would be beneficial.

                                  – David
                                  May 11 '15 at 20:53
















                                -1














                                Implementation in java..



                                public class PrimeFactor {

                                public int divisor=2;
                                void printPrimeFactors(int num)
                                {

                                if(num == 1)
                                return;

                                if(num%divisor!=0)
                                {
                                while(num%divisor!=0)
                                ++divisor;
                                }
                                if(num%divisor==0){

                                System.out.println(divisor);
                                printPrimeFactors(num/divisor);
                                }

                                }
                                public static void main(String args)
                                {
                                PrimeFactor obj = new PrimeFactor();
                                obj.printPrimeFactors(90);
                                }

                                }





                                share|improve this answer
























                                • Beyond attempting to solve the original question, this isn't relevant to the Turbo C issue raised in the original question.

                                  – Sam Storie
                                  May 11 '15 at 20:40











                                • A quick explanation as to why this is the right way to do it would be beneficial.

                                  – David
                                  May 11 '15 at 20:53














                                -1












                                -1








                                -1







                                Implementation in java..



                                public class PrimeFactor {

                                public int divisor=2;
                                void printPrimeFactors(int num)
                                {

                                if(num == 1)
                                return;

                                if(num%divisor!=0)
                                {
                                while(num%divisor!=0)
                                ++divisor;
                                }
                                if(num%divisor==0){

                                System.out.println(divisor);
                                printPrimeFactors(num/divisor);
                                }

                                }
                                public static void main(String args)
                                {
                                PrimeFactor obj = new PrimeFactor();
                                obj.printPrimeFactors(90);
                                }

                                }





                                share|improve this answer













                                Implementation in java..



                                public class PrimeFactor {

                                public int divisor=2;
                                void printPrimeFactors(int num)
                                {

                                if(num == 1)
                                return;

                                if(num%divisor!=0)
                                {
                                while(num%divisor!=0)
                                ++divisor;
                                }
                                if(num%divisor==0){

                                System.out.println(divisor);
                                printPrimeFactors(num/divisor);
                                }

                                }
                                public static void main(String args)
                                {
                                PrimeFactor obj = new PrimeFactor();
                                obj.printPrimeFactors(90);
                                }

                                }






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered May 11 '15 at 20:29









                                RajRaj

                                286310




                                286310













                                • Beyond attempting to solve the original question, this isn't relevant to the Turbo C issue raised in the original question.

                                  – Sam Storie
                                  May 11 '15 at 20:40











                                • A quick explanation as to why this is the right way to do it would be beneficial.

                                  – David
                                  May 11 '15 at 20:53



















                                • Beyond attempting to solve the original question, this isn't relevant to the Turbo C issue raised in the original question.

                                  – Sam Storie
                                  May 11 '15 at 20:40











                                • A quick explanation as to why this is the right way to do it would be beneficial.

                                  – David
                                  May 11 '15 at 20:53

















                                Beyond attempting to solve the original question, this isn't relevant to the Turbo C issue raised in the original question.

                                – Sam Storie
                                May 11 '15 at 20:40





                                Beyond attempting to solve the original question, this isn't relevant to the Turbo C issue raised in the original question.

                                – Sam Storie
                                May 11 '15 at 20:40













                                A quick explanation as to why this is the right way to do it would be beneficial.

                                – David
                                May 11 '15 at 20:53





                                A quick explanation as to why this is the right way to do it would be beneficial.

                                – David
                                May 11 '15 at 20:53


















                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f3221156%2frecursive-func-to-find-prime-factors%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                Npm cannot find a required file even through it is in the searched directory

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith