Find the sum of digits of a number at even and odd places

Here given code implementation process.

// C Program for
// Find the sum of digits of a number at even and odd places
#include <stdio.h>

int absValue(int num)
{
	if (num < 0)
	{
		return -num;
	}
	return num;
}
void sumOfDigits(int num)
{
	int x = absValue(num);
	int count = 1;
	int evenSum = 0;
	int oddSum = 0;
	while (x > 0)
	{
		if (count % 2 == 1)
		{
			oddSum += (x % 10);
			count = 0;
		}
		else
		{
			evenSum += (x % 10);
			count = 1;
		}
		x = x / 10;
	}
	printf("\n  Number   : %d", num);
	printf("\n  Even Sum : %d", evenSum);
	printf("\n  Odd Sum  : %d", oddSum);
}
int main(int argc, char
	const *argv[])
{
	// Test
	// Number   : 83412
	// Even Sum : 3+1   = 4
	// Odd  Sum : 8+4+2 = 14
	sumOfDigits(83412);
	// Number   : 3245673
	// Even Sum : 2+5+7   = 14
	// Odd  Sum : 3+4+6+3 = 16
	sumOfDigits(3245673);
	// Number   : 1
	// Even Sum : 0
	// Odd  Sum : 1 = 1
	sumOfDigits(1);
	return 0;
}

Output

  Number   : 83412
  Even Sum : 4
  Odd Sum  : 14
  Number   : 3245673
  Even Sum : 14
  Odd Sum  : 16
  Number   : 1
  Even Sum : 0
  Odd Sum  : 1
