Search code examples
cloopsfor-loop

How to convert number to a star symbol?


I want to write a simple application that writes 3 numbers into an array. The numbers should be read in from the keyboard with scanf. The following should be displayed on the screen: If the entered numbers are 1,5,4, the following will be displayed

*
*****
****

My problem stays only in,how to convert number to a star symbol.Thank you!

for (int i = 1; i <= 3; ++i) {
    printf("%d ",i);
    for (int j = 1; j <= i; ++j) {
        printf("*");
    }
    printf("\n");
}

Solution

  • Take this written in javascript, you should be able to convert it in C for your project :)

    The loops are the key, one to scan each element of the input array, and another to print as many * as the value of the array (1, 5 and 4)

    var input = [1, 5, 4]; // Let's assume you already have the input array
    var output = ""; // You don't need this
    
    for (var i = 0; i < input.length; i++) {
      for (j = 0; j < input[i]; j++) {
        output += "*"; // printf("*");
      }
      output += "\n"; // printf("\n");
    }
    
    console.log(output); // You don't need this