# Find sum and product of all even digit sum in a Singly Linked List

Here given code implementation process.

``````// C Program
// Find sum and product of all even digit sum in a Singly Linked List
#include <stdio.h>

#include <stdlib.h> //for malloc function

{
int data;
};
// Singly linked list
struct SingleLL
{
};
// Returns the new linked list
{
// Create memory of head and tail Nodes
struct SingleLL *sll = (struct SingleLL *) malloc(sizeof(struct SingleLL));
if (sll == NULL)
{
printf("Memory overflow\n");
}
else
{
sll->tail = NULL;
}
return sll;
}
// Returns a new Node of linked list
{
// Create dynamic node
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
// Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
// Add new Node at end of linked list
void addNode(struct SingleLL *sll, int data)
{
if (sll->head == NULL)
{
}
else
{
// Append the node at last position
sll->tail->next = node;
}
sll->tail = node;
}
// Display linked list element
void display(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
// iterating linked list elements
while (temp != NULL)
{
printf(" %d →", temp->data);
// Visit to next node
temp = temp->next;
}
printf(" NULL\n");
}

// Returns the sum of digits of a given number
int digitSum(int num)
{
int sum = 0;
int n = num;

while (n > 0)
{
sum += n%10;
n = n / 10;
}

return sum;
}
//  Find the sum and product of even digit sum in linked list
void evenSumProduct(struct SingleLL *sll)
{
if (sll->head == NULL)
{
printf("\n Empty linked list\n");
return;
}
// Define auxiliary variable

int sum = 0;
int product = 1;
int status = 0;

// iterate linked list elements
while(auxiliary != NULL)
{
if(auxiliary->data >= 0 && (digitSum(auxiliary->data) % 2  == 0) )
{

// When digit sum is even
status = 1;
// Calculate sum
sum += auxiliary->data;
// Calculate product
product *= auxiliary->data;
}
// Visit to next node
auxiliary = auxiliary->next;
}

if(status==0)
{
// When Even sum digits not present
printf("\n None ");
}
else
{
// Display sum and product
printf("\n Sum : %d",sum);
printf("\n Product : %d\n",product);
}

}
int main()
{
// Define linked list
struct SingleLL *sll = newLinkedList();

//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
display(sll);
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
evenSumProduct(sll);

return 0;
}``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL

Sum : 17
Product : 88``````
``````/*
Java Program for
Find sum and product of all even digit sum in a Singly Linked List
*/
// Linked list node
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
if (this.head == null)
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
System.out.print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
System.out.print(" NULL\n");
}
// Returns the sum of digits of a given number
public int digitSum(int num)
{
int sum = 0;
int n = num;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
//  Find the sum and product of even digit sum in linked list
public void evenSumProduct()
{
if (this.head == null)
{
System.out.print("\n Empty linked list\n");
return;
}
// Define auxiliary variable
int sum = 0;
int product = 1;
boolean status = false;
// iterate linked list elements
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.digitSum(auxiliary.data) % 2 == 0))
{
// When digit sum is even
status = true;
// Calculate sum
sum += auxiliary.data;
// Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (status == false)
{
// When Even sum digits not present
System.out.print("\n None ");
}
else
{
// Display sum and product
System.out.print(" Sum : " + sum );
System.out.print("\n Product : " + product + "\n");
}
}
public static void main(String[] args)
{

SingleLL sll = new SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display();
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
sll.evenSumProduct();
}
}``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program for
Find sum and product of all even digit sum in a Singly Linked List
*/

