# Find sum and product of prime nodes in linked list

Here given code implementation process.

``````// C Program
// Find sum and product of prime nodes in linked list
#include <stdio.h>

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

struct Node
{
int data;
struct Node *next;
};
//Create a node of linked list
struct Node *create_node(int data)
{
//Create dynamic node
struct Node *node = (struct Node *) malloc(sizeof(struct Node));
if (node == NULL)
{
printf("Memory overflow\n");
}
else
{
//Set initial node value
node->data = data;
node->next = NULL;
}
return node;
}
{
struct Node *node = create_node(data);
{
}
else
{
//Find last node
while (temp->next != NULL)
{
temp = temp->next;
}
temp->next = node;
}
}
{
{
return;
}
while (temp != NULL)
{
{
printf(" →");
}
printf(" %d", temp->data);
//visit to next node
temp = temp->next;
}
printf(" → NULL\n");
}
//Check that whether given number is prime or not
int is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return 1;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return 0;
}
int i = 11;
while ((i *i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return 0;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return 0;
}
i = i + 6;
}
return 1;
}
//Calculate sum and product of prime node values in linked list
{
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
int sum = 0;
int product = 1;
int find = 0;
while (auxiliary != NULL)
{
//Check node value is prime or not
if (is_prime(auxiliary->data))
{
//Active the find of prime nodes
find = 1;
//Calculating the sum of prime node value
sum += auxiliary->data;
product *= auxiliary->data;
}
// Visit to next node
auxiliary = auxiliary->next;
}
if (find == 0)
{
printf("\n Prime node not exists");
}
else
{
//Display result
printf(" Prime Sum     : %d", sum);
printf("\n Prime Product : %d\n", product);
}
}
}
int main()
{
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
return 0;
}``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30``````
``````// Java Program
// Find sum and product of prime nodes in linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
System.out.print(" →");
}
System.out.print(" " + temp.data);
//visit to next node
temp = temp.next;
}
System.out.print(" → NULL\n");
}
//Check that whether given number is prime or not
public boolean is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Calculate sum and product of prime node values in linked list
public void find_sum_product()
{
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
int sum = 0;
int product = 1;
boolean find = false;
while (auxiliary != null)
{
//Check node value is prime or not
if (is_prime(auxiliary.data))
{
//Active the find of prime nodes
find = true;
//Calculating the sum of prime node value
sum += auxiliary.data;
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (find == false)
{
System.out.print("\n Prime node not exists\n");
}
else
{
//Display result
System.out.print(" Prime Sum     : " + sum + "");
System.out.print("\n Prime Product : " + product + "\n");
}
}
}
public static void main(String[] args)
{
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display();
obj.find_sum_product();
}
}``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Find sum and product of prime nodes in linked list

class Node
{
public: int data;
Node *next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
public:
Node *tail;
//Class constructor
{
this->tail = NULL;
}
//insert node at last of linke list
{
//Create a node
Node *node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
while (temp != NULL)
{
{
cout << " →";
}
cout << " " << temp->data;
//visit to next node
temp = temp->next;
}
cout << " → NULL\n";
}
//Check that whether given number is prime or not
bool is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i *i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Calculate sum and product of prime node values in linked list
void find_sum_product()
{
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
int sum = 0;
int product = 1;
bool find = false;
while (auxiliary != NULL)
{
//Check node value is prime or not
if (this->is_prime(auxiliary->data))
{
//Active the find of prime nodes
find = true;
//Calculating the sum of prime node value
sum += auxiliary->data;
product *= auxiliary->data;
}
// Visit to next node
auxiliary = auxiliary->next;
}
if (find == false)
{
cout << "\n Prime node not exists\n";
}
else
{
//Display result
cout << " Prime Sum     : " << sum << "";
cout << "\n Prime Product : " << product << "\n";
}
}
}
};
int main()
{
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display();
obj.find_sum_product();
return 0;
}``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30``````
``````//Include namespace system
using System;
// C# Program
// Find sum and product of prime nodes in linked list

