Software & Finance





C Programming (Turb C++ Compiler) Loops - Build Pattern of * and numbers using loops





This program will accept a number as input. The loops are used to build a pattern of * and numbers. There are four loops, two for displaying number and other two for displaying pattern of stars.


Source Code


#include <stdio.h>

 

int main()

{

    int i,j,n;

    printf("Enter a number: ");

    scanf("%d", &n);

 

    printf("\n");

 

    for(i = n; i > 1; i--)

    {

        for(j = 1; j <= i; j++)

            printf("*");

        printf("\n");

    }

    for(i = 1; i <= n; i++)

    {

        for(j = 1; j <= i; j++)

            printf("*");

        printf("\n");

    }

    printf("\n");

 

    for(i = 1; i < n; i++)

    {

        for(j = 1; j <= i; j++)

            printf("%d",j);

        printf("\n");

    }

    for(int i = n; i >= 0; i--)

    {

        for(int j = 1; j <= i; j++)

            printf("%d",j);

        printf("\n");

    }

    printf("\n");

 

    return 0;

}

Output


Enter a number: 6

 

******

*****

****

***

**

*

**

***

****

*****

******

 

1

12

123

1234

12345

123456

12345

1234

123

12

1