Given a number N, print sum of all even numbers from 1 to N.
Answers
Answered by
2
Answer:
hi..
Explanation:
N=int(input("enter the limit"))
sum=0
for i in range(1,N+1):
if i%2==0:
sum+=i
print(sum)
hope it helps you
please mark brainliest
Answered by
1
Written below is a C program to print the sum of all even numbers from 1 to N, where is N is taken as user input:
/**
* C program to print sum of all even numbers between 1 to n
*/
#include <stdio.h>
int main()
{
int i, N, Sum=0;
/* N as an input from the user */
printf("Enter the value of N: ");
scanf("%d", &N);
for(i=2; i<=N; i+=2)
{
/* Adding even numbers to sum */
Sum = Sum + i;
}
printf("Sum of all even number between 1 to %d = %d", N, Sum);
return 0;
}
- To determine the sum of even numbers, we take the limit as an input from the user using the scanf() function, till where we want to add the even numbers. We put it in a variable, like N.
- Then we set another variable Sum initial value to 0, so that it will store the sum.
- We must iterate through even numbers from 1 to n in order to find the sum of even numbers. We create a loop with an initial value of 2 and increase it by 2 each time. The loop should be formatted as for(i=2; i=N; i+=2).
- The instruction, Sum = Sum + i add the previous value of sum with i inside the body of the loop.
- At last we print the sum's final value after the loop.
To learn more:
https://brainly.in/question/32474569
https://brainly.in/question/49442199
#SPJ2
Similar questions