// Linked list node
{
public:
int data;
{
this->data = data;
this->next = NULL;
}
};
class SingleLL
{
public:
SingleLL()
{
this->tail = NULL;
}
//Add new Node at end of linked list
{
if (this->head == NULL)
{
}
else
{
// Append the node at last position
this->tail->next = node;
}
this->tail = node;
}
// Display linked list element
void display()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
//iterating linked list elements
while (temp != NULL)
{
cout << " " << temp->data << " →";
// Visit to next node
temp = temp->next;
}
cout << " NULL\n";
}
// Returns the sum of digits of a given number
int digitSum(int num)
{
int sum = 0;
int n = num;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
//  Find the sum and product of even digit sum in linked list
void evenSumProduct()
{
if (this->head == NULL)
{
cout << "\n Empty linked list\n";
return;
}
// Define auxiliary variable
int sum = 0;
int product = 1;
bool status = false;
// iterate linked list elements
while (auxiliary != NULL)
{
if (auxiliary->data >= 0 && (this->digitSum(auxiliary->data) % 2 == 0))
{
// When digit sum is even
status = true;
// Calculate sum
sum += auxiliary->data;
// Calculate product
product *= auxiliary->data;
}
// Visit to next node
auxiliary = auxiliary->next;
}
if (status == false)
{
// When Even sum digits not present
cout << "\n None ";
}
else
{
// Display sum and product
cout << " Sum : " << sum;
cout << "\n Product : " << product << "\n";
}
}
};
int main()
{
SingleLL sll = SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display();
// 2 + 11 + 4 = 17
// 2 *11 *4 = 88
sll.evenSumProduct();
return 0;
}``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88``````
``````// Include namespace system
using System;
/*
C# Program for
Find sum and product of all even digit sum in a Singly Linked List
*/
// Linked list node
{
public int data;
{
this.data = data;
this.next = null;
}
}
public class SingleLL
{
public SingleLL()
{
this.tail = null;
}
//Add new Node at end of linked list
public void addNode(int data)
{
if (this.head == null)
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
public void display()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
Console.Write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
Console.Write(" NULL\n");
}
// Returns the sum of digits of a given number
public int digitSum(int num)
{
int sum = 0;
int n = num;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
//  Find the sum and product of even digit sum in linked list
public void evenSumProduct()
{
if (this.head == null)
{
Console.Write("\n Empty linked list\n");
return;
}
// Define auxiliary variable
int sum = 0;
int product = 1;
Boolean status = false;
// iterate linked list elements
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.digitSum(auxiliary.data) % 2 == 0))
{
// When digit sum is even
status = true;
// Calculate sum
sum += auxiliary.data;
// Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (status == false)
{
// When Even sum digits not present
Console.Write("\n None ");
}
else
{
// Display sum and product
Console.Write(" Sum : " + sum);
Console.Write("\n Product : " + product + "\n");
}
}
public static void Main(String[] args)
{
SingleLL sll = new SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display();
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
sll.evenSumProduct();
}
}``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88``````
``````<?php
/*
Php Program for
Find sum and product of all even digit sum in a Singly Linked List
*/
// Linked list node
{
public \$data;
public \$next;

function __construct(\$data)
{
\$this->data = \$data;
\$this->next = null;
}
}
class SingleLL
{
public \$tail;

function __construct()
{
\$this->tail = null;
}
//Add new Node at end of linked list
{
\$node = new LinkNode(\$data);
if (\$this->head == null)
{
}
else
{
// Append the node at last position
\$this->tail->next = \$node;
}
\$this->tail = \$node;
}
// Display linked list element
public	function display()
{
if (\$this->head == null)
{
echo "\n Empty linked list\n";
return;
}
//iterating linked list elements
while (\$temp != null)
{
echo " ". \$temp->data ." →";
// Visit to next node
\$temp = \$temp->next;
}
echo " NULL\n";
}
// Returns the sum of digits of a given number
public	function digitSum(\$num)
{
\$sum = 0;
\$n = \$num;
while (\$n > 0)
{
\$sum += \$n % 10;
\$n = intval(\$n / 10);
}
return \$sum;
}
//  Find the sum and product of even digit sum in linked list
public	function evenSumProduct()
{
if (\$this->head == null)
{
echo "\n Empty linked list\n";
return;
}
// Define auxiliary variable
\$sum = 0;
\$product = 1;
\$status = false;
// iterate linked list elements
while (\$auxiliary != null)
{
if (\$auxiliary->data >= 0 && (\$this->digitSum(\$auxiliary->data) % 2 == 0))
{
// When digit sum is even
\$status = true;
// Calculate sum
\$sum += \$auxiliary->data;
// Calculate product
\$product *= \$auxiliary->data;
}
// Visit to next node
\$auxiliary = \$auxiliary->next;
}
if (\$status == false)
{
// When Even sum digits not present
echo "\n None ";
}
else
{
// Display sum and product
echo " Sum : ". \$sum;
echo "\n Product : ". \$product ."\n";
}
}
}

