write a program to calculate sum of 1 to 10 numbers using while ?
Answers
Answered by
1
counter = 1
_sum = 0
while counter < 11:
_sum += counter
counter += 1
print("sum", _sum)
Answered by
1
Required Answer:-
Question:
- Write a program to calculate sum of 1 to 10 using while loop.
Solution:
Here is the code for the problem.
In python.
s,i=0,1
while i<=10:
s,i=s+i,i+1
print("Sum is: ",s)
In Java.
public class Sum {
public static void main(String[] args) {
int s=0, i=1;
while(i<=10)
s+=i++;
System.out.println("Sum is: "+s);
}
}
After each iteration, value of i is added to s. Sum of first 10 numbers is stored in s. It is now displayed on the screen.
Output is attached for verification.
Attachments:
![](https://hi-static.z-dn.net/files/d57/2a45b352339a97ecc4e9c12880fab7f9.jpg)
![](https://hi-static.z-dn.net/files/dfb/697f8cafe63a5b619a2d606e700e2003.jpg)
![](https://hi-static.z-dn.net/files/d52/ced589f89aadd2ceb081baf5f22fca92.jpg)
![](https://hi-static.z-dn.net/files/db9/121bd95736f68956d412e09fc3f4bf36.jpg)
Similar questions