- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C Program for Difference between sums of odd and even digits?
Suppose we have one long integer. We have to find if the differences between the sum of the odd position digits, and the sum of the even position digits are 0 or not. The positions are start from 0 (left most).
For example, suppose a number is 156486. The odd position sum is (5 + 4 + 6) = 15, and even position sum is (1 + 6 + 8) = 15, so they are same.
To solve this problem, we can use two different ways. The first way is traversing form start to end and get the sum by alternating the position, then get the difference. The next method is simpler, and efficient. If the number is divisible by 11, then the difference must be 0. So in other words we can say that if the sum of odd position numbers and sum of even position numbers are same, then the number is divisible by 11.
Algorithm
isDiffZero(n)
begin if n is divisible by 11, then return 1 else return 0 end if end
Example
#include<stdio.h> long isDiffZero(int n) { if(n % 11 == 0){ return 1; } else { return 0; } } main() { int n; printf("Enter a number: "); scanf("%d", &n); if(isDiffZero(n)) { printf("Difference is zero"); } else { printf("Difference is non zero"); } }
Output
Enter a number: 156486 Difference is zero
- Related Articles
- C Program for the 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++
- Count even and odd digits in an Integer in C++
- C/C++ Program for Odd-Even Sort (Brick Sort)?
- C/C++ Program for the 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++)?
