Compare Sum of Odd Positioned and Even Positioned Digits

The program must accept an integer N as the input. The task is to compare the sum of the digits at odd positions and even positions in the number. If the sum of the digits at odd positions is greater, then print ODD. If the sum of the digits at even positions is greater, then print EVEN. If both sums are equal, then print EQUAL.

Boundary Condition(s):
1 <= N <= 10^9

Input Format:
The first line contains the integer N.

Output Format:
The first line contains either ODD, EVEN or EQUAL.

Example Input/Output 1:
Input:
2435

Output:
EVEN

Explanation:
Sum of odd positioned digits = 2 + 3 = 5
Sum of even positioned digits = 4 + 5 = 9
Since 9 > 5, the output is EVEN.

Example Input/Output 2:
Input:
4444

Output:
EQUAL

Explanation:
Both the sum of odd positioned digits and even positioned digits are equal to 8.

n = input().strip()
odd_sum = sum(int(n[i]) for i in range(0, len(n), 2))
even_sum = sum(int(n[i]) for i in range(1, len(n), 2))
if odd_sum > even_sum:
    print("ODD")
elif even_sum > odd_sum:
    print("EVEN")
else:
    print("EQUAL")
#include<stdio.h>
#include<string.h>

int main() {
    char n[12];
    scanf("%s", n);
    int odd_sum = 0, even_sum = 0, len = strlen(n);
    for(int i = 0; i < len; i++) {
        if(i % 2 == 0) {
            odd_sum += n[i] - '0';
        } else {
            even_sum += n[i] - '0';
        }
    }
    if(odd_sum > even_sum) printf("ODD");
    else if(even_sum > odd_sum) printf("EVEN");
    else printf("EQUAL");
    return 0;
}
#include <iostream>
#include <string>
using namespace std;

int main() {
    string n;
    cin >> n;
    int odd_sum = 0, even_sum = 0;
    for(int i = 0; i < n.size(); i++) {
        if(i % 2 == 0) {
            odd_sum += n[i] - '0';
        } else {
            even_sum += n[i] - '0';
        }
    }
    if(odd_sum > even_sum) cout << "ODD";
    else if(even_sum > odd_sum) cout << "EVEN";
    else cout << "EQUAL";
    return 0;
}
import java.util.Scanner;

public class CompareSums {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int odd_sum = 0, even_sum = 0;
        for(int i = 0; i < n.length(); i++) {
            if(i % 2 == 0) {
                odd_sum += n.charAt(i) - '0';
            } else {
                even_sum += n.charAt(i) - '0';
            }
        }
        if(odd_sum > even_sum) System.out.println("ODD");
        else if(even_sum > odd_sum) System.out.println("EVEN");
        else System.out.println("EQUAL");
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

More posts. You may also be interested in.