class Node
{
public int data;
public Node next;
public Node(int data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
public Node tail;
//Class constructor
{
this.tail = null;
}
//insert node at last of linke list
{
//Create a node
Node node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
public void display()
{
{
return;
}
while (temp != null)
{
{
Console.Write(" →");
}
Console.Write(" " + temp.data);
//visit to next node
temp = temp.next;
}
Console.Write(" → NULL\n");
}
//Check that whether given number is prime or not
public Boolean is_prime(int num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
int i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Calculate sum and product of prime node values in linked list
public void find_sum_product()
{
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
int sum = 0;
int product = 1;
Boolean find = false;
while (auxiliary != null)
{
//Check node value is prime or not
if (is_prime(auxiliary.data))
{
//Active the find of prime nodes
find = true;
//Calculating the sum of prime node value
sum += auxiliary.data;
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (find == false)
{
Console.Write("\n Prime node not exists\n");
}
else
{
//Display result
Console.Write(" Prime Sum     : " + sum + "");
Console.Write("\n Prime Product : " + product + "\n");
}
}
}
public static void Main(String[] args)
{
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display();
obj.find_sum_product();
}
}``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30``````
``````<?php
// Php Program
// Find sum and product of prime nodes in linked list

class Node
{
public \$data;
public \$next;

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructor
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
while (\$temp != null)
{
{
echo " →";
}
echo " ". \$temp->data;
//visit to next node
\$temp = \$temp->next;
}
echo " → NULL\n";
}
//Check that whether given number is prime or not
public	function is_prime(\$num)
{
if (\$num == 2 || \$num == 3 || \$num == 5)
{
return true;
}
if (\$num <= 1 || (\$num % 2 == 0) || (\$num % 3 == 0) || (\$num % 5 == 0))
{
return false;
}
\$i = 11;
while ((\$i * \$i) <= \$num)
{
if (\$num % \$i == 0)
{
//When number is divisible of current i value
return false;
}
else if (\$num % (\$i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
\$i = \$i + 6;
}
return true;
}
//Calculate sum and product of prime node values in linked list
public	function find_sum_product()
{
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
\$sum = 0;
\$product = 1;
\$find = false;
while (\$auxiliary != null)
{
//Check node value is prime or not
if (\$this->is_prime(\$auxiliary->data))
{
//Active the find of prime nodes
\$find = true;
//Calculating the sum of prime node value
\$sum += \$auxiliary->data;
\$product *= \$auxiliary->data;
}
// Visit to next node
\$auxiliary = \$auxiliary->next;
}
if (\$find == false)
{
echo "\n Prime node not exists\n";
}
else
{
//Display result
echo " Prime Sum     : ". \$sum ."";
echo "\n Prime Product : ". \$product ."\n";
}
}
}
}

function main()
{
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
\$obj->display();
\$obj->find_sum_product();
}
main();``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30``````
``````// Node Js Program
// Find sum and product of prime nodes in linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructor
constructor()
{
this.tail = null;
}
//insert node at last of linke list
{
//Create a node
var node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
display()
{
{
return;
}
while (temp != null)
{
{
process.stdout.write(" →");
}
process.stdout.write(" " + temp.data);
//visit to next node
temp = temp.next;
}
process.stdout.write(" → NULL\n");
}
//Check that whether given number is prime or not
is_prime(num)
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Calculate sum and product of prime node values in linked list
find_sum_product()
{
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
var sum = 0;
var product = 1;
var find = false;
while (auxiliary != null)
{
//Check node value is prime or not
if (this.is_prime(auxiliary.data))
{
//Active the find of prime nodes
find = true;
//Calculating the sum of prime node value
sum += auxiliary.data;
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (find == false)
{
process.stdout.write("\n Prime node not exists\n");
}
else
{
//Display result
process.stdout.write(" Prime Sum     : " + sum + "");
process.stdout.write("\n Prime Product : " + product + "\n");
}
}
}
}

function main()
{
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display();
obj.find_sum_product();
}
main();``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30``````
``````#  Python 3 Program
#  Find sum and product of prime nodes in linked list

class Node :

def __init__(self, data) :
# Set node value
self.data = data
self.next = None

# Class constructor
def __init__(self) :
self.tail = None

# insert node at last of linke list
# Create a node
node = Node(data)
self.tail = node
else :
self.tail.next = node
self.tail = node

def display(self) :
print("\nEmpty linked list\n", end = "")
return

while (temp != None) :
print(" →", end = "")

print(" ", temp.data, end = "")
# visit to next node
temp = temp.next

print(" → NULL\n", end = "")

# Check that whether given number is prime or not
def is_prime(self, num) :
if (num == 2 or num == 3 or num == 5) :
return True

if (num <= 1 or(num % 2 == 0) or(num % 3 == 0) or(num % 5 == 0)) :
return False

i = 11
while ((i * i) <= num) :
if (num % i == 0) :
# When number is divisible of current i value
return False

elif(num % (i + 2) == 0) :
# When number is divisible of current i + 2 value
return False

i = i + 6

return True

# Calculate sum and product of prime node values in linked list
def find_sum_product(self) :
print("\nEmpty linked List", end = "")
else :
#  Define some useful resultant variables
#  Define usful resultant variables
sum = 0
product = 1
find = False
while (auxiliary != None) :
# Check node value is prime or not
if (self.is_prime(auxiliary.data)) :
# Active the find of prime nodes
find = True
# Calculating the sum of prime node value
sum += auxiliary.data
product *= auxiliary.data

#  Visit to next node
auxiliary = auxiliary.next

if (find == False) :
print("\n Prime node not exists\n", end = "")
else :
# Display result
print(" Prime Sum     : ", sum ,"", end = "")
print("\n Prime Product : ", product ,"\n", end = "")

def main() :
#  2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display()
obj.find_sum_product()

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

#### Output

`````` Linked Linked
2 →  9 →  1 →  5 →  6 →  4 →  3 →  8 → NULL
Prime Sum     :  10
Prime Product :  30``````
``````#  Ruby Program
#  Find sum and product of prime nodes in linked list

class Node

# Define the accessor and reader of class Node
attr_accessor :data, :next
def initialize(data)

# Set node value
self.data = data
self.next = nil
end
end

# Class constructor
def initialize()

self.tail = nil
end
# insert node at last of linke list

# Create a node
node = Node.new(data)

self.tail = node
else

self.tail.next = node
self.tail = node
end
end
def display()

return
end
while (temp != nil)

print(" →")
end
print(" ", temp.data)
# visit to next node
temp = temp.next
end
print(" → NULL\n")
end
# Check that whether given number is prime or not
def is_prime(num)

if (num == 2 || num == 3 || num == 5)

return true
end
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))

return false
end
i = 11
while ((i * i) <= num)

if (num % i == 0)

# When number is divisible of current i value
return false
elsif(num % (i + 2) == 0)

# When number is divisible of current i + 2 value
return false
end
i = i + 6
end
return true
end
# Calculate sum and product of prime node values in linked list
def find_sum_product()

else

#  Define some useful resultant variables
#  Define usful resultant variables
sum = 0
product = 1
find = false
while (auxiliary != nil)

# Check node value is prime or not
if (self.is_prime(auxiliary.data))

# Active the find of prime nodes
find = true
# Calculating the sum of prime node value
sum += auxiliary.data
product *= auxiliary.data
end
#  Visit to next node
auxiliary = auxiliary.next
end
if (find == false)

print("\n Prime node not exists\n")
else

# Display result
print(" Prime Sum     : ", sum ,"")
print("\n Prime Product : ", product ,"\n")
end
end
end
end
def main()

#  2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display()
obj.find_sum_product()
end
main()``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30
``````
``````// Scala Program
// Find sum and product of prime nodes in linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructor
def this()
{
this(null, null);
}
//insert node at last of linke list
def add_node(data: Int): Unit = {
//Create a node
var node: Node = new Node(data);
{
this.tail = node;
}
else
{
this.tail.next = node;
this.tail = node;
}
}
def display(): Unit = {
{
return;
}
while (temp != null)
{
{
print(" →");
}
print(" " + temp.data);
//visit to next node
temp = temp.next;
}
print(" → NULL\n");
}
//Check that whether given number is prime or not
def is_prime(num: Int): Boolean = {
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Calculate sum and product of prime node values in linked list
def find_sum_product(): Unit = {
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
var sum: Int = 0;
var product: Int = 1;
var find: Boolean = false;
while (auxiliary != null)
{
//Check node value is prime or not
if (is_prime(auxiliary.data))
{
//Active the find of prime nodes
find = true;
//Calculating the sum of prime node value
sum += auxiliary.data;
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
if (find == false)
{
print("\n Prime node not exists\n");
}
else
{
//Display result
print(" Prime Sum     : " + sum + "");
print("\n Prime Product : " + product + "\n");
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display();
obj.find_sum_product();
}
}``````

#### Output

`````` Linked Linked
2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
Prime Sum     : 10
Prime Product : 30``````
``````// Swift 4 Program
// Find sum and product of prime nodes in linked list

class Node
{
var data: Int;
var next: Node? ;
init(_ data: Int)
{
//Set node value
self.data = data;
self.next = nil;
}
}
{
var tail: Node? ;
//Class constructor
init()
{
self.tail = nil;
}
//insert node at last of linke list
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
while (temp != nil)
{
{
print(" →", terminator: "");
}
print(" ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print(" → NULL\n", terminator: "");
}
//Check that whether given number is prime or not
func is_prime(_ num: Int) -> Bool
{
if (num == 2 || num == 3 || num == 5)
{
return true;
}
if (num <= 1 || (num % 2 == 0) || (num % 3 == 0) || (num % 5 == 0))
{
return false;
}
var i: Int = 11;
while ((i * i) <= num)
{
if (num % i == 0)
{
//When number is divisible of current i value
return false;
}
else if (num % (i + 2) == 0)
{
//When number is divisible of current i + 2 value
return false;
}
i = i + 6;
}
return true;
}
//Calculate sum and product of prime node values in linked list
func find_sum_product()
{
{
}
else
{
// Define some useful resultant variables
// Define usful resultant variables
var sum: Int = 0;
var product: Int = 1;
var find: Bool = false;
while (auxiliary != nil)
{
//Check node value is prime or not
if (self.is_prime(auxiliary!.data))
{
//Active the find of prime nodes
find = true;
//Calculating the sum of prime node value
sum += auxiliary!.data;
product *= auxiliary!.data;
}
// Visit to next node
auxiliary = auxiliary!.next;
}
if (find == false)
{
print("\n Prime node not exists\n", terminator: "");
}
else
{
//Display result
print(" Prime Sum     : ", sum ,"", terminator: "");
print("\n Prime Product : ", product ,"\n", terminator: "");
}
}
}
}
func main()
{
// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
obj.display();
obj.find_sum_product();
}
main();``````

#### Output

`````` Linked Linked
2 →  9 →  1 →  5 →  6 →  4 →  3 →  8 → NULL
Prime Sum     :  10
Prime Product :  30``````

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