About Me

My photo
Raipur, Chhattisgarh, India
Hi , I am Amit Thakur. I have worked as a QA Engineer for two years and as a Java Developer for one year in NIHILENT TECHNOLOGIES PVT. LTD., Pune.Currently I am working as DEAN (Research & Development) in Bhilai Institute of Technology, Raipur.

Tuesday, September 3, 2013

C Program to Print Right angled Pyramid using Nested Loops

Program : Print this Right angled Pyramid in C using Nested Loops

*
*  *
*  *  *
*  *  *  *
*  *  *  *  *
*  *  *  *  *  *

Live Example : Program

#include<stdio.h>
#include<conio.h>
main()
{
int i,j,lines;
char ch = '*';

clrscr();
printf("Enter number of lines : ");
scanf("%d",&lines);

    for(i=0;i <=lines;i++)
    {
    printf("\n");
        for (j=0;j < i;j++)
           printf("%c",ch);
    }
getch();
}

Explanation of Above Code :

Accept the total number of lines to be printed.
printf("Enter number of lines : ");
scanf("%d",&lines);
Now we are using nested loop to print the pyramid. First for loop is used to print the star on the line. Second loop will be used to identify the total number of stars to be printed on the screen.
for(i=0;i <=lines;i++)
    {
    printf("n");
        for (j=0;j < i;j++)
           printf("%c",ch);
    }

No comments: