Q:

Write a C Program to Print pyramid of numbers using Recursion

0

Write a C Program to Print pyramid of numbers. Here’s simple Program to Print pyramid of numbers using Recursion in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Below is the source code for C Program to Print pyramid of numbers using Recursion which is successfully compiled and run on Windows System to produce desired output as shown below :

 
 

SOURCE CODE : :

/* C Program to Print pyramid of numbers */


#include<stdio.h>
void func1(int n);
void func2(int n);
void func3(int n);

int main( )
{

    int n;
    printf("Enter how many lines u want to print ? ");
    scanf("%d",&n);
    printf("\n------------ Pattern 1 ----------- \n\n");
        func1(n);
        printf("\n");
        printf("\n------------ Pattern 2 ----------- \n\n");
        func2(n);
        printf("\n");
        printf("\n------------ Pattern 3 ----------- \n\n");
        func3(n);

        return 0;

}

void func1(int n)
{
        int i;
        if(n==0)
                return;
    else
        {
                func1(n-1);
                for(i=1; i<= n; i++)
                        printf("%d ",i);
        printf("\n");
        }
}

void func2(int n)
{
        int i;
        if(n==0)
                return;
    else
        {
                for(i=1; i<=n; i++)
                        printf("%d ",i);
        printf("\n");
                func2(n-1);
        }
}

void func3(int n)
{
        int i;
        if(n==0)
                return;
    else
        {
                for(i=n; i>=1; i--)
                        printf("%d ",i);
        printf("\n");
                func3(n-1);
        }
}

OUTPUT  : :


Enter how many lines u want to print ? 10

------------ Pattern 1 -----------

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10


------------ Pattern 2 -----------

1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


------------ Pattern 3 -----------

10 9 8 7 6 5 4 3 2 1
9 8 7 6 5 4 3 2 1
8 7 6 5 4 3 2 1
7 6 5 4 3 2 1
6 5 4 3 2 1
5 4 3 2 1
4 3 2 1
3 2 1
2 1
1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< C program to Print Heart Star Pattern using For lo...