9.02.2011

8.Design numbers rectangle structure

Q. Write a program to generate a following numbers structure:
   (Where user entered number through keyboard, for example if num=5)
                            55555
                            44444
                            33333
                            22222
                            11111

Ans.

/* c program for number structure*/
#include<stdio.h>
#include<conio.h>
int main()
{
 int num,r,c;
 printf("Enter loop repeat number(rows): "); 
 scanf("%d",&num);
 for(r=num; r>=1; r--)
 {
  for(c=num; c>=1; c--)
     printf("%d",r);
  printf("\n");
 }
 getch();
 return 0;
}
/*************OUTPUT**************
Enter loop repeat number(rows): 5

                            55555
                            44444
                            33333
                            22222
                            11111

***********************************/              

4 comments:

  1. using single loop dis program is posible or not/

    ReplyDelete
    Replies
    1. @Gaurav Giri,
      This is not possible to single loop, because outer loop used for number of rows and inner loop used for number of columns repetition.

      Delete
    2. thanks alot sir but why to use post fix --,here both loops r increasing no?

      Delete
    3. Yes, You can write your own code, also its depend on what we want to make design/pyramid.

      We can also make above pyramid program with differ code as:

      #include"stdio.h"
      int main()
      {
      int num=5,n,r,c;
      for(r=1,n=num; r<=num; r++,n--)
      {
      for(c=1; c<=num; c++)
      printf("%d",n);
      printf("\n");
      }
      getch();
      return 0;
      }

      /*output would be same as above program*/

      Delete