write a program to display sum of all even numbers from 1 to 10
Answers
Answered by
1
Answer:
class sum
{
public static void main()
{int i,sum=0;
for(i=1;i<=10;i++)
if(i%2==0)
sum=sum+i;
System.out.println(+sum);
}
}
Answered by
3
The following codes are written using Python.
s = 0
for i in range(1, 10):
if i%2 == 0:
s = s + i
print(s, "is the sum of all the even numbers from 1 to 10.")
You first set a sum variable, to which all the values will get added.
Then, you start a for loop with the starting value as 1 and the ending value as 10. If it satisfies the next condition, which checks whether it is divisible by 2 or not [even numbers], it gets added to the sum variable.
The changing variable 'i' takes up all the values that satisfy the condition.
Attachments:
Similar questions