i am not able to code correctly and dont know how to print numbers br=efore stars
Pattern star and number 2
Hello, @Ayushi21,
Perform the following modification in your code.
- Place the code that is printing * outside of the inner for loop.
You must be thinking, why?
Do you think that the inner for loop has any impact on that code?
Does that execution of while loop depends upon the value of the value of j?
If you notice, after executing once, the value of s inside while loop becomes 0.
And there is no statement inside the inner for loop that modifies the value of s.
Thus, it does not depend upon the inner for loop.
Hence, write while loop after inner for loop, but inside outer for loop.
If you still have doubts, feel free to ask.
Give a like, if you are satisfied.
hey i understood that but i have one doubt left that how to print numbers according to the question. like i want the numbers to be print like 1…12…123 but in the output the numbers are like 1…21…321…https://ide.codingblocks.com/s/101505 – anyways this is the url of the code so can you please look at it.
That’s very simple:
Your code is printing the numbers in reverse order because you are executing the loop in the reverse order.
Change the order of execution and you problem will solve:
- Instead of iterating from j= i to 1, iterate for j=1 to i
- instead of decrementing the value of j, start incrementing it.
Hope, this would help.
Give a like, if you are satisfied.
and what i we have to print the same pattern in inverted pattern
yes,
Here is the modification:
for(j=1;j<=i;j++)
{
cout<<j;
}
Hope, this would help.
Give a like, if you are satisfied.