Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts

C program to find factorial of a number using recursion

>> Wednesday, November 2, 2011


Factorial is mostly used in calculting permutation and combination. Factorial sign is '!' i.e exclamatory mark.
For eg 4! can be calculated as : 4!=4*3*2*1=24. Here the c program to calculate factorial using recursion function.



#include<stdio.h> #include<conio.h> int recfact(int); main( ) { int a, fact, rec ; printf ( "\nEnter any number " ) ; scanf ( "%d", &a ) ; rec = recfact ( a ) ; printf ( "Factorial value = %d", fact ) ; getch(); } int recfact( int x ) { int f ; if ( x == 1 ) return ( 1 ) ; else f = x * recfact ( x - 1 ) ; return ( f ) ; }


Output :

C program to find factorial using recursion
c program to find factorial using recursion

Read more...

c program to print n numbers without using loop

>> Tuesday, November 1, 2011


This program will print the n numbers without using any loop. Here we uses the concept of recursion. Lets See the program
to make clear view that how recursion works.


#include <stdio.h> #include <conio.h> void print(int p) {   if(p>0)   {     print(p-1);                    // recursive call;     printf("\n\n %d \t", p);   }   return; } int main() {   int n;   printf("enter the number\n\n");   scanf("%d",&n);                  // number to which you wnat to print   print(n);   getch();   return 0; }

Output:

C program to print n numbers, withou loop



Read more...

c program to find whether the given number is prime or not using Recursion ?

>> Friday, October 28, 2011


This C program uses recursion for finding whether the entered number is prime or not. A function is called recursive if the statement in a body calls the same functions. It uses the modulus operator ('%') to find the prime number.Lets have a look on this program. Function name is prime in this program and it calls prime() again till the condition is satisfied and till the parser is not return to the main function.




#include<stdio.h> #include<conio.h> int prime(int n) {     static int i=2;                                              if(n%i==0&&n!=2)            {            return(1);            }            if(i<n)            {            i++;            prime(n);                                    // recursion call            } } int main() {     int p;     printf("enter the number to check for prime number : ");     scanf("%d",&p);     int l=prime(p);                                      //function returns the value and puts in 'l' variable         if(l==1)                                            //conditions         printf("%d is not a prime number ", p);         else         printf("%d is a prime number ", p);     getch(); }

Here is output for the above program :

C program to find whether the entered number is prime or not using recursion
output : Find prime number using recursion


Read more...

About This Blog

This blog will contain c programs related to interview preparation, basic programs, operating system, graphics, data structure, algorithms implementation, compiler and porjects. C is one of the most widely used programming languages of all time. This blog is intended for engineer students and for the people who are preparing for their interview in IT sector.

Share and Save