Sunday, August 13, 2023

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]


 

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]


Program :

#include <stdio.h>  

void main()

{

int N, sum=0; 

scanf("%d", &N);

int i;

for (i = 1; i <= N; i++)

{

if (i % 2 == 0)

sum = sum + i;

}

printf("Sum = %d", sum);

}



InputOutput
Test Case 1
10
Sum = 30
Test Case 2
15
Sum = 56

No comments:

Post a Comment