library > COP 2220C home > 4.7: Nested loops zyBooks catalog @ CHALLENGE 47.1: N
ID: 3750598 • Letter: L
Question
library > COP 2220C home > 4.7: Nested loops zyBooks catalog @ CHALLENGE 47.1: Nested loops Indent text. Print numbers 0,1,2,., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number Ex userNum 3 prints 1 #include 3 int main(void) ( 4 int userNum; 5 int i; 6 int 3: 8 userNum-3; 10 : /, Your solution goes here 12 return e; 13 > to searchExplanation / Answer
#include <stdio.h>
int main(void) {
int userNum;
int i;
int j;
userNum = 3;
for(i=0;i<=userNum;i++) // outer loop for printing number
{
printf("%d",i);
if(i<userNum)
{
printf(" ");
for(j=0;j<=i;j++) // inner loop for printing spaces
printf(" ");
}
}
return 0;
}
Output:
0
1
2
3
Do ask if any doubt. Please upvote.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.