- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion

# C Program for the Difference between sums of odd and even digits?

Given a number, find the difference between sum of odd digits and sum of even digits. Which means we will be count all even digits and all odd digits and the subtracting their sums.

### Sample

Input:12345 Output:3

## Explanation

the odd digits is 2+4=6 the even digits is 1+3+5=9 odd-even=9-6=3

Taking every digit out of number and checking whether the digit is even or odd if even then add it to even sum if not then to odd sum and then take difference of them.

## Example

#include <iostream> using namespace std; int main() { int n, r=0; int diff =0; int even=0; int odd=0; n=12345; while(n != 0){ r = n%10; if(r % 2 == 0) { even+=r; } else { odd+=r; } n/=10; } diff=odd-even; printf("%d",diff); return 0; }

- Related Articles
- C Program for Difference between sums of odd and even digits?
- Python Program for Difference between sums of odd and even digits
- Difference between sums of odd and even digits.
- Difference between sums of odd level and even level nodes of a Binary Tree in Java
- Difference between sums of odd position and even position nodes of a Binary Tree in Java
- Count number of ordered pairs with Even and Odd Sums in C++
- Print all n-digit numbers with absolute difference between sum of even and odd digits is 1 in C++
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime in C++
- C/C++ Program for the Odd-Even Sort (Brick Sort)?
- Count even and odd digits in an Integer in C++
- C/C++ Program for Odd-Even Sort (Brick Sort)?
- Python program to find the sum of all even and odd digits of an integer list
- Find the sum of digits of a number at even and odd places in C++
- Python Program for Odd-Even Sort / Brick Sort
- Absolute Difference of even and odd indexed elements in an Array (C++)?

Advertisements