URI Online Judge | 1159
Soma de Pares Consecutivos
Adaptado por Neilor Tonin, URI
Brasil
Timelimit: 1
Brasil
O programa deve ler um valor inteiro X indefinidas vezes. (O programa irá parar quando o valor de X for igual a 0). Para cada X lido, imprima a soma dos 5 pares consecutivos a partir de X, inclusive o X , se for par. Se o valor de entrada for 4, por exemplo, a saída deve ser 40, que é o resultado da operação: 4+6+8+10+12, enquanto que se o valor de entrada for 11, por exempo, a saída deve ser 80, que é a soma de 12+14+16+18+20.
Entrada
O arquivo de entrada contém muitos valores inteiros. O último valor do arquivo é zero.
Saída
URI Online Judge | 1159
Sum of Consecutive Even Numbers
Adapted by Neilor Tonin, URI
Brazil
Timelimit: 1
Brazil
The program must read an integer X 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.
#include <stdio.h>
int main()
{
int x,a,b=0;
while(1)
{
scanf("%d", &x);
b=0;
if(x==0)
break;
else if(x%2==0)
{
for(a=x; a<=x+8; a+=2) b+=a;
}
else
{
for(a=x+1;a<=x+9;a+=2) b+=a;
}
printf("%d\n", b);
}
return 0;
}
Nenhum comentário:
Postar um comentário