// Java program for
// Find the sum of digits of a number at even and odd places
public class DigitSum
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void sumOfDigits(int num)
	{
		int x = absValue(num);
		int count = 1;
		int evenSum = 0;
		int oddSum = 0;
		while (x > 0)
		{
			if (count % 2 == 1)
			{
				// Sum of odd place digit 
				oddSum += (x % 10);
				count = 0;
			}
			else
			{
				// Sum of even place digit 
				evenSum += (x % 10);
				count = 1;
			}
			x = x / 10;
		}
		// Display calculated result
		System.out.print("\n Number : " + num);
		System.out.print("\n Even Sum : " + evenSum);
		System.out.print("\n Odd Sum : " + oddSum);
	}
	public static void main(String[] args)
	{
		DigitSum task = new DigitSum();
		// Test
		// Number   : 83412
		// Even Sum : 3+1   = 4
		// Odd  Sum : 8+4+2 = 14
		task.sumOfDigits(83412);
		// Number   : 3245673
		// Even Sum : 2+5+7   = 14
		// Odd  Sum : 3+4+6+3 = 16
		task.sumOfDigits(3245673);
		// Number   : 1
		// Even Sum : 0
		// Odd  Sum : 1 = 1
		task.sumOfDigits(1);
	}
}

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find the sum of digits of a number at even and odd places
class DigitSum
{
	public: int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	void sumOfDigits(int num)
	{
		int x = this->absValue(num);
		int count = 1;
		int evenSum = 0;
		int oddSum = 0;
		while (x > 0)
		{
			if (count % 2 == 1)
			{
				// Sum of odd place digit 
				oddSum += (x % 10);
				count = 0;
			}
			else
			{
				// Sum of even place digit 
				evenSum += (x % 10);
				count = 1;
			}
			x = x / 10;
		}
		// Display calculated result
		cout << "\n Number : " << num;
		cout << "\n Even Sum : " << evenSum;
		cout << "\n Odd Sum : " << oddSum;
	}
};
int main()
{
	DigitSum *task = new DigitSum();
	// Test
	// Number   : 83412
	// Even Sum : 3+1   = 4
	// Odd  Sum : 8+4+2 = 14
	task->sumOfDigits(83412);
	// Number   : 3245673
	// Even Sum : 2+5+7   = 14
	// Odd  Sum : 3+4+6+3 = 16
	task->sumOfDigits(3245673);
	// Number   : 1
	// Even Sum : 0
	// Odd  Sum : 1 = 1
	task->sumOfDigits(1);
	return 0;
}

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
package main
import "fmt"
// Go program for
// Find the sum of digits of a number at even and odd places
type DigitSum struct {}
func getDigitSum() * DigitSum {
	var me *DigitSum = &DigitSum {}
	return me
}
func(this DigitSum) absValue(num int) int {
	if num < 0 {
		return -num
	}
	return num
}
func(this DigitSum) sumOfDigits(num int) {
	var x int = this.absValue(num)
	var count int = 1
	var evenSum int = 0
	var oddSum int = 0
	for (x > 0) {
		if count % 2 == 1 {
			// Sum of odd place digit 
			oddSum += (x % 10)
			count = 0
		} else {
			// Sum of even place digit 
			evenSum += (x % 10)
			count = 1
		}
		x = x / 10
	}
	// Display calculated result
	fmt.Print("\n Number : ", num)
	fmt.Print("\n Even Sum : ", evenSum)
	fmt.Print("\n Odd Sum : ", oddSum)
}
func main() {
	var task * DigitSum = getDigitSum()
	// Test
	// Number   : 83412
	// Even Sum : 3+1   = 4
	// Odd  Sum : 8+4+2 = 14
	task.sumOfDigits(83412)
	// Number   : 3245673
	// Even Sum : 2+5+7   = 14
	// Odd  Sum : 3+4+6+3 = 16
	task.sumOfDigits(3245673)
	// Number   : 1
	// Even Sum : 0
	// Odd  Sum : 1 = 1
	task.sumOfDigits(1)
}

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
// Include namespace system
using System;
// Csharp program for
// Find the sum of digits of a number at even and odd places
public class DigitSum
{
	public int absValue(int num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	public void sumOfDigits(int num)
	{
		int x = this.absValue(num);
		int count = 1;
		int evenSum = 0;
		int oddSum = 0;
		while (x > 0)
		{
			if (count % 2 == 1)
			{
				// Sum of odd place digit 
				oddSum += (x % 10);
				count = 0;
			}
			else
			{
				// Sum of even place digit 
				evenSum += (x % 10);
				count = 1;
			}
			x = x / 10;
		}
		// Display calculated result
		Console.Write("\n Number : " + num);
		Console.Write("\n Even Sum : " + evenSum);
		Console.Write("\n Odd Sum : " + oddSum);
	}
	public static void Main(String[] args)
	{
		DigitSum task = new DigitSum();
		// Test
		// Number   : 83412
		// Even Sum : 3+1   = 4
		// Odd  Sum : 8+4+2 = 14
		task.sumOfDigits(83412);
		// Number   : 3245673
		// Even Sum : 2+5+7   = 14
		// Odd  Sum : 3+4+6+3 = 16
		task.sumOfDigits(3245673);
		// Number   : 1
		// Even Sum : 0
		// Odd  Sum : 1 = 1
		task.sumOfDigits(1);
	}
}

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
<?php
// Php program for
// Find the sum of digits of a number at even and odd places
class DigitSum
{
	public	function absValue($num)
	{
		if ($num < 0)
		{
			return -$num;
		}
		return $num;
	}
	public	function sumOfDigits($num)
	{
		$x = $this->absValue($num);
		$count = 1;
		$evenSum = 0;
		$oddSum = 0;
		while ($x > 0)
		{
			if ($count % 2 == 1)
			{
				// Sum of odd place digit 
				$oddSum += ($x % 10);
				$count = 0;
			}
			else
			{
				// Sum of even place digit 
				$evenSum += ($x % 10);
				$count = 1;
			}
			$x = (int)($x / 10);
		}
		// Display calculated result
		echo("\n Number : ".$num);
		echo("\n Even Sum : ".$evenSum);
		echo("\n Odd Sum : ".$oddSum);
	}
}

function main()
{
	$task = new DigitSum();
	// Test
	// Number   : 83412
	// Even Sum : 3+1   = 4
	// Odd  Sum : 8+4+2 = 14
	$task->sumOfDigits(83412);
	// Number   : 3245673
	// Even Sum : 2+5+7   = 14
	// Odd  Sum : 3+4+6+3 = 16
	$task->sumOfDigits(3245673);
	// Number   : 1
	// Even Sum : 0
	// Odd  Sum : 1 = 1
	$task->sumOfDigits(1);
}
main();

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
// Node JS program for
// Find the sum of digits of a number at even and odd places
class DigitSum
{
	absValue(num)
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	sumOfDigits(num)
	{
		var x = this.absValue(num);
		var count = 1;
		var evenSum = 0;
		var oddSum = 0;
		while (x > 0)
		{
			if (count % 2 == 1)
			{
				// Sum of odd place digit 
				oddSum += (x % 10);
				count = 0;
			}
			else
			{
				// Sum of even place digit 
				evenSum += (x % 10);
				count = 1;
			}
			x = parseInt(x / 10);
		}
		// Display calculated result
		process.stdout.write("\n Number : " + num);
		process.stdout.write("\n Even Sum : " + evenSum);
		process.stdout.write("\n Odd Sum : " + oddSum);
	}
}

function main()
{
	var task = new DigitSum();
	// Test
	// Number   : 83412
	// Even Sum : 3+1   = 4
	// Odd  Sum : 8+4+2 = 14
	task.sumOfDigits(83412);
	// Number   : 3245673
	// Even Sum : 2+5+7   = 14
	// Odd  Sum : 3+4+6+3 = 16
	task.sumOfDigits(3245673);
	// Number   : 1
	// Even Sum : 0
	// Odd  Sum : 1 = 1
	task.sumOfDigits(1);
}
main();

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
#  Python 3 program for
#  Find the sum of digits of a number at even and odd places
class DigitSum :
	def absValue(self, num) :
		if (num < 0) :
			return -num
		
