Simple program recursion
simple program finding
factorial
K.LAVANYA
CSE-A
24D21A0535
PPS
RECURSION
• Recursion is a function in
which a function calls itself
• Recursion make large
program into small program
• It has a condition called base
case
• The base which tell a
recursion function where to
stop
Example program for recursion
• #include<stdio.h>
• Int sum(int n);
• Int main() {
• Int number ,result;
• Printf(“enter a positive integer“);
• Scanf(“%d”,&number);
• Result=sum(number);
• Printf(“sum=%d”,result);
• Return 0;
• }
• Int sum(int n) {
• If(n!=0)
• Return n+sum(n-1);
• Else
• Return n;
• }
FACTORIAL
• The factorial of a
nonnegative integer
n,written n!(and pronounced
“n factorial”), is the product
• Recursion definition of the
factorial function
• N!=n*(n-1)
• Example
• 5!=5*4*3*2*1
• 5!=5*(4!)
Simple program on factorial
• #include<stdio.h>
• #include<conio.h>
• Int factorial(int);
• Int main()
• {
• Int n;
• Printf(“n enter number”);
• Scanf(“%d”,&n);
• Answer=factorial(n)
• Printf(“factorial=%d”,answer);
• getch()
• Return 0;
• }
• Int factorial(int n)
• {
• Int f;
• If(n==0)
• Return 1;
• Else
• F=n*factorial(n-1);
• Return f;
• }
output
• Enter a number 5
factorial is 120
THANK YOU

Simple program recursion problem solving.pptx

  • 1.
    Simple program recursion simpleprogram finding factorial K.LAVANYA CSE-A 24D21A0535 PPS
  • 2.
    RECURSION • Recursion isa function in which a function calls itself • Recursion make large program into small program • It has a condition called base case • The base which tell a recursion function where to stop
  • 3.
    Example program forrecursion • #include<stdio.h> • Int sum(int n); • Int main() { • Int number ,result; • Printf(“enter a positive integer“); • Scanf(“%d”,&number); • Result=sum(number); • Printf(“sum=%d”,result); • Return 0; • } • Int sum(int n) { • If(n!=0) • Return n+sum(n-1); • Else • Return n; • }
  • 4.
    FACTORIAL • The factorialof a nonnegative integer n,written n!(and pronounced “n factorial”), is the product • Recursion definition of the factorial function • N!=n*(n-1) • Example • 5!=5*4*3*2*1 • 5!=5*(4!)
  • 5.
    Simple program onfactorial • #include<stdio.h> • #include<conio.h> • Int factorial(int); • Int main() • { • Int n; • Printf(“n enter number”); • Scanf(“%d”,&n); • Answer=factorial(n) • Printf(“factorial=%d”,answer); • getch() • Return 0; • }
  • 6.
    • Int factorial(intn) • { • Int f; • If(n==0) • Return 1; • Else • F=n*factorial(n-1); • Return f; • }
  • 7.
    output • Enter anumber 5 factorial is 120
  • 8.