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;
}

71 comments:

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

    ReplyDelete
    Replies
    1. #include

      int main()
      {
      int ch=97,i,n,j,k,z;
      printf("Enter the number of lines");
      scanf("%d",&n);
      for(i=0,z=1;i<n/2;i++)
      {
      for(j=0;j<i;j++)
      {
      printf("%c ",ch);
      }
      printf("\n");
      for(k=0;k<j;k++,z++)
      {
      printf("%d ",z);
      }
      printf("\n");
      }

      return 0;
      }

      Delete
  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
    Replies
    1. #include

      int main()
      {
      int i,j,k,n;
      printf("Enter the no.of lines");
      scanf("%d",&n);
      for(i=0;i<n;i++)
      {
      for(j=0;j<i;j++)
      {
      printf("*");
      }
      printf(" ");
      for(k=0;k<i;k++)
      {
      printf("*");
      }
      printf(" ");
      printf("\n");
      }
      return 0;
      }

      Delete
  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
    4. int i,j;
      for(;i++<10;printf("\n"))for(j=0;j++<10;i!=j&&i+j!=11?printf(" "):printf("*"));

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

    ReplyDelete
    Replies
    1. With prompting of size

      #include
      int main(){
      char a[3];int i,j,k;
      while(!k){
      fgets(a, sizeof a, stdin);
      sscanf(a, "%2d", &k);
      } //input part done
      for (;i++<k;printf("\n")) for (j=0;j++<k;) printf("%c", (i+j)%2?'*':' ');
      }

      Delete
  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
    Replies
    1. assume a number under 2 digits
      #include
      int main(){
      char a[3];int i,j,k;
      while(!k){
      fgets(a, sizeof a, stdin);
      sscanf(a, "%2d", &k);
      } //input part done
      for (;i++k-i&&j<k+i?'*':' ');
      }

      Delete
  25. pls give solution for ths
    write a program to print the following output

    1
    6 2
    10 7 3
    13 11 8 4
    15 14 12 9 5

    ReplyDelete
    Replies
    1. plot some functions.

      #include
      int main(){
      char a[3];int i,j,k,O;
      while(!k){
      fgets(a, sizeof a, stdin);
      sscanf(a, "%d", &k);
      } //input part done
      for (;i++<k;printf("\n\n")) for (j=0;j++<k;O<0?:printf("%d ",O)) O=j-1-k+((2*k-(i-j)+2)*(i-j+1))/2;
      }

      Delete
  26. Please anyone explain this program how it works ..
    #include

    int main()
    {
    int i,n,j,k;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
    for(k=n;k>i;k--)//for space print.//
    {
    printf(" ");
    }

    for(j=1;j<=10;j++)//for number print in every row//
    {
    if(j<i)
    continue;
    if(j==2*i-1)
    printf("%d",j);
    else
    if(j<2*i-1)
    printf("%d ",j);
    else break; }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  27. Can anyone tell me how to display X-shape pattern using nested loops in C (not C++). ASAP

    ReplyDelete
  28. Plz give me this pattern answer
    *
    **
    ***
    **
    *

    ReplyDelete
  29. Plz give me this pattern answer
    *
    **
    ***
    **
    *

    ReplyDelete
  30. Plz give me this pattern answer
    *
    **
    ***
    **
    *

    ReplyDelete

  31. Plz give me this pattern answer
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************
    ************

    ReplyDelete
  32. Plz give me answer of this pattern

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

    ReplyDelete
  33. 1 1
    2 2
    3 3
    4
    3 3
    2 2
    1 1
    how can I print these No in X pattren

    ReplyDelete
  34. 1 1
    2 2
    3 3
    4
    3 3
    2 2
    1 1
    how can I print these No in X pattren

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

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

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

    ReplyDelete
  38. SSSSS
    S. S
    S. S
    S. S
    SSSSS
    I NEED A PROGRAM FOR THIS OUT PUT.PLESE

    ReplyDelete
  39. SSSSS
    S. S
    S. S
    S. S
    SSSSS
    I NEED A PROGRAM FOR THIS OUT PUT.PLESE

    ReplyDelete
  40. Replies
    1. int main()
      {
      int i,j,n;
      printf("enter number ");
      scanf(" %d",&n);
      for(i=1;i<=n;i++)
      {
      printf ("\n%d\n",i);
      for(j=1;j<=i;j++)
      printf("%2d", j);
      }
      return 0;
      }

      Delete
    2. int main()
      {
      int i,j,n;
      printf("enter number ");
      scanf(" %d",&n);
      for(i=1;i<=n;i++)
      {
      printf ("\n%d\n",i);
      for(j=1;j<=i;j++)
      printf("%2d", j);
      }
      return 0;
      }

      Delete
  41. I want c code for the following output , if am give input 15,plz help me


    7 6 5
    8 1 4 15
    9 2 3 14
    10 11 12 13

    ReplyDelete
  42. I want c code for the following output , if am give input 15,plz help me


    7 6 5
    8 1 4 15
    9 2 3 14
    10 11 12 13

    ReplyDelete
  43. Write the class named parallelogram which has a function display() that is started below. A call of parallelogram::display (6, 5) produces a parallelogram with 6 rows of 5 stars in the following shape:
    *****
    *****
    *****
    *****
    *****
    *****
    void parallelogram ::display (int iRows, int iCols)
    Any One?????????

    ReplyDelete
    Replies
    1. #include
      using namespace std;

      class parallelogram {
      private:
      //member Atributes
      public:
      void display(int, int);
      };
      void parallelogram::display(int row, int col) {
      for (int i = 0; i < row; i++)
      {
      for (int j = 0; j < col; j++) {
      cout << "*";
      }
      cout << endl;
      }
      }


      void main() {
      parallelogram object;
      object.display(5, 12);
      }

      for more visit and join site
      www.cppexamples.blogspot.com

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

    ReplyDelete
  45. a program that accepts a number any integer less than 1 and has 8 digits maximum

    example

    Please enter a number:12345

    1
    12
    123
    1234
    12345
    2345
    345
    45
    5

    ReplyDelete
  46. I need this programm:

    https://moodle.hitsa.ee/pluginfile.php/1112932/mod_assign/intro/v.jpg

    How to do you this? I am beginner in C-language

    ReplyDelete
  47. for more shapes code visit :)

    http://cppexamples.blogspot.com/p/c-shapes-code.html

    ReplyDelete