C Tutorial

Draw box in c program

Draw box in c program

In c program, lets draw the box using for loop with symbol '#'.

#include<tdio.h>

main()
{
 int count=0, rows=10;
 printf("Enter number of rows: ");
 scanf("%d", &rows);
 //Top Line
 printf("\n#############################");
 for(count=1;count<=rows;count++)
 {
   //Left and Right Lines
   printf("\n#                       	#");  
 }
 //Bottom line
 printf("\n##############################");
}

Output:

$ gcc for_loop_draw_box.c
for_loop_draw_box.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main()
 ^~~~
$ ./a.out
Enter number of rows: 15

#############################
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
##############################
$ ./a.out
Enter number of rows: 12

#############################
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
#                       	#
##############################

Lets use * instead of # symbol,

#include<stdio.h>

main()
{
 int count=0, rows=10;
 printf("Enter number of rows: ");
 scanf("%d", &rows);
 printf("\n***********************");
 for(count=1;count<=rows;count++)
 {
   printf("\n*                 	*");
 }
 printf("\n************************");
}

Output:

$ gcc for_loop_draw_box.c
for_loop_draw_box.c:3:1: warning: return type defaults to ‘int’ [-Wimplicit-int]
 main()
 ^~~~
$ ./a.out
Enter number of rows: 12

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



Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^