		return num
	
	def sumOfDigits(self, num) :
		x = self.absValue(num)
		count = 1
		evenSum = 0
		oddSum = 0
		while (x > 0) :
			if (count % 2 == 1) :
				#  Sum of odd place digit 
				oddSum += (x % 10)
				count = 0
			else :
				#  Sum of even place digit 
				evenSum += (x % 10)
				count = 1
			
			x = int(x / 10)
		
		#  Display calculated result
		print("\n Number : ", num, end = "")
		print("\n Even Sum : ", evenSum, end = "")
		print("\n Odd Sum : ", oddSum, end = "")
	

def main() :
	task = DigitSum()
	#  Test
	#  Number   : 83412
	#  Even Sum : 3+1   = 4
	#  Odd  Sum : 8+4+2 = 14
	task.sumOfDigits(83412)
	#  Number   : 3245673
	#  Even Sum : 2+5+7   = 14
	#  Odd  Sum : 3+4+6+3 = 16
	task.sumOfDigits(3245673)
	#  Number   : 1
	#  Even Sum : 0
	#  Odd  Sum : 1 = 1
	task.sumOfDigits(1)

if __name__ == "__main__": main()

Output

 Number :  83412
 Even Sum :  4
 Odd Sum :  14
 Number :  3245673
 Even Sum :  14
 Odd Sum :  16
 Number :  1
 Even Sum :  0
 Odd Sum :  1
#  Ruby program for
#  Find the sum of digits of a number at even and odd places
class DigitSum 
	def absValue(num) 
		if (num < 0) 
			return -num
		end

		return num
	end

	def sumOfDigits(num) 
		x = self.absValue(num)
		count = 1
		evenSum = 0
		oddSum = 0
		while (x > 0) 
			if (count % 2 == 1) 
				#  Sum of odd place digit 
				oddSum += (x % 10)
				count = 0
			else
 
				#  Sum of even place digit 
				evenSum += (x % 10)
				count = 1
			end

			x = x / 10
		end

