pesquisa

URI PROBLEMA 1071 - Soma de Impares Consecutivos I SOLUÇÃO EM C

URI Online Judge | 1071

Soma de Impares Consecutivos I

Adaptado por Neilor Tonin, URI  Brasil
Timelimit: 1
Leia 2 valores inteiros X e Y. A seguir, calcule e mostre a soma dos números impares entre eles.

Entrada

O arquivo de entrada contém dois valores inteiros.

Saída

O programa deve imprimir um valor inteiro. Este valor é a soma dos valores ímpares que estão entre os valores fornecidos na entrada que deverá caber em um inteiro.



URI Online Judge | 1071

Sum of Consecutive Odd Numbers I

Adapted by Neilor Tonin, URI  Brazil
Timelimit: 1
Read two integer values and Y. Print the sum of all odd values between them.

Input

The input file contain two integer values.

Output

The program must print an integer number. This number is the sum off all odd values between both input values that must fit in an integer number.

#include <stdio.h>

int main() {
 int cont,num1,num2,temp,soma;
    while(scanf("%d%d",&num1,&num2)==2){
        soma=0;
        if(num1>num2){
            temp=num1;
            num1=num2;
            num2=temp;
        }
        if(num1%2!=0){
        for(cont=num1+2;cont<num2;cont++){
            if(cont%2!=0)
                soma+=cont;
            }
        }
        else{
            for(cont=num1+1;cont<num2;cont++){
            if(cont%2!=0)
                soma+=cont;
            }
        }
        printf("%d\n",soma);
    }

    return 0;
}

Postar um comentário

0 Comentários