First when the program starts it askes to enter a number, in this case the entered number n is 10, then initializing variable sum =0 and uses variable i as counter from 1 to n.
Then the program will check if i <=n when it yes it will add i value to the sum and when it is no it will display the sum of numbers from 1 to 10.
So at first for i=1 it will check is 1 <= 10 ? it is yes, so 1 will be added to sum which was zero so the new sum= 1+0 =1 ,then i will be incremented by 1 so new become i =2.
Then again check is 2 <=10 ? it is yes, so 2 will be added to sum which was equal to 1 so the new sum= 1+2 =3, then i will be incremented by 1 so new become i =3.
Then again check is 3 <=10 ? it is yes, so 3 will be added to sum which was equal to 3 so the new sum= 3+3 =6 And so on till i=11 then check is 11 <=10? it is no, then the program will display the sum (1+2+3+4+5+6+7+8+9+10=55)and stops.
Output:
55
Explanation:
First when the program starts it askes to enter a number, in this case the entered number n is 10, then initializing variable sum =0 and uses variable i as counter from 1 to n.
Then the program will check if i <=n when it yes it will add i value to the sum and when it is no it will display the sum of numbers from 1 to 10.
So at first for i=1 it will check is 1 <= 10 ? it is yes, so 1 will be added to sum which was zero so the new sum= 1+0 =1 ,then i will be incremented by 1 so new become i =2.
Then again check is 2 <=10 ? it is yes, so 2 will be added to sum which was equal to 1 so the new sum= 1+2 =3, then i will be incremented by 1 so new become i =3.
Then again check is 3 <=10 ? it is yes, so 3 will be added to sum which was equal to 3 so the new sum= 3+3 =6 And so on till i=11 then check is 11 <=10? it is no, then the program will display the sum (1+2+3+4+5+6+7+8+9+10=55)and stops.
need an explanation for this answer? contact us directly to get an explanation for this answer