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

//Linked List Node
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;
}
//Add new node at end of linked list 
void add_node(struct Node **head, int data)
{
	struct Node *node = create_node(data);
	if ( *head == NULL)
	{
		*head = node;
	}
	else
	{
		struct Node *temp = *head;
		//Find last node
		while (temp->next != NULL)
		{
			temp = temp->next;
		}
		//Add node at last possition
		temp->next = node;
	}
}
//Display linked list element
void display(struct Node *head)
{
	if (head == NULL)
	{
		printf("\nEmpty linked list\n");
		return;
	}
	struct Node *temp = head;
	//iterating linked list elements
	while (temp != NULL)
	{
		if (temp != head)
		{
			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
void find_sum_product(struct Node *head)
{
	if (head == NULL)
	{
		printf("\nEmpty linked List");
	}
	else
	{
		// Define some useful resultant variables
		struct Node *auxiliary = head;
		// Define usful resultant variables
		int sum = 0;
		int product = 1;
		int find = 0;
		// iterating linked list elements
		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()
{
	struct Node *head = NULL;
	//Add linked list node
	add_node( &head, 2);
	add_node( &head, 9);
	add_node( &head, 1);
	add_node( &head, 5);
	add_node( &head, 6);
	add_node( &head, 4);
	add_node( &head, 3);
	add_node( &head, 8);
	printf(" Linked Linked \n");
	// 2 → 9 → 1 → 5 → 6 → 4 → 3 → 8 → NULL
	display(head);
	find_sum_product(head);
	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

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void add_node(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			System.out.print("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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()
	{
		if (this.head == null)
		{
			System.out.print("\nEmpty linked List");
		}
		else
		{
			// Define some useful resultant variables
			Node auxiliary = this.head;
			// Define usful resultant variables
			int sum = 0;
			int product = 1;
			boolean find = false;
			// iterating linked list elements
			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)
	{
		MyLinkedList obj = new MyLinkedList();
		//Add linked list node
		obj.add_node(2);
		obj.add_node(9);
		obj.add_node(1);
		obj.add_node(5);
		obj.add_node(6);
		obj.add_node(4);
		obj.add_node(3);
		obj.add_node(8);
		System.out.print(" Linked Linked \n");
		// 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

//Node of LinkedList
class Node
{
	public: int data;
	Node *next;
	Node(int data)
	{
		//Set node value
		this->data = data;
		this->next = NULL;
	}
};
class MyLinkedList
{
	public: 
    Node *head;
	Node *tail;
	//Class constructor
	MyLinkedList()
	{
		this->head = NULL;
		this->tail = NULL;
	}
	//insert node at last of linke list
	void add_node(int data)
	{
		//Create a node
		Node *node = new Node(data);
		if (this->head == NULL)
		{
			//When linked list empty add first node
			this->head = node;
			this->tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this->tail->next = node;
			this->tail = node;
		}
	}
	//Display linked list element
	void display()
	{
		if (this->head == NULL)
		{
			cout << "\nEmpty linked list\n";
			return;
		}
		Node *temp = this->head;
		//iterating linked list elements
		while (temp != NULL)
		{
			if (temp != this->head)
			{
				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()
	{
		if (this->head == NULL)
		{
			cout << "\nEmpty linked List";
		}
		else
		{
			// Define some useful resultant variables
			Node *auxiliary = this->head;
			// Define usful resultant variables
			int sum = 0;
			int product = 1;
			bool find = false;
			// iterating linked list elements
			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()
{
	MyLinkedList obj = MyLinkedList();
	//Add linked list node
	obj.add_node(2);
	obj.add_node(9);
	obj.add_node(1);
	obj.add_node(5);
	obj.add_node(6);
	obj.add_node(4);
	obj.add_node(3);
	obj.add_node(8);
	cout << " Linked Linked \n";
	// 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

//Node of LinkedList
class Node
{
	public int data;
	public Node next;
	public Node(int data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	public Node head;
	public Node tail;
	//Class constructor
	public MyLinkedList()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	public void add_node(int data)
	{
		//Create a node
		Node node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	public void display()
	{
		if (this.head == null)
		{
			Console.Write("\nEmpty linked list\n");
			return;
		}
		Node temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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()
	{
		if (this.head == null)
		{
			Console.Write("\nEmpty linked List");
		}
		else
		{
			// Define some useful resultant variables
			Node auxiliary = this.head;
			// Define usful resultant variables
			int sum = 0;
			int product = 1;
			Boolean find = false;
			// iterating linked list elements
			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)
	{
		MyLinkedList obj = new MyLinkedList();
		//Add linked list node
		obj.add_node(2);
		obj.add_node(9);
		obj.add_node(1);
		obj.add_node(5);
		obj.add_node(6);
		obj.add_node(4);
		obj.add_node(3);
		obj.add_node(8);
		Console.Write(" Linked Linked \n");
		// 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

//Node of LinkedList
class Node
{
	public $data;
	public $next;

	function __construct($data)
	{
		//Set node value
		$this->data = $data;
		$this->next = null;
	}
}
class MyLinkedList
{
	public $head;
	public $tail;
	//Class constructor
	function __construct()
	{
		$this->head = null;
		$this->tail = null;
	}
	//insert node at last of linke list
	public	function add_node($data)
	{
		//Create a node
		$node = new Node($data);
		if ($this->head == null)
		{
			//When linked list empty add first node
			$this->head = $node;
			$this->tail = $node;
		}
		else
		{
			//Add new node at end of linked list
			$this->tail->next = $node;
			$this->tail = $node;
		}
	}
	//Display linked list element
	public	function display()
	{
		if ($this->head == null)
		{
			echo "\nEmpty linked list\n";
			return;
		}
		$temp = $this->head;
		//iterating linked list elements
		while ($temp != null)
		{
			if ($temp != $this->head)
			{
				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()
	{
		if ($this->head == null)
		{
			echo "\nEmpty linked List";
		}
		else
		{
			// Define some useful resultant variables
			$auxiliary = $this->head;
			// Define usful resultant variables
			$sum = 0;
			$product = 1;
			$find = false;
			// iterating linked list elements
			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()
{
	$obj = new MyLinkedList();
	//Add linked list node
	$obj->add_node(2);
	$obj->add_node(9);
	$obj->add_node(1);
	$obj->add_node(5);
	$obj->add_node(6);
	$obj->add_node(4);
	$obj->add_node(3);
	$obj->add_node(8);
	echo " Linked Linked \n";
	// 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

//Node of LinkedList
class Node
{
	constructor(data)
	{
		//Set node value
		this.data = data;
		this.next = null;
	}
}
class MyLinkedList
{
	//Class constructor
	constructor()
	{
		this.head = null;
		this.tail = null;
	}
	//insert node at last of linke list
	add_node(data)
	{
		//Create a node
		var node = new Node(data);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	display()
	{
		if (this.head == null)
		{
			process.stdout.write("\nEmpty linked list\n");
			return;
		}
		var temp = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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()
	{
		if (this.head == null)
		{
			process.stdout.write("\nEmpty linked List");
		}
		else
		{
			// Define some useful resultant variables
			var auxiliary = this.head;
			// Define usful resultant variables
			var sum = 0;
			var product = 1;
			var find = false;
			// iterating linked list elements
			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()
{
	var obj = new MyLinkedList();
	//Add linked list node
	obj.add_node(2);
	obj.add_node(9);
	obj.add_node(1);
	obj.add_node(5);
	obj.add_node(6);
	obj.add_node(4);
	obj.add_node(3);
	obj.add_node(8);
	process.stdout.write(" Linked Linked \n");
	// 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

# Node of LinkedList
class Node :
	
	def __init__(self, data) :
		# Set node value
		self.data = data
		self.next = None
	
class MyLinkedList :
	
	# Class constructor
	def __init__(self) :
		self.head = None
		self.tail = None
	
	# insert node at last of linke list
	def add_node(self, data) :
		# Create a node
		node = Node(data)
		if (self.head == None) :
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else :
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		
	
	# Display linked list element
	def display(self) :
		if (self.head == None) :
			print("\nEmpty linked list\n", end = "")
			return
		
		temp = self.head
		# iterating linked list elements
		while (temp != None) :
			if (temp != self.head) :
				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) :
		if (self.head == None) :
			print("\nEmpty linked List", end = "")
		else :
			#  Define some useful resultant variables
			auxiliary = self.head
			#  Define usful resultant variables
			sum = 0
			product = 1
			find = False
			#  iterating linked list elements
			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() :
	obj = MyLinkedList()
	# Add linked list node
	obj.add_node(2)
	obj.add_node(9)
	obj.add_node(1)
	obj.add_node(5)
	obj.add_node(6)
	obj.add_node(4)
	obj.add_node(3)
	obj.add_node(8)
	print(" Linked Linked \n", end = "")
	#  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

# Node of LinkedList
class Node 

	# Define the accessor and reader of class Node  
	attr_reader :data, :next
	attr_accessor :data, :next
	def initialize(data)
	
		# Set node value
		self.data = data
		self.next = nil
	end
end
class MyLinkedList 

	# Define the accessor and reader of class MyLinkedList  
	attr_reader :head, :tail
	attr_accessor :head, :tail


	
	# Class constructor
	def initialize()
	
		self.head = nil
		self.tail = nil
	end
	# insert node at last of linke list
	def add_node(data)
	
		# Create a node
		node = Node.new(data)
		if (self.head == nil)
		
			# When linked list empty add first node
			self.head = node
			self.tail = node
		else
		
			# Add new node at end of linked list
			self.tail.next = node
			self.tail = node
		end
	end
	# Display linked list element
	def display()
	
		if (self.head == nil)
		
			print("\nEmpty linked list\n")
			return
		end
		temp = self.head
		# iterating linked list elements
		while (temp != nil)
		
			if (temp != self.head)
			
				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()
	
		if (self.head == nil)
		
			print("\nEmpty linked List")
		else
		
			#  Define some useful resultant variables
			auxiliary = self.head
			#  Define usful resultant variables
			sum = 0
			product = 1
			find = false
			#  iterating linked list elements
			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()

	obj = MyLinkedList.new()
	# Add linked list node
	obj.add_node(2)
	obj.add_node(9)
	obj.add_node(1)
	obj.add_node(5)
	obj.add_node(6)
	obj.add_node(4)
	obj.add_node(3)
	obj.add_node(8)
	print(" Linked Linked \n")
	#  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

//Node of LinkedList
class Node(var data: Int,
	var next: Node)
{
	def this(data: Int)
	{
		this(data, null);
	}
}
class MyLinkedList(var head: Node,
	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);
		if (this.head == null)
		{
			//When linked list empty add first node
			this.head = node;
			this.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			this.tail.next = node;
			this.tail = node;
		}
	}
	//Display linked list element
	def display(): Unit = {
		if (this.head == null)
		{
			print("\nEmpty linked list\n");
			return;
		}
		var temp: Node = this.head;
		//iterating linked list elements
		while (temp != null)
		{
			if (temp != this.head)
			{
				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 = {
		if (this.head == null)
		{
			print("\nEmpty linked List");
		}
		else
		{
			// Define some useful resultant variables
			var auxiliary: Node = this.head;
			// Define usful resultant variables
			var sum: Int = 0;
			var product: Int = 1;
			var find: Boolean = false;
			// iterating linked list elements
			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 = {
		var obj: MyLinkedList = new MyLinkedList();
		//Add linked list node
		obj.add_node(2);
		obj.add_node(9);
		obj.add_node(1);
		obj.add_node(5);
		obj.add_node(6);
		obj.add_node(4);
		obj.add_node(3);
		obj.add_node(8);
		print(" Linked Linked \n");
		// 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

//Node of LinkedList
class Node
{
	var data: Int;
	var next: Node? ;
	init(_ data: Int)
	{
		//Set node value
		self.data = data;
		self.next = nil;
	}
}
class MyLinkedList
{
	var head: Node? ;
	var tail: Node? ;
	//Class constructor
	init()
	{
		self.head = nil;
		self.tail = nil;
	}
	//insert node at last of linke list
	func add_node(_ data: Int)
	{
		//Create a node
		let node: Node? = Node(data);
		if (self.head == nil)
		{
			//When linked list empty add first node
			self.head = node;
			self.tail = node;
		}
		else
		{
			//Add new node at end of linked list
			self.tail!.next = node;
			self.tail = node;
		}
	}
	//Display linked list element
	func display()
	{
		if (self.head == nil)
		{
			print("\nEmpty linked list\n", terminator: "");
			return;
		}
		var temp: Node? = self.head;
		//iterating linked list elements
		while (temp != nil)
		{
			if (!(temp === self.head))
			{
				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()
	{
		if (self.head == nil)
		{
			print("\nEmpty linked List", terminator: "");
		}
		else
		{
			// Define some useful resultant variables
			var auxiliary: Node? = self.head;
			// Define usful resultant variables
			var sum: Int = 0;
			var product: Int = 1;
			var find: Bool = false;
			// iterating linked list elements
			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()
{
	let obj: MyLinkedList = MyLinkedList();
	//Add linked list node
	obj.add_node(2);
	obj.add_node(9);
	obj.add_node(1);
	obj.add_node(5);
	obj.add_node(6);
	obj.add_node(4);
	obj.add_node(3);
	obj.add_node(8);
	print(" Linked Linked \n", terminator: "");
	// 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

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