top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a c Program for Pyramid ?

+4 votes
928 views
                                      *
                                     * *
                                    * * *
                                   * * * *
                                  * * * * * 
                                 * * * *     

I above Ouput I Missed two stars.My question is how to find out the remaining stars.Please tel me in the C programming way.

posted Dec 30, 2013 by Giri Prasad

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
If you know that it is a pyramid and know the number of rows say in this case 6 then number of total stars are n*(n+1)/2 i.e. 21. Just count the number of * and subtract from 21 will give you number of missing stars.
Hi

Can you explain with program Logic.I am bit confuse.
Its simple ur first row will have 1 star, 2nd will have 2 and nth wil have n stars.
Now its a case of 1+2+3...n problem which is n*(n+1)/2...

1 Answer

–2 votes
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c,sp,x;
 printf("Enter loop repeat number(rows):");
 scanf("%d",&num);
 for(r=1; num>=r; r++)
 {
   for(sp=num-r; sp>=1; sp--)
      printf(" ");
   for(c=1; c<=r; c++)
      printf("%d",c);
   for(x=r-1; x>=1; x--)
      printf("%d",x);
   printf("\n");
 }
 getch();
 return 0;
} 
answer Jan 15, 2014 by Atul Mishra
I tested this code and it prints for num=5
     1
   121
  12321
 1234321
123454321

Where question is different then this....
before giving answer pls check my question.
Similar Questions
+1 vote

How to write a c program for data signaling rate for four signals? Data signaling rate formula should be written in c language as formula cannot be inserted?

+3 votes

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

...