Write A Program To Find The Difference Between The Sum Of The Digits In The Even Places And Sum Of The Digits In The Odd Places . Tcs Nqt

Wed, 14 Apr, 2021

Write a program to find the difference between the sum of the digits in the even places and sum of the digits in the odd places
Problem description 
int the number 5179
even place terms are : (5+7) = 12
odd  place terms are : (1+9) = 10
result :  12 - 10 = 2
Example-1
Input: 5179
Expected Output: 2
Example-2
Input: 718
Expected Output: 14
PROGRAM IN C++ :

#include <iostream>
#include <cstring>

using namespace std;

int main() 
{ 
	int number; 
	cin>>number;
	int even = 0 , odd = 0 ;
	int checker = 1 , result = 0;
	
	while(number != 0) {
	    
    	if (checker % 2 == 0) 
            even += number % 10;   
        else
            odd += number % 10;   
        number /= 10; 
        checker++;
	}
	
	result = even - odd;
	if( result > 0)
	cout<<result;
	else
	cout<< -1 * result;

	return 0; 
}

 

0 comments
Leave a comment