Blogger templates

Pages

Monday, 11 June 2012

Write a C Program to Print The Given Number Pattern by using for Loop

Number Pattern

Nested for Statement:
If one for statement is completely placed with in the other for Statement.

Remember:
  • One loop must completely be placed inside the other loop means no overlapping of loops.
  • Each loop must have different variables.
Statement of C Program: This Program Generates the Following Number Pattern :


#include<stdio.h>
#include<conio.h>
main()
{
int i , j , n , p ;
k=1;
clrscr();
printf(" Enter the Number of Rows");
scanf("%d" , &n);
for( i=1 ; i<=n ; i++ )
{
for( j=1 ; j< (n-1) ; j++ )
printf(" ");
for( j=1 ; j<=i ; j++ )
printf("%d" , k++ );
for( j= (i-1) ; j>=1 ; j-- ) 
{
p=k-2;
k=k-1;
printf("%d" , p);
                             /*  End of last for j */

prtintf("\n");                                            //  To goto Next Row
}                            /*  End of for i  */
getch();
}                            /* End of main() */

0 comments:

Post a Comment