Beecrowd 1071 Sum of Consecutive Odd Numbers I Solution

Introduction

The URI 1071 problem requires finding the sum of all the odd numbers between two input values. This is a common problem in programming, and it is often used to test a programmer’s skills in loop constructs, conditional statements, and basic arithmetic operations. In this article, we will discuss the problem in detail, provide step-by-step solutions in C, C++, C++, Python, and Java, and explain the underlying logic and concepts.

Understanding the Problem

The URI 1071 problem states that we need to find the sum of all odd numbers between two input values. To solve this problem, we need to use a loop construct that iterates through all numbers between the two input values and checks whether they are odd or not. If a number is odd, we need to add it to the running sum.

Solution

We can solve the URI 1071 problem in several programming languages, including C, C++, C++, Python, and Java. Here is a step-by-step guide on how to solve the problem in each language.

C Solution

#include<stdio.h>
int main(){
    int a,b,sum=0;
    scanf("%d %d",&a,&b);
    if(a>b){
        int temp=a;
        a=b;
        b=temp;
    }
    for(int i=a+1;i<b;i++){
        if(i%2!=0) sum+=i;
    }
    printf("%d\n",sum);
    return 0;
}

C++ Solution

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a,b,sum=0;
    cin>>a>>b;
    if(a>b) swap(a,b);
    for(int i=a+1;i<b;i++){
        if(i%2!=0) sum+=i;
    }
    cout<<sum<<"\n";
    return 0;
}

Python Solution

a=int(input())
b=int(input())
if a>b:
    a,b=b,a
sum=0
for i in range(a+1,b):
    if i%2!=0: sum+=i
print(sum)

Java Solution

import java.util.*;
class Main{
    public static void main(String args[]){
        Scanner sc=new Scanner(System.in);
        int a,b,sum=0;
        a=sc.nextInt();
        b=sc.nextInt();
        if(a>b){
            int temp=a;
            a=b;
            b=temp;
        }
        for(int i=a+1;i<b;i++){
            if(i%2!=0) sum+=i;
        }
        System.out.println(sum);
    }
}

Explanation

The solutions provided above use a loop construct to iterate through all numbers between the two input values, a and b. We first check which value is larger and then swap them if needed, so that a is always the smaller of the two values. We then use a for loop to iterate through all numbers between a+1 and b-1, and check whether each number is odd or not. If the number is odd, we add it to the running sum. Finally, we print the sum of all odd numbers between a and b.

Conclusion

In conclusion, the URI 1071 problem requires finding the sum of all odd numbers between

BEECROWD Problems Solution

1 thought on “Beecrowd 1071 Sum of Consecutive Odd Numbers I Solution || Beecrowd 1071”

Leave a Comment