C Programming

C Programming


Star Pyramid

Posted: 17 Apr 2013 09:24 AM PDT

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

Posted: 17 Apr 2013 08:27 AM PDT

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

Posted: 17 Apr 2013 08:05 AM PDT

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

Posted: 17 Apr 2013 07:26 AM PDT

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


Post a Comment