Help! (Beginner)

Joined
Nov 29, 2019
Messages
2
Reaction score
0
Hello. Could anyone tell me why does the code below not run properly? I intended to print a pyramid using * but the output is strange.
C:
#include <stdio.h>
int main ( )
{
    int i , j , rows ;


    printf("Enter the number of rows:");
    scanf("%d", &rows);

    for ( i = 1 ; i <= rows ; i++);
    {
        for ( j = 1 ; j <= rows - i ; j++)
            printf("  ");

        for ( j = 1 ; j <= 2 *i - 1 ; j++)
            printf("* ");

        printf("\n");
    }
}

For example, if I run this code and give "rows" a value of 5 it outputs 11 stars one after each other. Help me, please.
 
Joined
Jan 22, 2020
Messages
2
Reaction score
0
Hello aliplayer1,

Well, the output is strange due to the semicolon that you wrote in the first "for loop". If you quit that semicolon, the output will be the desired one.

Should be this way (without modifying the rest of parts of your code):

Code:
#include <stdio.h>
int main ( )
{
    int i , j , rows ;


    printf("Enter the number of rows:");
    scanf("%d", &rows);

    for ( i = 1 ; i <= rows ; i++)
    {
        for ( j = 1 ; j <= rows - i ; j++)
            printf("  ");

        for ( j = 1 ; j <= 2 *i - 1 ; j++)
            printf("* ");

        printf("\n");
    }
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,989
Messages
2,570,207
Members
46,782
Latest member
ThomasGex

Latest Threads

Top