Given a number N, the task is to find the sum of digits of a number at even and odd places.
Examples:
Input: N = 54873
Output:
Sum odd = 16
Sum even = 11
Input: N = 457892
Output:
Sum odd = 20
Sum even = 15
Approach:
- First, calculate the reverse of the given number.
- To the reverse number we apply modulus operator and extract its last digit which is actually the first digit of a number so it is odd positioned digit.
- The next digit will be even positioned digit, and we can take the sum in alternating turns.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the reverse of a number int reverse(int n) { int rev = 0; while (n != 0) { rev = (rev * 10) + (n % 10); n /= 10; } return rev; } // Function to find the sum of the odd // and even positioned digits in a number void getSum(int n) { n = reverse(n); int sumOdd = 0, sumEven = 0, c = 1; while (n != 0) { // If c is even number then it means // digit extracted is at even place if (c % 2 == 0) sumEven += n % 10; else sumOdd += n % 10; n /= 10; c++; } cout << "Sum odd = " << sumOdd << "\n"; cout << "Sum even = " << sumEven; } // Driver code int main() { int n = 457892; getSum(n); return 0; } |
Java
// Java implementation of the approach import java.util.*; class GFG { // Function to return the reverse of a number static int reverse(int n) { int rev = 0; while (n != 0) { rev = (rev * 10) + (n % 10); n /= 10; } return rev; } // Function to find the sum of the odd // and even positioned digits in a number static void getSum(int n) { n = reverse(n); int sumOdd = 0, sumEven = 0, c = 1; while (n != 0) { // If c is even number then it means // digit extracted is at even place if (c % 2 == 0) sumEven += n % 10; else sumOdd += n % 10; n /= 10; c++; } System.out.println("Sum odd = " + sumOdd); System.out.println("Sum even = " + sumEven); } // Driver code public static void main(String args[]) { int n = 457892; getSum(n); } } // This code is contributed by // Surendra_Gangwar |
Python3
# Python3 implementation of the approach # Function to return the # reverse of a number def reverse(n): rev = 0 while (n != 0): rev = (rev * 10) + (n % 10) n //= 10 return rev # Function to find the sum of the odd # and even positioned digits in a number def getSum(n): n = reverse(n) sumOdd = 0 sumEven = 0 c = 1 while (n != 0): # If c is even number then it means # digit extracted is at even place if (c % 2 == 0): sumEven += n % 10 else: sumOdd += n % 10 n //= 10 c += 1 print("Sum odd =", sumOdd) print("Sum even =", sumEven) # Driver code n = 457892getSum(n) # This code is contributed # by mohit kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the reverse of a number static int reverse(int n) { int rev = 0; while (n != 0) { rev = (rev * 10) + (n % 10); n /= 10; } return rev; } // Function to find the sum of the odd // and even positioned digits in a number static void getSum(int n) { n = reverse(n); int sumOdd = 0, sumEven = 0, c = 1; while (n != 0) { // If c is even number then it means // digit extracted is at even place if (c % 2 == 0) sumEven += n % 10; else sumOdd += n % 10; n /= 10; c++; } Console.WriteLine("Sum odd = " + sumOdd); Console.WriteLine("Sum even = " + sumEven); } // Driver code public static void Main() { int n = 457892; getSum(n); } } // This code is contributed by // Akanksha Rai |
PHP
<?php // PHP implementation of the above approach // Function to return the reverse of a number function reverse($n) { $rev = 0; while ($n != 0) { $rev = ($rev * 10) + ($n % 10); $n = floor($n / 10); } return $rev; } // Function to find the sum of the odd // and even positioned digits in a number function getSum($n) { $n = reverse($n); $sumOdd = 0; $sumEven = 0; $c = 1; while ($n != 0) { // If c is even number then it means // digit extracted is at even place if ($c % 2 == 0) $sumEven += $n % 10; else $sumOdd += $n % 10; $n = floor($n / 10); $c++; } echo "Sum odd = ", $sumOdd, "\n"; echo "Sum even = ", $sumEven; } // Driver code $n = 457892; getSum($n); // This code is contributed by Ryuga ?> |
Sum odd = 20 Sum even = 15
Another approach: The problem can be solved without reversing the number. We can extract all the digits from the number one by one from the end. If the original number was odd then the last digit must be odd positioned else it will be even positioned. After processing a digit, we can invert the state from odd to even and vice versa.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to find the sum of the odd // and even positioned digits in a number void getSum(int n) { // If n is odd then the last digit // will be odd positioned bool isOdd = (n % 2 == 1) ? true : false; // To store the respective sums int sumOdd = 0, sumEven = 0; // While there are digits left process while (n != 0) { // If current digit is odd positioned if (isOdd) sumOdd += n % 10; // Even positioned digit else sumEven += n % 10; // Invert state isOdd = !isOdd; // Remove last digit n /= 10; } cout << "Sum odd = " << sumOdd << "\n"; cout << "Sum even = " << sumEven; } // Driver code int main() { int n = 457892; getSum(n); return 0; } |
Java
// Java implementation of the above approach class GFG{ // Function to find the sum of the odd // and even positioned digits in a number static void getSum(int n) { // If n is odd then the last digit // will be odd positioned boolean isOdd = (n % 2 == 1) ? true : false; // To store the respective sums int sumOdd = 0, sumEven = 0; // While there are digits left process while (n != 0) { // If current digit is odd positioned if (isOdd) sumOdd += n % 10; // Even positioned digit else sumEven += n % 10; // Invert state isOdd = !isOdd; // Remove last digit n /= 10; } System.out.println("Sum odd = " + sumOdd); System.out.println("Sum even = " + sumEven); } // Driver code public static void main(String[] args) { int n = 457892; getSum(n); } } // This code is contributed by jrishabh99 |
Python3
# Python3 implementation of the approach # Function to find the sum of the odd # and even positioned digits in a number def getSum(n): # If n is odd then the last digit # will be odd positioned if (n % 2 == 1) : isOdd = True else: isOdd = False # To store the respective sums sumOdd = 0 sumEven = 0 # While there are digits left process while (n != 0) : # If current digit is odd positioned if (isOdd): sumOdd += n % 10 # Even positioned digit else: sumEven += n % 10 # Invert state isOdd = not isOdd # Remove last digit n //= 10 print( "Sum odd = " , sumOdd ) print("Sum even = " ,sumEven) # Driver code if __name__ =="__main__": n = 457892 getSum(n) # This code is contributed by chitranayal |
Sum odd = 20 Sum even = 15
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Check if product of digits of a number at even and odd places is equal
- Primality test for the sum of digits at odd places of a number
- Check whether sum of digits at odd places of a number is divisible by K
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Number formed by deleting digits such that sum of the digits becomes even and the number odd
- Check whether product of digits at even places of a number is divisible by K
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Check if a number has an odd count of odd divisors and even count of even divisors
- Smallest odd number with even sum of digits from the given number N
- Count total number of N digit numbers such that the difference between sum of even and odd digits is 1
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Count of numbers in range which are divisible by M and have digit D at odd places
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Arrangement of the characters of a word such that all vowels are at odd places
- Check if the number is even or odd whose digits and base (radix) is given
- Find smallest number with given number of digits and sum of digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