		#  Display calculated result
		print("\n Number : ", num)
		print("\n Even Sum : ", evenSum)
		print("\n Odd Sum : ", oddSum)
	end

end

def main() 
	task = DigitSum.new()
	#  Test
	#  Number   : 83412
	#  Even Sum : 3+1   = 4
	#  Odd  Sum : 8+4+2 = 14
	task.sumOfDigits(83412)
	#  Number   : 3245673
	#  Even Sum : 2+5+7   = 14
	#  Odd  Sum : 3+4+6+3 = 16
	task.sumOfDigits(3245673)
	#  Number   : 1
	#  Even Sum : 0
	#  Odd  Sum : 1 = 1
	task.sumOfDigits(1)
end

main()

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
// Scala program for
// Find the sum of digits of a number at even and odd places
class DigitSum()
{
	def absValue(num: Int): Int = {
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	def sumOfDigits(num: Int): Unit = {
		var x: Int = absValue(num);
		var count: Int = 1;
		var evenSum: Int = 0;
		var oddSum: Int = 0;
		while (x > 0)
		{
			if (count % 2 == 1)
			{
				// Sum of odd place digit 
				oddSum += (x % 10);
				count = 0;
			}
			else
			{
				// Sum of even place digit 
				evenSum += (x % 10);
				count = 1;
			}
			x = x / 10;
		}
		// Display calculated result
		print("\n Number : " + num);
		print("\n Even Sum : " + evenSum);
		print("\n Odd Sum : " + oddSum);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: DigitSum = new DigitSum();
		// Test
		// Number   : 83412
		// Even Sum : 3+1   = 4
		// Odd  Sum : 8+4+2 = 14
		task.sumOfDigits(83412);
		// Number   : 3245673
		// Even Sum : 2+5+7   = 14
		// Odd  Sum : 3+4+6+3 = 16
		task.sumOfDigits(3245673);
		// Number   : 1
		// Even Sum : 0
		// Odd  Sum : 1 = 1
		task.sumOfDigits(1);
	}
}

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1
// Swift 4 program for
// Find the sum of digits of a number at even and odd places
class DigitSum
{
	func absValue(_ num: Int) -> Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	func sumOfDigits(_ num: Int)
	{
		var x: Int = self.absValue(num);
		var count: Int = 1;
		var evenSum: Int = 0;
		var oddSum: Int = 0;
		while (x > 0)
		{
			if (count % 2 == 1)
			{
				// Sum of odd place digit 
				oddSum += (x % 10);
				count = 0;
			}
			else
			{
				// Sum of even place digit 
				evenSum += (x % 10);
				count = 1;
			}
			x = x / 10;
		}
		// Display calculated result
		print("\n Number : ", num, terminator: "");
		print("\n Even Sum : ", evenSum, terminator: "");
		print("\n Odd Sum : ", oddSum, terminator: "");
	}
}
func main()
{
	let task: DigitSum = DigitSum();
	// Test
	// Number   : 83412
	// Even Sum : 3+1   = 4
	// Odd  Sum : 8+4+2 = 14
	task.sumOfDigits(83412);
	// Number   : 3245673
	// Even Sum : 2+5+7   = 14
	// Odd  Sum : 3+4+6+3 = 16
	task.sumOfDigits(3245673);
	// Number   : 1
	// Even Sum : 0
	// Odd  Sum : 1 = 1
	task.sumOfDigits(1);
}
main();

Output

 Number :  83412
 Even Sum :  4
 Odd Sum :  14
 Number :  3245673
 Even Sum :  14
 Odd Sum :  16
 Number :  1
 Even Sum :  0
 Odd Sum :  1
// Kotlin program for
// Find the sum of digits of a number at even and odd places
class DigitSum
{
	fun absValue(num: Int): Int
	{
		if (num < 0)
		{
			return -num;
		}
		return num;
	}
	fun sumOfDigits(num: Int): Unit
	{
		var x: Int = this.absValue(num);
		var count: Int = 1;
		var evenSum: Int = 0;
		var oddSum: Int = 0;
		while (x > 0)
		{
			if (count % 2 == 1)
			{
				// Sum of odd place digit 
				oddSum += (x % 10);
				count = 0;
			}
			else
			{
				// Sum of even place digit 
				evenSum += (x % 10);
				count = 1;
			}
			x = x / 10;
		}
		// Display calculated result
		print("\n Number : " + num);
		print("\n Even Sum : " + evenSum);
		print("\n Odd Sum : " + oddSum);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: DigitSum = DigitSum();
	// Test
	// Number   : 83412
	// Even Sum : 3+1   = 4
	// Odd  Sum : 8+4+2 = 14
	task.sumOfDigits(83412);
	// Number   : 3245673
	// Even Sum : 2+5+7   = 14
	// Odd  Sum : 3+4+6+3 = 16
	task.sumOfDigits(3245673);
	// Number   : 1
	// Even Sum : 0
	// Odd  Sum : 1 = 1
	task.sumOfDigits(1);
}

Output

 Number : 83412
 Even Sum : 4
 Odd Sum : 14
 Number : 3245673
 Even Sum : 14
 Odd Sum : 16
 Number : 1
 Even Sum : 0
 Odd Sum : 1


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved