Write a program to find the sum of all even numbers from 1 to n where the value of n is taken as input. [for example when n is 10 then the sum is 2+4+6+8+10 = 30]
Answers
Answered by
0
Answer:
I wrote it in C. Hope it helps
Explanation:
#include <stdio.h>
#include <stdlib.h>
main(){
int sum, i, n;
printf("Give a number: ");
scanf("%d", &n);
sum = 0;
for(i=0;i<n;i++){
if(i%2==0){
sum = sum + i;
}
}
printf("The sum is %d", sum);
system("pause");
}
Similar questions
English,
7 months ago
Math,
7 months ago
Computer Science,
1 year ago
Computer Science,
1 year ago
Science,
1 year ago
Math,
1 year ago
Physics,
1 year ago