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:
Similar questions