Monday, 3 December 2012

Number Rectangle Program

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

1234
2341
3412
4123

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num,n,r,c,t;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r; c<=num; c++)
     printf("%d",c);
  for(t=1; t<r; t++)
     printf("%d",t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number rectangle C program
Figure: Screen shot for number rectangle C program

Character Pyramid Program

Q. Write a C program to print the following character pyramid C program:

ABCD

CDA
AB
C

Ans.

Cross-reference: Before you read answer of above program i recommend to read Number Pyramid Program for easily understand this program. 

/*c program for character pyramid*/
#include<stdio.h>
int main()
{

 char ch='D',n,c,r,t;
 n=ch;
 for(r='A'; r<=ch; r++,n--)
 {
  if(r=='B' || r=='D')
  {
    for(c='A',t='C'; c<=n; c++,t++)
    {
      if(c=='C' && r=='B')
         printf("A");
      else
         printf("%c",t);
    }
  }
  else
  {
    for(c='A'; c<=n; c++)
        printf("%c", c);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of character pyramid C program
Figure: Screen shot for character pyramid C program

Number Pyramid

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

1234
341
12
3

Ans.

/*c program for number pyramid*/
#include<stdio.h>
int main()
{

 int num=4,n,c,r,t;
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  if(r==2 || r==4)
  {
    for(c=1,t=3; c<=n; c++,t++)
    {
      if(c==3 && r==2)
         printf("1");
      else
         printf("%d",t);
    }
  }
  else
  {
    for(c=1; c<=n; c++)
        printf("%d", c);
  }
  printf("\n");
 }
 return 0;
}

The output of above program would be:


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

Sunday, 2 December 2012

Character Rectangle Design

Q. Write a C program to print the following character rectangle structure:

ABCBA
AB BA
A   A
AB BA
ABCBA

Ans.

Cross-Reference: Before you reading answer of above program, i recommend to read Number Rectangle Design for easily understand this program.

/*c program for character rectangle pyramid*/
#include<stdio.h>
int main()
{

 char ch='C',r,c,q,t,sp,st;
 st=ch;
 for(r='A'; r<=ch; r++,st--)
 {
  for(c='A';c<='B'; c++)
  {
    if(r=='C')
    {
      printf("A ");
      break;
    }
    else
      printf("%c",c);
  }
  for(sp=r; sp>'A'; sp--)
    printf(" ");
  for(t=st; t>='A'; t--)
    printf("%c",t);
  printf("\n");
 }
 for(r='A'; r<=ch-1; r++)
 {
  for(c='A'; c<='B'; c++)
     printf("%c", c);
  for(sp=r; sp<='A'; sp++)
     printf(" ");
  for(t=r+1; t>='A'; t--)
     printf("%c", t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of character rectangle C program
Figure: Screen shot for character rectangle structure C program

Star Triangle Pyramid

Q. Write a C program to print the following star pyramid structure:

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

Ans.

/*c program for star pyramid design*/
#include<stdio.h>
int main()
{

 int n=9,r,c;
 for(r=5; r>=1; r--,n=n-2)
 {
   for(c=1; c<=n; c++)
      printf("*");
   printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of star triangle C program
Figure: Screen shot for star triangle C program


Number Rectangle Design

Q. Write a C program to print the following number rectangle design :

12321
12 21
1   1
12 21
12321

Ans.

/*c program for number rectangle design*/
#include<stdio.h>
int main()
{

 int num,n,r,c,sp,q,t;
 printf("Enter any number : ");
 scanf("%d", &num);
 n=num;
 for(r=1; r<=num; r++,n--)
 {
  for(c=1; c<=2; c++)
  {
   if(r==3)
   {
     printf("1 ");
     break;
   }
   else
     printf("%d",c);
  }
  for(sp=r; sp>1; sp--)
     printf(" ");
  for(t=n; t>=1; t--)
     printf("%d",t);
  printf("\n");
 }
 for(r=1; r<=num-1; r++)
 {
  for(c=1; c<=2; c++)
     printf("%d",c);
  for(sp=r; sp<=1; sp++)
     printf(" ");
  for(t=r+1; t>=1; t--)
     printf("%d",t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


Output of number rectangle C program
Figure: Screen shot for number rectangle C program


Number Triangle Program

Q. Write a C program to print the following number triangle design:

1
232
34543
4567654

Ans.

/*c program for number triangle design*/
#include<stdio.h>
int main()
{

 int num,r,c,s,t,q;
 printf("Enter any number : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=r, s=r; c>=1; c--,s++)
     printf("%d", s);
  for(q=r-1,t=s-2; q>=1; q--,t--)
     printf("%d", t);
  printf("\n");
 }
 return 0;
}

The output of above program would be:


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