Sunday, December 9, 2012

Number Pryamid By Function

Q. Write a C program to print the following triangle using function.

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

Ans.

/*C program for number pyramid using function*/
#include<stdio.h>
void pyramid(int );
int main()
{
 int num;
 printf("Enter the number of rows : ");
 scanf("%d", &num);
 pyramid(num);
 return 0;
}

void pyramid(int n)
{
 int r,c,x=1,y=1;
 for(r=1; r<=n; r++)
 {
  for(c=1; c<=r; c++,x++)
  {
    if(x<=9)
       printf(" %d",x);
    else
    {
       printf(" %d",y);
       y++;
    }
  }
  printf("\n");
 }
}

The output of above program would be:

Output of create number pyramid using function C program
Figure: Screen shot for number pyramid
using function C program

Monday, December 3, 2012

Character Rectangle Program


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

ABCD
BCDA
CDAB
DABC

Ans.

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

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

{
 char ch,r,c,t;
 printf("Enter any character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
    ch = ch - 32;
 for(r='A'; r<=ch; r++)
 {
  for(c=r; c<=ch; c++)
     printf("%c",c);
  for(t='A'; t<r; 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 pyramid C program

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, December 2, 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