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

82 comments:

diya said...

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

s.m. dilhari said...

#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

s.m. dilhari said...

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?????

Priyanka kumari said...

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

Anonymous said...

Use header in the program

Priyanka kumari said...

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

dlucf27 said...

can you possibly give a way to print a sideways diamond?

bala said...

write a program for
* *
** **
*** ***
*******

Jojo said...

how to print a heart using asterix(*)

Anonymous said...

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

Anonymous said...

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

Anonymous said...

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

Unknown said...

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();

Unknown said...

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)

Anonymous said...

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

santosh said...

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

Anonymous said...

#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");}
}

Alok said...
This comment has been removed by the author.
Unknown said...

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

Unknown said...

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

Anonymous said...

many of programs are not execute.......

Anonymous said...

# # #
####
# #
code for this program ???

Anonymous said...

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

iFTEKHAR LIVE said...

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

Unknown said...
This comment has been removed by the author.
Unknown said...

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

Unknown said...

how to print an E shaped program in c

Logo said...

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

Logo said...

#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");
}
}

Logo said...

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

Unknown said...

#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();
}






Unknown said...
This comment has been removed by the author.
Unknown said...

how to print X shaped program

Unknown said...

How about a Checkerboard effect using * in C

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

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

Unknown said...

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

Logo said...

int i,j;
for(;i++<10;printf("\n"))for(j=0;j++<10;i!=j&&i+j!=11?printf(" "):printf("*"));

Logo said...

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?'*':' ');
}

Logo said...

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?'*':' ');
}

Logo said...

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

Unknown said...

remove spaces in actual diamond

Unknown said...

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

Unknown said...

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

Unknown said...

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

Unknown said...

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

Unknown said...

Plz give me this pattern answer
*
**
***
**
*

Unknown said...

Plz give me this pattern answer
*
**
***
**
*

Unknown said...

Plz give me this pattern answer
*
**
***
**
*

Unknown said...


Plz give me this pattern answer
************
************
************
************
************
************
************
************
************
************
************
************

Unknown said...

Plz give me answer of this pattern

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

zain rajpoot said...

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

zain rajpoot said...

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

Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...
This comment has been removed by the author.
Unknown said...

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

Unknown said...

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

Anonymous said...

1
1
2
12
3
123
4
1234

Anonymous said...

1
1
2
12
3
123
4
1234

Unknown said...

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

Unknown said...

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

Unknown said...

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

Unknown said...

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

Unknown said...

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?????????

Unknown said...
This comment has been removed by the author.
Unknown said...

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

Sander said...

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

Muhammad Iqbal said...

for more shapes code visit :)

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

Muhammad Iqbal said...

#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

srikanth reddy said...

If n = 4 the following output

1111

222

33

4

srikanth reddy said...

i need a programing for it

Unknown said...

how about

helloworld
helloworl
hellowor
hellowo
hellow
hello
hell
hel
he
h

thanks.

priyadharsini said...

Please..help me with this progam.im a beginner in c
Write a program to generate the given pattern.

CCCCC
S***S
S***S
S***S
KKKKK

Unknown said...

#include
int main()
{
int i,j,k;
char name[40];
printf("Enter the word");
gets(name);
k=strlen(name);

for(i=0;i<k;i++)
{
for(j=0;j<k-i;j++)
{

printf("%c",name[j]);



}
printf("\n");
}
return 0;
}

bhavesh sharma said...

How to make heart shape

bhavesh sharma said...

How to make heart shape

Unknown said...

how can i create cube "plz give me code"

Unknown said...

how to print this pattern
1
2 3

Unknown said...

or how to print
1
2 3 2
3 4 5 4 3
(upto n)

KICHU said...

plz give me ans for this pattern


AAAA AA
A A A
A A A
A A A
AAA A A
A A A
A A A
AAA AA

Prerna said...

i WANT TO PRINT THIS STRING "ERIC" IN FOLLOWING PATTERN

C
I C
R I C
E R I C