Understanding \n and tricks of for loop by drawing pattern!!

 TASK-

Write a C program to Generate following pattern:


\n is used to shift to new line : see the code:

Crack for pattern:

  • 1st and last line has full stars it mean a condition is required for 1st and last line.
  • Other than 1st and last line each line has two stars and some space between them. 

CODE:

#include<stdio.h> //star pattern

int main(){

int i,j,n=5; //Here n is taken 5 you may increase or take from user: n is number of rows

for (i=0;i<5;i++) //5 rows so 5 Iteration 

{

if (i==0 || i==4) // Because at start and end we find a full line of *

{

for (j=0;j<9;j++) //Here the stars of up and down line are 9 you may change

printf("*");

}

else

{

printf("*");

for (j=1;j<8;j++)

printf(" "); // Creating space between two stars 

printf("*");

}

printf("\n");

}

getchar();

return 0;

}

Expected Output:




NEED WORK PRODUCE WORK

My name is Abdul Rehman I am from Pakistan. I am doing BS in Computer and information sciences. Currently, I am creating these blogs to help students of computer sciences all over the world..

1 Comments

Post a Comment
Previous Post Next Post