How many times will the following loop get executed?
for x = 5 to 1
for y = 1 to x
print y
Answers
Answered by
0
Answer:
5
5555555555555555555
Answered by
0
Answer:
The loop gets executed by 15 times.
Explanation:
This is a nested loop where each inner loop is executed when a outer loop condition satisfies.
#include <stdio.h>
int main()
{
int x,y;
for (x=5;x>=1;x--)
{
for (y=1;y<=x;y++)
printf( "%d",y);
}
}
here when x=5 the y loop is executed 5 times.
then x=4; the y loop is executed 4 times.
then x=3; the y loop then executes 3 times.
then x=2; the y loop then executes 2 times
then x=1; the y loop then executes only 1 time.
∴The loop gets executed by 5 + 4 + 3 + 2 + 1 = 15 times.
In general, it will be the summation of value of x in this particular program.
Similar questions
English,
1 month ago
English,
1 month ago
Math,
2 months ago
Social Sciences,
10 months ago
Math,
10 months ago