Sunday, April 21, 2013

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, April 18, 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, April 17, 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

Reverse Number Pyramid

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

10 9 8 7
6  5 4
3  2
1

Ans.

/*c program for reverse number pyramid*/
#include<stdio.h>
int main()
{
 int n=4,num,r,c;
 static int p=10;
 num = n;
 for(r=1; r<=n; r++,num--)
 {
   for(c=1; c<=num; c++)
   {
     printf("%d ",p);
     p--;
   }
   printf("\n");
 }
 getch();
 return 0;
}

The output of above program would be:

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

Thursday, April 4, 2013

Number star pyramid

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

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

Ans.

/* c program for number star pyramid */
#include<stdio.h>
int main()
{
 int num=6,r,c;

 for(r=1; r<=num; r++)
 {
   for(c=1; c<=r; c++)
   {
      if(r%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