Balanced numbers P26492


Statement
 

pdf   zip   main.cc   main.c   main.java   main.py

thehtml

In this exercise, we say that a natural number is balanced if the sum of the digits in even positions is equal to the sum of the digits in odd positions. For instance, 1463 and 48015 are balanced numbers.

Write a function that tells if a natural number n is balanced or not.

Interface

C++
bool is_balanced(int n);
C
int is_balanced(int n);
Java
public static boolean isBalanced(int n);
Python
is_balanced(n) # returns bool
 
is_balanced(n: int) -> bool

Observation You only need to submit the required procedure; your main program will be ignored.

Public test cases
  • Input

    1463
    48015
    277242355
    471003930
    

    Output

    1
    1
    0
    0
    
  • Information
    Author
    Salvador Roura
    Language
    English
    Translator
    Carlos Molina
    Original language
    Catalan
    Other languages
    Catalan
    Official solutions
    C C++ Java Python
    User solutions
    C C++ Java Python