
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Find the sum of digits of a number at even and odd places in C++
Suppose, we have an integer N, We have to find the sum of the odd place digits and the even place digits. So if the number is like 153654, then odd_sum = 9, and even_sum = 15.
To solve this, we can extract all digits from last digit, if the original number has odd number of digits, then the last digit must be odd positioned, else it will be even positioned. After process a digit, we can invert the state from odd to even and vice versa.
Example
#include<iostream> using namespace std; bool isOdd(int x){ if(x % 2 == 0) return false; return true; } void getSum(int n) { bool odd_check = isOdd(n); int odd_sum = 0, even_sum = 0; while (n != 0) { if (odd_check) odd_sum += n % 10; else even_sum += n % 10; odd_check = !odd_check; n /= 10; } cout << "Sum odd : " << odd_sum << endl; cout << "Sum even : " << even_sum; } int main() { int n = 153654; getSum(n); }
Output
Sum odd : 9 Sum even : 15
- Related Articles
- Check if product of digits of a number at even and odd places is equal in Python
- Primality test for the sum of digits at odd places of a number in C++
- Check whether product of digits at even places is divisible by sum of digits at odd place of a numbers in Python
- Sum of individual even and odd digits in a string number using JavaScript
- Check whether sum of digits at odd places of a number is divisible by K in Python
- The number of terms of an A.P. is even ; sum of all terms at odd places and even places are $24$ and $30$ respectively . Last term exceeds the first term by $10.5$. Then find the number of terms in the series.
- Python program to find the sum of all even and odd digits of an integer list
- Find the Number With Even Sum of Digits using C++
- Check whether product of digits at even places of a number is divisible by K in Python
- Count odd and even digits in a number in PL/SQL
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
- Difference between sums of odd and even digits.
- Find sum of even and odd nodes in a linked list in C++
- Find the Largest number with given number of digits and sum of digits in C++
- Even numbers at even index and odd numbers at odd index in C++

Advertisements