Beecrowd 1099 – Sum of Consecutive Odd Numbers II solution in C

Question Link

Solution in C

#include <stdio.h>

int main() {

   int n,x,y,i,j,sum=0,temp=0;
   scanf("%d",&n);
   for(i=0;i<n;i++){
       scanf("%d %d",&x,&y);
      if(x>y){
        temp=x;
        x=y;
        y=temp;
      }
      if(x%2==0){
        x--;
      }
      x+=2;

       for(j=x;j<y;j+=2){

              sum=sum+j;

       }
       printf("%d\n",sum);
       sum=0;
   }


    return 0;
}

Beecrowd 1099 – Sum of Consecutive Odd Numbers II solution in Python

Solution in Python

def main():
    n = int(input())
    for _ in range(n):
        x, y = map(int, input().split())
        if x > y:
            x, y = y, x
        if x % 2 == 0:
            x -= 1
        x += 2
        sum = 0
        for j in range(x, y, 2):
            sum += j
        print(sum)

if __name__ == "__main__":
    main()

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

Leave a Comment