function main()
{
\$sll = new SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
\$sll->display();
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
\$sll->evenSumProduct();
}
main();``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88``````
``````/*
Node Js Program for
Find sum and product of all even digit sum in a Singly Linked List
*/
// Linked list node
{
constructor(data)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
constructor()
{
this.tail = null;
}
//Add new Node at end of linked list
{
var node = new LinkNode(data);
if (this.head == null)
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
display()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
var temp = this.head;
//iterating linked list elements
while (temp != null)
{
process.stdout.write(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
process.stdout.write(" NULL\n");
}
// Returns the sum of digits of a given number
digitSum(num)
{
var sum = 0;
var n = num;
while (n > 0)
{
sum += n % 10;
n = parseInt(n / 10);
}
return sum;
}
//  Find the sum and product of even digit sum in linked list
evenSumProduct()
{
if (this.head == null)
{
process.stdout.write("\n Empty linked list\n");
return;
}
// Define auxiliary variable
var auxiliary = this.head;
var sum = 0;
var product = 1;
var status = false;
// iterate linked list elements
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.digitSum(auxiliary.data) % 2 == 0))
{
// When digit sum is even
status = true;
// Calculate sum
sum += auxiliary.data;
// Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (status == false)
{
// When Even sum digits not present
process.stdout.write("\n None ");
}
else
{
// Display sum and product
process.stdout.write(" Sum : " + sum);
process.stdout.write("\n Product : " + product + "\n");
}
}
}

function main()
{
var sll = new SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display();
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
sll.evenSumProduct();
}
main();``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88``````
``````#   Python 3 Program for
#   Find sum and product of all even digit sum in a Singly Linked List

#  Linked list node

def __init__(self, data) :
self.data = data
self.next = None

class SingleLL :

def __init__(self) :
self.tail = None

# Add new Node at end of linked list
def addNode(self, data) :
if (self.head == None) :
else :
#  Append the node at last position
self.tail.next = node

self.tail = node

#  Display linked list element
def display(self) :
if (self.head == None) :
print("\n Empty linked list")
return

# iterating linked list elements
while (temp != None) :
print("", temp.data ,"→", end = "")
#  Visit to next node
temp = temp.next

print(" NULL")

#  Returns the sum of digits of a given number
def digitSum(self, num) :
sum = 0
n = num
while (n > 0) :
sum += n % 10
n = int(n / 10)

return sum

#   Find the sum and product of even digit sum in linked list
def evenSumProduct(self) :
if (self.head == None) :
print("\n Empty linked list")
return

#  Define auxiliary variable
sum = 0
product = 1
status = False
#  iterate linked list elements
while (auxiliary != None) :
if (auxiliary.data >= 0 and(self.digitSum(auxiliary.data) % 2 == 0)) :
#  When digit sum is even
status = True
#  Calculate sum
sum += auxiliary.data
#  Calculate product
product *= auxiliary.data

#  Visit to next node
auxiliary = auxiliary.next

if (status == False) :
#  When Even sum digits not present
print("\n None ", end = "")
else :
#  Display sum and product
print(" Sum : ", sum, end = "")
print("\n Product : ", product )

def main() :
sll = SingleLL()
#   Constructed linked list
#   10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display()
#  2 + 11 + 4 = 17
#  2 * 11 * 4 = 88
sll.evenSumProduct()

if __name__ == "__main__": main()``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum :  17
Product :  88``````
``````#   Ruby Program for
#   Find sum and product of all even digit sum in a Singly Linked List

#  Linked list node
# Define the accessor and reader of class LinkNode
attr_accessor :data, :next

def initialize(data)
self.data = data
self.next = nil
end

end

class SingleLL
# Define the accessor and reader of class SingleLL

def initialize()
self.tail = nil
end

# Add new Node at end of linked list
if (self.head == nil)
else
#  Append the node at last position
self.tail.next = node
end

self.tail = node
end

#  Display linked list element
def display()
if (self.head == nil)
print("\n Empty linked list\n")
return
end

# iterating linked list elements
while (temp != nil)
print(" ", temp.data ," →")
#  Visit to next node
temp = temp.next
end

print(" NULL\n")
end

#  Returns the sum of digits of a given number
def digitSum(num)
sum = 0
n = num
while (n > 0)
sum += n % 10
n = n / 10
end

return sum
end

#   Find the sum and product of even digit sum in linked list
def evenSumProduct()
if (self.head == nil)
print("\n Empty linked list\n")
return
end

#  Define auxiliary variable
sum = 0
product = 1
status = false
#  iterate linked list elements
while (auxiliary != nil)
if (auxiliary.data >= 0 && (self.digitSum(auxiliary.data) % 2 == 0))
#  When digit sum is even
status = true
#  Calculate sum
sum += auxiliary.data
#  Calculate product
product *= auxiliary.data
end

#  Visit to next node
auxiliary = auxiliary.next
end

if (status == false)
#  When Even sum digits not present
print("\n None ")
else
#  Display sum and product
print(" Sum : ", sum)
print("\n Product : ", product ,"\n")
end

end

end

def main()
sll = SingleLL.new()
#   Constructed linked list
#   10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display()
#  2 + 11 + 4 = 17
#  2 * 11 * 4 = 88
sll.evenSumProduct()
end

main()``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88
``````
``````/*
Scala Program for
Find sum and product of all even digit sum in a Singly Linked List
*/
// Linked list node
class LinkNode(var data: Int , var next: LinkNode)
{
def this(data: Int)
{
this(data, null);
}
}
{
def this()
{
this(null, null);
}
//Add new Node at end of linked list
def addNode(data: Int): Unit = {
if (this.head == null)
{
}
else
{
// Append the node at last position
this.tail.next = node;
}
this.tail = node;
}
// Display linked list element
def display(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
// Returns the sum of digits of a given number
def digitSum(num: Int): Int = {
var sum: Int = 0;
var n: Int = num;
while (n > 0)
{
sum += n % 10;
n = (n / 10).toInt;
}
return sum;
}
//  Find the sum and product of even digit sum in linked list
def evenSumProduct(): Unit = {
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// Define auxiliary variable
var sum: Int = 0;
var product: Int = 1;
var status: Boolean = false;
// iterate linked list elements
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.digitSum(auxiliary.data) % 2 == 0))
{
// When digit sum is even
status = true;
// Calculate sum
sum += auxiliary.data;
// Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (status == false)
{
// When Even sum digits not present
print("\n None ");
}
else
{
// Display sum and product
print(" Sum : " + sum);
print("\n Product : " + product + "\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var sll: SingleLL = new SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display();
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
sll.evenSumProduct();
}
}``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88``````
``````/*
Swift 4 Program for
Find sum and product of all even digit sum in a Singly Linked List
*/
// Linked list node
{
var data: Int;
var next: LinkNode? ;
init(_ data: Int)
{
self.data = data;
self.next = nil;
}
}
class SingleLL
{
var tail: LinkNode? ;
init()
{
self.tail = nil;
}
//Add new Node at end of linked list
func addNode(_ data: Int)
{
if (self.head == nil)
{
}
else
{
// Append the node at last position
self.tail!.next = node;
}
self.tail = node;
}
// Display linked list element
func display()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
//iterating linked list elements
while (temp  != nil)
{
print(" ", temp!.data ," →", terminator: "");
// Visit to next node
temp = temp!.next;
}
print(" NULL");
}
// Returns the sum of digits of a given number
func digitSum(_ num: Int)->Int
{
var sum: Int = 0;
var n: Int = num;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
//  Find the sum and product of even digit sum in linked list
func evenSumProduct()
{
if (self.head == nil)
{
print("\n Empty linked list");
return;
}
// Define auxiliary variable
var sum: Int = 0;
var product: Int = 1;
var status: Bool = false;
// iterate linked list elements
while (auxiliary  != nil)
{
if (auxiliary!.data >= 0 && (self.digitSum(auxiliary!.data) % 2 == 0))
{
// When digit sum is even
status = true;
// Calculate sum
sum += auxiliary!.data;
// Calculate product
product *= auxiliary!.data;
}
// Visit to next node
auxiliary = auxiliary!.next;
}
if (status == false)
{
// When Even sum digits not present
print("\n None ", terminator: "");
}
else
{
// Display sum and product
print(" Sum : ", sum, terminator: "");
print("\n Product : ", product );
}
}
}
func main()
{
let sll: SingleLL = SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display();
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
sll.evenSumProduct();
}
main();``````

#### Output

``````  10  →  2  →  14  →  3  →  11  →  16  →  4  → NULL
Sum :  17
Product :  88``````
``````/*
Kotlin Program for
Find sum and product of all even digit sum in a Singly Linked List
*/
// Linked list node
{
var data: Int;
var next: LinkNode ? ;
constructor(data: Int)
{
this.data = data;
this.next = null;
}
}
class SingleLL
{
var tail: LinkNode ? ;
constructor()
{
this.tail = null;
}
//Add new Node at end of linked list
fun addNode(data: Int): Unit
{
if (this.head == null)
{
}
else
{
// Append the node at last position
this.tail?.next = node;
}
this.tail = node;
}
// Display linked list element
fun display(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
//iterating linked list elements
while (temp != null)
{
print(" " + temp.data + " →");
// Visit to next node
temp = temp.next;
}
print(" NULL\n");
}
// Returns the sum of digits of a given number
fun digitSum(num: Int): Int
{
var sum: Int = 0;
var n: Int = num;
while (n > 0)
{
sum += n % 10;
n = n / 10;
}
return sum;
}
//  Find the sum and product of even digit sum in linked list
fun evenSumProduct(): Unit
{
if (this.head == null)
{
print("\n Empty linked list\n");
return;
}
// Define auxiliary variable
var sum: Int = 0;
var product: Int = 1;
var status: Boolean = false;
// iterate linked list elements
while (auxiliary != null)
{
if (auxiliary.data >= 0 && (this.digitSum(auxiliary.data) % 2 == 0))
{
// When digit sum is even
status = true;
// Calculate sum
sum += auxiliary.data;
// Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (status == false)
{
// When Even sum digits not present
print("\n None ");
}
else
{
// Display sum and product
print(" Sum : " + sum);
print("\n Product : " + product + "\n");
}
}
}
fun main(args: Array <String> ): Unit
{
var sll: SingleLL = SingleLL();
//  Constructed linked list
//  10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
sll.display();
// 2 + 11 + 4 = 17
// 2 * 11 * 4 = 88
sll.evenSumProduct();
}``````

#### Output

`````` 10 → 2 → 14 → 3 → 11 → 16 → 4 → NULL
Sum : 17
Product : 88``````

## 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.