C program to print different shapes using printf function


Different shapes by printf function in c

1
Write a c program to print following shape.
*****
*****
*****
Answer
#include<stdio.h>
void main(){
    int i,j;
  
    for(i=0;i<5;i++){
       for(j=0;j<5;j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

2
Write a c program to print following shape.
*
**
***
****
*****
******
*******
Answer
#include<stdio.h> 
int main(){
    int i,j;
    for(i=0;i<8;i++){
       for(j=0;j<i;j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

3
Write a c program to print following shape.
**
****
******
********
**********
************
Answer
#include<stdio.h>  
int main(){
    int i,j;
    for(i=0;i<8;i++){
       for(j=0;j<=2*i-3;j++){
           printf("*");
}
         printf("\n");
    }
    return 0;
}

4
Write a c program to print following shape.
*
***
*****
*******
*********
Answer
#include<stdio.h> 
int main(){
    int i,j;
    
    for(i=0;i<6;i++){
       for(j=0;j<=2*i-2;j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

5
Write a c program to print following shape.
*
****
*******
**********
Answer
#include<stdio.h> 
int main(){
    int i,j;
    
    for(i=0;i<5;i++){
       for(j=0;j<3*i-2;j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

6
Write a c program to print following shape.
******
*****
****
***
**
*
Answer
#include<stdio.h> 
int main(){
    int i,j;
    
    for(i=0;i<6;i++){
       for(j=0;j<6-i;j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

7
Write a c program to print following shape.
********
******
****
**
Answer
#include<stdio.h> 
int main(){
    int i,j;
    
    for(i=0;i<6;i++){
       for(j=0;j<8-2*i;j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

8
Write a c program to print following shape.
*********
*******
*****
***
*
Answer

#include<stdio.h> 

int main(){
    int i,j;
    
    for(i=0;i<6;i++){
       for(j=0;j<=8-2*i;j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

9
Write a c program to print following shape.
**
****
********
****************
Answer
#include<stdio.h> 
#include<math.h>
int main(){
    int i,j;
    
    for(i=0;i<4;i++){
       for(j=0;j<2*pow(2,i);j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

10
Write a c program to print following shape.
*
**
****
********
Answer
#include<stdio.h> 
#include<math.h>
int main(){
    int i,j;
    
    for(i=0;i<4;i++){
       for(j=0;j<pow(2,i);j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

11
Write a c program to print following shape.
*
***
*********
***************************
Answer
#include<stdio.h> 
#include<math.h>
void main(){
    int i,j;
    
    for(i=0;i<4;i++){
       for(j=0;j<pow(3,i);j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

12
Write a c program to print following shape.
***
*********
Answer
#include<stdio.h> 
#include<math.h>
int main(){
    int i,j;
    for(i=0;i<3;i++){
       for(j=0;j<3*pow(3,i);j++){
           printf("*");
       }
       printf("\n");
    }
    return 0;
}

13
Write a c program to print following shape.
 *
 * *
 * * *
 * * * *
Answer
#include<stdio.h> 
#include<math.h>
int main(){
    int i,j;
    char c='*';
    
    for(i=0;i<5;i++){
       for(j=0;j<i;j++){
           printf("%*c",2,c);
       }
       printf("\n");
    }
    return 0;
}

14
Write a c program to print following shape.
*
**
***
****
Answer
#include<stdio.h> 
#include<math.h>
int main(){
    int i,j;
    char * c="*******";
    
    for(i=0;i<5;i++){
       printf("%*.*s\n",i,i,c);
    }
    return 0;
}

15
Write a c program to print following shape.
      *
      **
      ***
      ****
Answer

#include<stdio.h> 
#include<math.h>
int main(){
    int i,j;
    char * c="*******";
    
    for(i=0;i<5;i++){
       printf("%*.*s\n",i+6,i,c);
    }
    return 0;
}

16

Write a c program to print following shape.
      *
     **
    ***
   ****
  *****
 ******
*******
Answer

#include<stdio.h> 
int main(){
    char *ptr="*******";
    int i,j;
    
    for(i=0;i<8;i++){
       printf("%*.*s\n",8,i,ptr);
    }
    return 0;
}

17

Write a c program to print following shape.
    *
   ***
  *****
 *******
*********
Answer

#include<stdio.h> 
int main(){
    char *ptr="*********";
    int i,j;
    
    for(i=0;i<5;i++){
       printf("%*.*s\n",5+i,2*i+1,ptr);
    }
    return 0;
}

18

Write a c program to print following shape.
    *
   ***
  *****
 *******
*********
*********
 *******
  *****
   ***
    *
Answer

#include<stdio.h> 
int main(){
    char *ptr="*********";
    int i,j;
    
    for(i=0;i<10;i++){
       if(i<5)
         printf("%*.*s\n",5+i,2*i+1,ptr);
       else
         printf("%*.*s\n",14-i,19-2*i,ptr);
    }
    return 0;
}

19

Write a c program to print following shape.
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
Answer

#include<stdio.h> 
int main(){
    char *ptr="*********";
    int i,j;
    
    for(i=0;i<9;i++){
       if(i<5)
         printf("%*.*s\n",5+i,2*i+1,ptr);
       else
         printf("%*.*s\n",13-i,17-2*i,ptr);
    }
    return 0;
}

20

Write a c program to print following shape.
 *********
 *******
  *****
   ***
    *
Answer

#include<stdio.h> 
int main(){
    char *ptr="*********";
    int i,j;
    
    for(i=0;i<5;i++){
       printf("%*.*s\n",9-i,9-2*i,ptr);
    }
    return 0;
}

21

Write a c program to print following shape.
    *
   **
  ***
 ****
*****
 ****
  ***
   **
    *
Answer

#include<stdio.h> 
int main(){
    char *ptr="*****";
    int i,j;
    
    for(i=0;i<10;i++){
       if(i<6)
         printf("%*.*s\n",5,i,ptr);
       else
         printf("%*.*s\n",5,10-i,ptr);
    }
    return 0;
}


22

Write a c program to print following shape.
*
**
***
****
*****
****
***
**
*
Answer

#include<stdio.h>  
int main(){
    char *ptr="*****";
    int i,j;
    
    for(i=1;i<10;i++){
       if(i<6)
         printf("%*.*s\n",0,i,ptr);
       else
         printf("%*.*s\n",0,10-i,ptr);
    }
    return 0;
}

23

Write a c program to print following shape.
    *
   ***
  *****
 *******
*********
*********
*********
***   ***
***   ***
***   ***
***   ***
Answer

#include<stdio.h> 
int main(){
    char *ptr="*********";
    int i,j;
    
    for(i=0;i<11;i++){
       if(i<5)
           printf("%*.*s\n",5+i,2*i+1,ptr);
       else{
           if(i==7){
               *(ptr+3)=' ';
               *(ptr+4)=' ';
               *(ptr+5)=' ';
           }
           printf("%*.*s\n",9,9,ptr);
       }
    }
    return 0;
}

36 comments:

  1. pls give us some examples regarding pyramids..such as
    a
    1 2
    b c d
    3 4 4 6

    ReplyDelete
  2. #include
    int main(){
    int i,j;
    i=0;

    while(i<5);
    {
    printf("*");

    }
    j=0;
    while(j<5);
    { printf("*");
    j++;
    }
    i++;
    return 0;
    } what abt this

    ReplyDelete
  3. in this lesson Question 9,10 answers r nt working.. compiler tells that undefined reference to `pow' .. how we should define that pow in C?????

    ReplyDelete
    Replies
    1. Use header in the program

      Delete
    2. I think you are using Linux gcc compiler. If yes, I hope this link will help you:
      http://cquestionbank.blogspot.com/2010/07/undefined-reference-to-pow-collect2-ld.html

      Delete
    3. it is too easy to write c coding for different shapes like circle etc
      but how you can write a c code for circle and display it by using function
      getch(); or getche();

      Delete
  4. Hi s.m. dilhari,

    I think you are using Linux gcc compiler. If yes, I hope this link will help you:
    http://cquestionbank.blogspot.com/2010/07/undefined-reference-to-pow-collect2-ld.html

    ReplyDelete
  5. can you possibly give a way to print a sideways diamond?

    ReplyDelete
  6. write a program for
    * *
    ** **
    *** ***
    *******

    ReplyDelete
  7. how to print a heart using asterix(*)

    ReplyDelete
  8. write a programm that this is the output:
    000001
    000010
    000100
    001000
    010000
    100000

    ReplyDelete
    Replies
    1. int main()
      {
      int i,j,n=6;
      clrscr();
      for(i=1;i<=n;i++)
      {
      for(j=1;j<=n;j++)
      {
      if((i+j)==(n+1))
      printf("1");
      else
      printf("0");
      }
      printf("\n");
      }
      getch();
      return 0;
      }

      Delete
    2. #include
      #include
      void main()
      {
      int i,j;
      int k=5;
      for(i=0;i<6;i++)
      {for(j=0;j<6;j++)
      {
      if(j!=k)
      printf("0");
      else {printf("1");
      k--;}
      }printf("\n");}
      }

      Delete
  9. Please help me to WRITE A PROGRAM SIMILAR TO THE PRIME NUMBER TESTER,EXCEPT THAT IT DISPLAY'S ALL THE FACTORS OF A NUMBER ENTERED BY THE USER,FOR EXAMPLE IF A USER ENTERED IT WOULD RESPONSE WITH 2 AND 4

    ReplyDelete
  10. sir in 3 rd question ur answer is wrong put i=2

    after run program..


    regards tanuj
    for any query email me (tanujkhurana83@gmail.com)

    ReplyDelete
  11. pls send me de solution manual of computer science :A structured programming approach using c by Behrouz A Forouzan as soon as possible to my id surabhisreess@gmail.com

    ReplyDelete
  12. i didnt get this expression ,plz explain it clearly..
    for(i=0;i<5;i++){
    printf("%*.*s\n",i,i,c);

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. pls give solution for ths
    write a program to print the following output
    1
    2 2
    3 3 3
    4 4 4 4
    5 5 5 5 5

    ReplyDelete
    Replies
    1. main()
      {
      for (i=1;i<=5;i++){
      for (j=1;j<=i;j++)
      printf("%d ",i);
      printf("%s","\n");
      }
      }

      Delete
  15. int main()
    {
    int i,j;
    for(i=1;i<=5;i++){
    for(j=1;j<=i;j++){
    printf("%d",i);
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  16. many of programs are not execute.......

    ReplyDelete
  17. # # #
    ####
    # #
    code for this program ???

    ReplyDelete
  18. pls give solution for ths
    write a program to print the following output
    1
    3 2
    4 5 6
    10 9 8 7

    ReplyDelete
    Replies
    1. #include
      int i, j, k;

      main()
      {
      k = 0;
      for (i=1;i<=4;i++){
      k+=i;
      if (i%2) {
      for (j=k-i+1;j<=k;j++)
      printf("%d ",j);
      } else {
      for (j=k;j>=k-i+1;j--)
      printf("%d ",j);
      }
      printf("%s","\n");
      }
      }

      Delete
  19. int main ()
    {
    int n=16,i,j,k;
    for (i=1,k=1;i<=n;i++,k++)
    {
    for (j=1;j<=i+k;j++)
    {
    printf ("* ");
    }
    printf ("\n");
    }
    }

    ReplyDelete
    Replies
    1. * *
      * * * *
      * * * * * *
      * * * * * * * *
      * * * * * * * * * *
      * * * * * * * * * * * *
      * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

      Delete
    2. #include
      #include
      void main()
      {
      int i,j;
      clrscr();
      for(i=1;i<=16;i++)
      {
      for(j=1;j<=2*i;j++)

      printf("*");
      printf("\n");

      }
      getch();
      }






      Delete
  20. This comment has been removed by the author.

    ReplyDelete
  21. how to print an E shaped program in c

    ReplyDelete
    Replies
    1. Short version:

      #include
      int i;

      main()
      {
      for (i=1;i<=5;i++){
      if (i/2*2!=i)
      printf("%s","*****");
      else
      printf("%s","*");
      printf("%s","\n");
      }
      }

      Long Version (bigger)

      #include
      int i, j;

      main()
      {
      for (i=1;i<=10;i++){
      if ((i-1)%4<=1){
      for (j=1;j<=8;j++)
      printf("%s","*");
      } else {
      for (j=1;j<=2;j++)
      printf("%s","*");
      }
      printf("%s","\n");
      }
      }

      Complex versoin:

      #include
      int i, j;

      main()
      {
      for (i=1;i<=10;i++){
      switch (i){
      case 1:
      case 10:
      for (j=1;j<=10;j++)
      printf("%s","*");
      break;
      case 2:
      case 9:
      printf("%s"," *");
      for (j=1;j<=7;j++)
      printf("%s"," ");
      printf("%s","*");
      break;
      case 5:
      case 6:
      printf("%s"," ");
      for (j=1;j<=8;j++)
      printf("%s","*");
      break;
      default:
      printf("%s"," *");
      break;
      }
      printf("%s","\n");
      }
      }

      Delete
    2. This comment has been removed by the author.

      Delete
    3. how to print X shaped program

      Delete
  22. How about a Checkerboard effect using * in C

    ReplyDelete
  23. This comment has been removed by the author.

    ReplyDelete
  24. Hi,

    If I take n as input from user what will be the code?
    The shape same as answer no. 17 .How should I write the code ?

    Thanks

    ReplyDelete