Showing posts with label square. Show all posts
Showing posts with label square. Show all posts

Friday, April 11, 2014

Square Star Pyramid

Q. Write a C program to print the square star pyramid design as:

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

Ans.

/*c program for star square design*/
#include<stdio.h>
int main()
{
 int cols,rows,r,c,sp;
 printf("Enter no. of columns: ");
 scanf("%d", &cols);
 printf("Enter no. of rows: ");
 scanf("%d", &rows);
 for(r=1; r<=cols; r++)
 {
  if(r==1 || r==cols)
      printf(" ");
  else
      printf("*");
 }
 printf("
"
);

 for(c=1; c<=rows-2; c++)
 {
   printf("*");
   for(sp=1; sp<=cols-2; sp++)
       printf(" ");
   printf("*
"
);

 }
 for(r=1; r<=cols; r++)
 {
   if(r==1 || r==cols)
       printf(" ");
   else
       printf("*");
 }
 getch();
 return 0;
}

The output of above program would be:

Output of Square Star Pyramid C program
Figure: Screen shot for Square Star Pyramid C program
Read More..

Monday, April 7, 2014

Square Number Triangle

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

1
1 4 9
1 4 9 16 25
1 4 9 16 25 36 49
1 4 9 16 25 36 49 64 81

Ans.

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

 int r,c,q=1;
 for(r=1; r<=5; r++,q=q+2)
 {
  for(c=1; c<=q; c++)
     printf(" %d",c*c);
  printf("
"
);
 }
 getch();
 return 0;
}

/***********************************************************
The output of above program would be:
***********************************************************/

Output of square number triangle C program
Figure: Screen shot for square number triangle C program

Related Program:


1
1 2 3
1 2 3 4 5
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 
Read More..
 
Copyright 2009 Information Blog
Powered By Blogger