Skip to content

Chapter 5 Practice Set Problem 7: Implementing as a fucntion. #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions Chapter 5 - Practice Set/07_problem7.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
// Write a program using function to print the following pattern (first n lines)
#include <stdio.h>

int main(){
int n = 3;
for (int i = 0; i < n; i++)
void star(int);
void star(int a)
{

for (int i = 0; i < a; i++)
{
// This loop runs from 0 to 2
// if i = 0 ---> print 1 star
// if i = 1 ---> print 3 stars
// if i = 2 ---> print 5 stars
// no_of_stars = (2*i+1)

// This for loop prints (2*i+1) stars
for(int j=0; j<2*i+1;j++){
for (int j = 0; j < ((2 * i) + 1); j++)
{
printf("*");
}

// This printf prints a new line
printf("\n");
}

}
// The loop runs from 0 to 2
// if i = 0 ---> print 1 star
// if i = 1 ---> print 3 stars
// if i = 2 ---> print 5 stars
// no_of_stars = (2*i+1)

// This for loop prints (2*i+1) stars
int main()
{
star(20);
return 0;
}
}