Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

confusion in recursion

$
0
0
I am learning about recursion in C so i examine a code in my book. but i have confusion in this recursion code that how break; will execute ?

i am wrong i know but factor(v); will execute each time before break;, that's what i am examine.
if i 6 as 'v' after that loop for (i=2;i<=v;i++) will be execute with i=2 and condition if (v%i==0) will satisfy. then printf("\n%d",i); and v=v/i; will execute so 'v' will be 3 and function factor(v); will call itself with v=3.

now the process will be same as before and 'v' will be 1 then function will call itself with 'v=1'.

now this time condition if (v%i==0) will not satisfy and no process under condition 'if'
will execute.

So my question is how statement break; will execute.

if i removed break; from code i get wrong output.

i have been read www.dreamincode.net/forums/topic/66595-recursion-a-tutorial-and-a-discussion/
www.cprogramming.com/tutorial/lesson16.html

#include<stdio.h>
#include<conio.h>
void factor (int);
main()
{
      int value;
      printf("\t\t LUC - 5 - d - e - Find prime factors of number\n\n");
      printf("Enter any number ");
      scanf("%d",&value);
      printf("\nFactors are ");
      factor(value);
      getch();
      }
      void factor (int v)
      {
      int i;
      for (i=2;i<=v;i++)
      {
          if (v%i==0)
      {
                 printf("\n%d",i);
                 v=v/i;
                 factor(v);
                 break;          
      }
      }
      }



Quote

output with statement break;

Posted Image

Quote

output without break;

Posted Image

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>