Posted on by Kalkicode
Code Number

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

The given problem is to find the sum of digits at even and odd places for a given number. For example, if we have a number "83412", the even-placed digits are "3" and "1", and the odd-placed digits are "8", "4", and "2". We need to find the sum of these digits separately.

## Explanation with Suitable Example

Let's take the number "83412" as an example and go through the steps of the code to understand how the sum of digits at even and odd places is calculated.

1. The number is taken as input, and the function `sumOfDigits` is called with this number.

2. Inside the `sumOfDigits` function, the `absValue` function is used to ensure that we work with the positive value of the number, irrespective of whether the input is positive or negative.

3. The variables `evenSum` and `oddSum` are initialized to 0, which will store the sum of even and odd-placed digits, respectively.

4. A `while` loop is used to iterate through each digit of the number. The loop continues until `x` becomes 0.

5. Inside the loop, the current digit is obtained using `(x % 10)`, and its position is determined using the `count` variable. If the `count` is odd (1, 3, 5, ...), then the digit is at an odd position, and it is added to the `oddSum`. Otherwise, it is added to the `evenSum`.

6. The `count` is updated to alternate between 1 and 0. This ensures that we correctly identify the even and odd positions.

7. After processing the current digit, it is removed from the number by performing `x = x / 10`.

8. The loop continues until all digits have been processed.

9. Finally, the function prints the original number and the sums of digits at even and odd positions.

## Standard Pseudocode and Algorithm

``````sumOfDigits(number)
x = absValue(number)
evenSum = 0
oddSum = 0
count = 1

while x > 0:
if count is odd:
oddSum += (x % 10)
count = 0
else:
evenSum += (x % 10)
count = 1
x = x / 10

print "Number   : number"
print "Even Sum : evenSum"
print "Odd Sum  : oddSum"
``````

## Code Solution

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)
{
// Test
// Number   : 83412
// Even Sum : 3+1   = 4
// Odd  Sum : 8+4+2 = 14
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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()
{
// Test
// Number   : 83412
// Even Sum : 3+1   = 4
// Odd  Sum : 8+4+2 = 14
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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)
{
// Test
// Number   : 83412
// Even Sum : 3+1   = 4
// Odd  Sum : 8+4+2 = 14
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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()
{
// Test
// Number   : 83412
// Even Sum : 3+1   = 4
// Odd  Sum : 8+4+2 = 14
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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()
{
// Test
// Number   : 83412
// Even Sum : 3+1   = 4
// Odd  Sum : 8+4+2 = 14
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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() :
#  Test
#  Number   : 83412
#  Even Sum : 3+1   = 4
#  Odd  Sum : 8+4+2 = 14
#  Number   : 3245673
#  Even Sum : 2+5+7   = 14
#  Odd  Sum : 3+4+6+3 = 16
#  Number   : 1
#  Even Sum : 0
#  Odd  Sum : 1 = 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()
#  Test
#  Number   : 83412
#  Even Sum : 3+1   = 4
#  Odd  Sum : 8+4+2 = 14
#  Number   : 3245673
#  Even Sum : 2+5+7   = 14
#  Odd  Sum : 3+4+6+3 = 16
#  Number   : 1
#  Even Sum : 0
#  Odd  Sum : 1 = 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
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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()
{
// Test
// Number   : 83412
// Even Sum : 3+1   = 4
// Odd  Sum : 8+4+2 = 14
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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
{
// Test
// Number   : 83412
// Even Sum : 3+1   = 4
// Odd  Sum : 8+4+2 = 14
// Number   : 3245673
// Even Sum : 2+5+7   = 14
// Odd  Sum : 3+4+6+3 = 16
// Number   : 1
// Even Sum : 0
// Odd  Sum : 1 = 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``````

## Resultant Output Explanation

Let's go through the example numbers from the code and understand their outputs:

1. For `number = 83412`:

• Even-placed digits: 3, 1
• Odd-placed digits: 8, 4, 2
• Even Sum: 3 + 1 = 4
• Odd Sum: 8 + 4 + 2 = 14
2. For `number = 3245673`:

• Even-placed digits: 2, 5, 7
• Odd-placed digits: 3, 4, 6, 3
• Even Sum: 2 + 5 + 7 = 14
• Odd Sum: 3 + 4 + 6 + 3 = 16
3. For `number = 1`:

• Even-placed digits: None (no even-placed digits for a single-digit number)
• Odd-placed digits: 1
• Even Sum: 0
• Odd Sum: 1

## Time Complexity of the Code

The time complexity of the code is O(d), where d is the number of digits in the given input number. The while loop runs once for each digit in the number, performing constant-time operations in each iteration. Hence, the time complexity is linear in terms of the number of digits.

## Comment

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.

Categories
Relative Post