Sunday, 21 April 2013

Differ Symbol Pyramid

Q. write C program to print the following symbol pyramid as:

*
$$
***
$$$$

Ans.

/*c program for symbol pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1; c<=r; c++)
   {
     if(r%2==0)
        printf(" $");
     else
        printf(" *");
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


Output of differ symbol pyramid C program
Figure: Screen shot for differ-symbol pyramid C program

Character - Symbol Pyramid

Q. Write a C program to print the following character and symbol design:

A
**
ABC
****
ABCDE
****
ABC
**
A

Ans.

/*c program for character and star pyramid*/
#include<stdio.h>
int main()
{
 char ch,r,c,q;
 printf("Enter ending character : ");
 scanf("%c", &ch);
 if(ch>='a' && ch<='z')
    ch=ch-32;
 for(r='A'; r<=ch; r++)
 {
  for(c='A'; c<=r; c++)
  {
    if(r%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 for(q=ch-1; q>='A'; q--)
 {
  for(c='A'; c<=q; c++)
  {
    if(q%2==0)
       printf("*");
    else
       printf("%c",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:
Output of character symbol pyramid C program
Figure: screen shot for character star pyramid C program

-------------------------------------------


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

1
**
123
****
12345
****
123
**
1

Ans.

/*c program for number and star pyramid*/
#include<stdio.h>
int main()
{
 int num,r,c,n;
 printf("Enter ending number : ");
 scanf("%d"&num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if(r%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 for(n=num-1; n>=1; n--)
 {
  for(c=1; c<=n; c++)
  {
    if(n%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:


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

Odd Number Triangle

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

1
1 3
1 3 5
1 3 5 7
1 3 5 7 9

Ans.

/*c program for odd number triangle*/
#include<stdio.h>
int main()
{
 int num,r,c,z;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1,z=1; c<=r; c++,z=z+2)
       printf("%d ",z);
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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

Thursday, 18 April 2013

Simple Star Pyramid

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

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

Ans.

/*c program for simple star pyramid*/
#include<stdio.h>
int main()
{
 int num=5,n=9,r,c;
 for(r=1; r<=num; r++,n=n-2)
 {
   for(c=1; c<=n; c++)
       printf("*");
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of simple star pyramid C program
Figure: Screen shot for simple star pyramid C program

Wednesday, 17 April 2013

Star Pyramid

Q. print the following star structure as:

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

Ans.

/*c star pyramid program*/
#include<stdio.h>
int main()
{
 int num,n,r,c,z;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 n=num;
 for(r=1; r<num; r++,n--)
 {
  for(c=1; c<=n; c++)
     printf("*");
  printf("\n");
 }
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
      printf("*");
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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

Even Odd Number Star Pyramid

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

1
*2
1*3
*2*4
1*3*5
*2*4*6

Ans.

/*odd even number star pyramid C program*/
#include<stdio.h>
int main()
{
 int num,r,c,z;
 printf("Enter no. of rows : ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
   for(c=1,z=r; c<=r; c++,z--)
   {
     if(z%2==0)
        printf("%d",c);
     else
        printf("*");
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of odd even number star pyramid C program
Figure: Screen shot for odd even number star pyramid C program

Number Star Pyarmid

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

1
1*
1*3
1*3*
1*3*5
1*3*5*

Ans.

/*number star c pyramid program*/
#include<stdio.h>
int main()
{
 int num,r,c;
 printf("Enter no. of rows: ");
 scanf("%d", &num);
 for(r=1; r<=num; r++)
 {
  for(c=1; c<=r; c++)
  {
    if(c%2==0)
       printf("*");
    else
       printf("%d",c);
  }
  printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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