Home > Blockchain >  Displaying multiplied number in asterisks (in C)
Displaying multiplied number in asterisks (in C)

Time:11-06

Problem 4

I'm new to Loops and I'm trying to understand how will I be able to display those asterisks, such that each row will have its respective number of asterisks.

I've tried doing this, but I'm having a hard time with what to do next.

The problem is that the output only shows 3 asterisks for each row instead expected output on the first image. output problem

void displayAst (int b) {
    
    for (int i = 1; i <= b ; i  ) {
        printf ("*");
    }
}

void displayMulti (int a) {
    

    for (int i = 1; i <= 10 ; i  ) {
        
        printf ("\n%d x %d = ", a, i);

        displayAst (a); 
        
            }
    }

int main () {

    int nNum;

    printf ("Enter Number: "); 
    scanf ("%d", &nNum);

    displayMulti (nNum);
    
    
    return 0;
}

CodePudding user response:

At yout displayMulti() method, where you are calling displayMulti(a).The method should be displayMulti(a*i). Solution:

 void displayAst (int b) {
        
        for (int i = 1; i <= b ; i  ) {
            printf ("*");
        }
    }
    
    void displayMulti (int a) {
        
    
        for (int i = 1; i <= 10 ; i  ) {
            
            printf ("\n%d x %d = ", a, i);
    
            displayAst (a*i); 
            
                }
        }
    
    int main () {
    
        int nNum;
    
        printf ("Enter Number: "); 
        scanf ("%d", &nNum);
    
        displayMulti (nNum);
        
        
        return 0;
    }

CodePudding user response:

Your mistake is at the following line

displayAst (a);

you are always passing a; which in your case is the input number from the user. i.e. 3. Instead the method should be

displayAst (a*i);

The number of stars that must be printed after the multiplication.

I know you are beginning to code. If you start using a debugger, you can yourself solve these issues. Plus a debugger would give you insight how your code works.

  • Related