Question

Uri 1159 Solution || Beecrowd 1159 Solution ||Sum of Consecutive Even Numbers solution in C

The program must read an integer indefinite times (stop when X=0). For each X, print the sum of five consecutive even numbers from X, including it if X is even. If the input number is 4, for example, the output must be 40, that is the result of the operation: 4+6+8+10+12. If the input number is 11, for example, the output must be 80, that is the result of 12+14+16+18+20.

Input

The input file contains many integer numbers. The last one is zero.

Output

Print the output according to the example below.

Input SampleOutput Sample
4
11
0
40
80

Uri 1159 Solution

Beecrowd 1159 – Sum of Consecutive Even Numbers solution in C

#include <stdio.h>

int main() {

int x,i,sum=0;
while(1){
        sum=0;
    scanf("%d",&x);
    if(x==0)break;
    else if(x%2!=0)x++;
    for(i=0;i<5;i++){
        sum=sum+x;
        x+=2;
    }
    printf("%d\n",sum);
}

    return 0;
}

Uri 1159 Solution

Beecrowd 1159 – Sum of Consecutive Even Numbers solution in Python

def main():
    while True:
        sum = 0
        x = int(input())
        if x == 0:
            break
        elif x % 2 != 0:
            x += 1
        for i in range(5):
            sum += x
            x += 2
        print(sum)

if __name__ == "__main__":
    main()

Next problem: Beecrowd 1172 – Array Replacement I solution in C/Cpp

Leave a Comment