C Program to Print Pyramid
Write a C Program to print pyramid ?
Hello friends, we are back with another simple and most interesting C Program.In our last blog we saw you how to write c program to check whether the number is palindrome or not.In this program we will make a pyramid with the help of C programming Language.This kind of problems is useful for beginners to understands the fundamentals of loops and spaces.
A pattern of numbers, star or characters is a way of arranging these in some logical manner or they may form a sequence. Some of these patterns are triangles which have special importance in mathematics.Let’s see the programming part of this program.
C Programming Code to print pyramid:
#include<stdio.h>
int pyramid(int row )
{
int i,j,space=4,c=0;
for ( i=1;i<=row;i++)
{
for (j=1;j<=space;j–)
{
printf(” “);
}
for( j=1;j<=i;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
return ( row );
}
int main()
{
int line;
printf(“enter the number of lines”);
scanf(“%d”,&line);
pyramid ( line );
return 0;
}
Logic behind this program:
We first take the number of rows in the pattern as input from user using scanf function.
One iteration of outer for loop will print a row of pyramid.
Inner for loop prints the initial spaces for every line and nested while loop prints (2*r – 1) space separated stars for rth row of pyramid.
Stay connected with us for more C Programming Language.Leave a comment for any question.