Search code examples
cnested-loops

messed up with loops cant find the logical mistake


Actually right now i am learning c and doing an excercise of looping and got messed up in a question.

my code is:

#include<stdio.h>
void main()
{
  int i,j,k,spc,k;
   printf("\enter the number of rows:");
   scanf("%d",&rows);
   spc=rows+4-1;
   for(i=1;i<=rows;i++)
    { 
      for(k=spc;k>1;k--)
        { printf(" ");
         }
    for(j=1;j<=i:j++)
       printf("*");
    printf("\n")
    spc--;
    }
  }

https://www.w3resource.com/c-programming-exercises/for-loop/c-for-loop-exercises-14.php and this is the reference for the answer by them whose excercise i am doing right now. can you see any difference bw these codes. please help me. thank you


Solution

  • as i can see, you have small errors which you need to fix , first is, int i,j,k,spc,k;, here, 'k' is written twice, next is scanf("%d",&rows); but, rows is not declared anywhere, in this line,for(j=1;j<=i:j++), you missed a semicolon and added colon instead, so replace it with for(j=1;j<=i;j++) and the last one is, printf("\n") ,in this line, you missed a semicolon! and for the desired output, you just need to add a space in printf("*"); ,i.e, printf("* ");.Thats it.