C Programming

C Programming


Number Pyramid Pattern

Posted: 17 Feb 2014 11:30 AM PST

Q. Write a C program to print the following square number pyramid pattern as:

1  2  3  4  5
15 14 13 12
6  7  8
11 10
9

Ans.

/*c program for square number pyramid pattern*/
#include<stdio.h>
int main()
{
 int r,c,s1=1,s2=15;
 for(r=5; r>=1; r--)
 {
  if(r%2==0)
  {
    for(c=1; c<=r; c++,s2--)
       printf(" %d",s2);
  }
  else
  {
     for(c=1; c<=r; c++,s1++)
       printf(" %d",s1); 
  }
  printf("\n");
 }
 getch();
 return 0;
}

/**********************************************************
The output of above program would be:
*********************************************************/


Output of square number pyramid pattern C program
Figure: Screen shot for square number pyramid C program

Number Triangle Pattern

Posted: 17 Feb 2014 11:08 AM PST

Q. Write a C program to print the following number pattern as:

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

Ans.

/*c program for triangle number pattern*/
#include<stdio.h>
int main()
{
 int num,r,c,q=1;
 for(r=1; r<=5; r++,q=q+2)
 {
  for(c=1; c<=q; c++)
     printf(" %d",c);
  printf("\n");
 }
 getch();
 return 0;
}


/**********************************************************
The output of above program would be:
***********************************************************/
Output of number triangle pattern C program
Figure: Screen shot for number triangle pattern C program

Related Programs:

print the following pattern as:

1
1 4 9
1 4 9 16 25
1 4 9 16 25 36 49
1 4 9 16 25 36 49 64 81


Post a Comment