Thursday, 8 August 2013

Chapter 5 Solutions Problem [J][b]

Question- A positive integer is entered through the keyboard, write a 
program to obtain the prime factors of the number. Modify the
function suitably to obtain the prime factors recursively.

Solution- I have done this problem without recursion in Problem [D][e]. let's check out the recursive view. recursion problems are always tricky. so, go through program carefully.

#include<stdio.h>

int main()
{
int n;
printf("Enter any integer\n");
scanf("%d",&n);
printf("Prime factors of %d are=>\n",n);
prime(n);
return 0;
}
prime(int n)
{
static int i=2;

if(i<=n)
{
if(n%i==0)
{
printf("%d ",i);
n=n/i;
}
else
i++;
prime(n);
}
}

/* If you are a beginner in C . i am damn sure you guys must be staring a lot at the word "static"
. Surprising that's the main part of the Program. you will learn about this in further chapter. just for the sake of proper explanation, Static class is variable storage class. it is mainly used in the condition when we multiple calls of the same function and  we don't want to lose the value of the variable, which is obtained by previous call. if you don't get it. follow up this program. */


int main( )                                                                                 int main( ) 
{                                                                                              {
increment( ) ;                                                                          increment( ) ; 
increment( ) ;                                                                          increment( ) ; 
increment( ) ;                                                                          increment( ) ; 
return 0;                                                                                  return 0;
}                                                                                              }
increment( )                                                                           increment( ) 
{                                                                                             {
static int i = 1 ;                                                                      auto int i = 1 ;  
printf ( "%d\n", i ) ;                                                              printf ( "%d\n", i ) ; 
i = i + 1 ;                                                                                i=i+1;
output                                                                                    output 
1                                                                                              1
2                                                                                              1
3                                                                                              1

Source - Let us C book. 

/*Now here static and auto storage classes are used. auto is default storage. if you don't use any storage and it goes to auto. let's don't worry about this . let's see 1st program. there value of i persists,if  you notice carefully. after operation i=i+1; value of i is 2 which is used next operation to get i=3. but on the other hand ,in second program after i=i+1; value of i is 2 but that is rejected in next call of the same function. so that's why we get 1 1 1 as output. */ 


if you are able to grasp this concept, you can easily understand that in recursion problem ,i wanted to make value of i persistent i.e. don't start again with i=2,instead use previous value. that's why i used "static",   

No comments:

Post a Comment