Hassen Fatima
Hassen Fatima

Reputation: 423

How to print n numbers of * using a loop in C?

I am trying to create a program which prints 6 * 10 times using a for loop, but it does not work correctly.

#include <stdio.h>

int main()
{
    int i, num1 = 60, num2;
    num2 = 60 / 10;
    for (i=0;i<10;i++) {
        printf("%d* \n", num2);
    }
}

This prints 6* ten times, but I want to print 6 six 10 times like this

******
******
******
......

Upvotes: 1

Views: 281

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

What you are doing is took a loop and print same int with a star.

If you need to print 2D grid, you have to use nested loop.

Like this

int i, j, num1 = 60, num2;
num2 = 60 / 10;
for (i = 0; i < 10; i++) {
    for (j = 0; j < num2; j++)
        printf("*");
    printf("\n");
}

IDEONE

Upvotes: 4

Related Questions