Posted on by Kalkicode

# Find sum and product of divisible key in linked list

Here given code implementation process.

// C Program
// Find sum and product of divisible key 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");
}
//Calculate sum and product of nodes which is divisible by given key
void find_sum_product(struct Node *head, int key)
{
{
}
else
{
// Define usful resultant variable
int sum = 0;
int product = 1;
int find = 0;
// Get first node of linked list
while (auxiliary != NULL)
{
if (auxiliary->data % key == 0)
{
// When node value is divisible by key
// Active the result status
find = 1;
//Calculate the sum of node value
sum += auxiliary->data;
//Calculate product
product *= auxiliary->data;
}
// Visit to next node
auxiliary = auxiliary->next;
}
//Display result
if (find == 1)
{
printf(" Given key : %d", key);
printf("\n Sum       :  %d", sum);
printf("\n Product   :  %d\n", product);
}
else
{
printf(" Given key [%d] not exist\n", key);
}
}
}
int main()
{
//  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
return 0;
}

#### Output

2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum       :  18
Product   :  162
// Java Program
// Find sum and product of divisible key 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");
}
//Calculate sum and product of nodes which is divisible by given key
public void find_sum_product(int key)
{
{
}
else
{
this.display();
// Define usful resultant variable
int sum = 0;
int product = 1;
int find = 0;
// Get first node of linked list
while (auxiliary != null)
{
if (auxiliary.data % key == 0)
{
// When node value is divisible by key
// Active the result status
find = 1;
//Calculate the sum of node value
sum += auxiliary.data;
//Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
//Display result
if (find == 1)
{
System.out.print(" Given key : " + key);
System.out.print("\n Sum : " + sum);
System.out.print("\n Product : " + product + "\n");
}
else
{
System.out.print(" Given key [" + key + "] not exist\n");
}
}
}
public static void main(String[] args)
{
//  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
obj.find_sum_product(3);
}
}

#### Output

Linked List :  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum : 18
Product : 162
#include <iostream>
using namespace std;

// C++ Program
// Find sum and product of divisible key in linked list

class Node
{
public: int data;
Node *next;
Node(int data)
{
//Set node value
this->data = data;
this->next = NULL;
}
};
{
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;
}
cout << "Linked List : ";
while (temp != NULL)
{
{
cout << " →";
}
cout << " " << temp->data;
//visit to next node
temp = temp->next;
}
cout << " → NULL\n";
}
//Calculate sum and product of nodes which is divisible by given key
void find_sum_product(int key)
{
{
cout << "\n Empty linked List";
}
else
{
this->display();
// Define usful resultant variable
int sum = 0;
int product = 1;
int find = 0;
// Get first node of linked list
while (auxiliary != NULL)
{
if (auxiliary->data % key == 0)
{
// When node value is divisible by key
// Active the result status
find = 1;
//Calculate the sum of node value
sum += auxiliary->data;
//Calculate product
product *= auxiliary->data;
}
// Visit to next node
auxiliary = auxiliary->next;
}
//Display result
if (find == 1)
{
cout << " Given key : " << key;
cout << "\n Sum : " << sum;
cout << "\n Product : " << product << "\n";
}
else
{
cout << " Given key [" << key << "] not exist\n";
}
}
}
};
int main()
{
//  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
obj.find_sum_product(3);
return 0;
}

#### Output

Linked List :  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum : 18
Product : 162
//Include namespace system
using System;
// C# Program
// Find sum and product of divisible key 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");
}
//Calculate sum and product of nodes which is divisible by given key
public void find_sum_product(int key)
{
{
}
else
{
this.display();
// Define usful resultant variable
int sum = 0;
int product = 1;
int find = 0;
// Get first node of linked list
while (auxiliary != null)
{
if (auxiliary.data % key == 0)
{
// When node value is divisible by key
// Active the result status
find = 1;
//Calculate the sum of node value
sum += auxiliary.data;
//Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
//Display result
if (find == 1)
{
Console.Write(" Given key : " + key);
Console.Write("\n Sum : " + sum);
Console.Write("\n Product : " + product + "\n");
}
else
{
Console.Write(" Given key [" + key + "] not exist\n");
}
}
}
public static void Main(String[] args)
{
//  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
obj.find_sum_product(3);
}
}

#### Output

Linked List :  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum : 18
Product : 162
<?php
// Php Program
// Find sum and product of divisible key 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";
}
//Calculate sum and product of nodes which is divisible by given key
public	function find_sum_product(\$key)
{
{
}
else
{
\$this->display();
// Define usful resultant variable
\$sum = 0;
\$product = 1;
\$find = 0;
// Get first node of linked list
while (\$auxiliary != null)
{
if (\$auxiliary->data % \$key == 0)
{
// When node value is divisible by key
// Active the result status
\$find = 1;
//Calculate the sum of node value
\$sum += \$auxiliary->data;
//Calculate product
\$product *= \$auxiliary->data;
}
// Visit to next node
\$auxiliary = \$auxiliary->next;
}
//Display result
if (\$find == 1)
{
echo " Given key : ". \$key;
echo "\n Sum : ". \$sum;
echo "\n Product : ". \$product ."\n";
}
else
{
echo " Given key [". \$key ."] not exist\n";
}
}
}
}

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

#### Output

Linked List :  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum : 18
Product : 162
// Node Js Program
// Find sum and product of divisible key 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");
}
//Calculate sum and product of nodes which is divisible by given key
find_sum_product(key)
{
{
}
else
{
this.display();
// Define usful resultant variable
var sum = 0;
var product = 1;
var find = 0;
// Get first node of linked list
while (auxiliary != null)
{
if (auxiliary.data % key == 0)
{
// When node value is divisible by key
// Active the result status
find = 1;
//Calculate the sum of node value
sum += auxiliary.data;
//Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
//Display result
if (find == 1)
{
process.stdout.write(" Given key : " + key);
process.stdout.write("\n Sum : " + sum);
process.stdout.write("\n Product : " + product + "\n");
}
else
{
process.stdout.write(" Given key [" + key + "] not exist\n");
}
}
}
}

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

#### Output

Linked List :  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum : 18
Product : 162
#  Python 3 Program
#  Find sum and product of divisible key 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

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

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

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

# Calculate sum and product of nodes which is divisible by given key
def find_sum_product(self, key) :
print("\n Empty linked List", end = "")
else :
self.display()
#  Define usful resultant variable
sum = 0
product = 1
find = 0
#  Get first node of linked list
while (auxiliary != None) :
if (auxiliary.data % key == 0) :
#  When node value is divisible by key
#  Active the result status
find = 1
# Calculate the sum of node value
sum += auxiliary.data
# Calculate product
product *= auxiliary.data

#  Visit to next node
auxiliary = auxiliary.next

# Display result
if (find == 1) :
print(" Given key : ", key, end = "")
print("\n Sum : ", sum, end = "")
print("\n Product : ", product ,"\n", end = "")
else :
print(" Given key [", key ,"] not exist\n", end = "")

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

if __name__ == "__main__": main()

#### Output

Linked List :   2 →  9 →  1 →  5 →  6 →  4 →  3 →  7 → NULL
Given key :  3
Sum :  18
Product :  162
#  Ruby Program
#  Find sum and product of divisible key 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
# Calculate sum and product of nodes which is divisible by given key
def find_sum_product(key)

else

self.display()
#  Define usful resultant variable
sum = 0
product = 1
find = 0
#  Get first node of linked list
while (auxiliary != nil)

if (auxiliary.data % key == 0)

#  When node value is divisible by key
#  Active the result status
find = 1
# Calculate the sum of node value
sum += auxiliary.data
# Calculate product
product *= auxiliary.data
end
#  Visit to next node
auxiliary = auxiliary.next
end
# Display result
if (find == 1)

print(" Given key : ", key)
print("\n Sum : ", sum)
print("\n Product : ", product ,"\n")
else

print(" Given key [", key ,"] not exist\n")
end
end
end
end
def main()

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

#### Output

Linked List :  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum : 18
Product : 162
// Scala Program
// Find sum and product of divisible key 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");
}
//Calculate sum and product of nodes which is divisible by given key
def find_sum_product(key: Int): Unit = {
{
}
else
{
this.display();
// Define usful resultant variable
var sum: Int = 0;
var product: Int = 1;
var find: Int = 0;
// Get first node of linked list
while (auxiliary != null)
{
if (auxiliary.data % key == 0)
{
// When node value is divisible by key
// Active the result status
find = 1;
//Calculate the sum of node value
sum += auxiliary.data;
//Calculate product
product *= auxiliary.data;
}
// Visit to next node
auxiliary = auxiliary.next;
}
//Display result
if (find == 1)
{
print(" Given key : " + key);
print("\n Sum : " + sum);
print("\n Product : " + product + "\n");
}
else
{
print(" Given key [" + key + "] not exist\n");
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
//  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
obj.find_sum_product(3);
}
}

#### Output

Linked List :  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
Given key : 3
Sum : 18
Product : 162
// Swift 4 Program
// Find sum and product of divisible key 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;
}
print("Linked List : ", terminator: "");
while (temp != nil)
{
{
print(" →", terminator: "");
}
print(" ", temp!.data, terminator: "");
//visit to next node
temp = temp!.next;
}
print(" → NULL\n", terminator: "");
}
//Calculate sum and product of nodes which is divisible by given key
func find_sum_product(_ key: Int)
{
{
print("\n Empty linked List", terminator: "");
}
else
{
self.display();
// Define usful resultant variable
var sum: Int = 0;
var product: Int = 1;
var find: Int = 0;
// Get first node of linked list
while (auxiliary != nil)
{
if (auxiliary!.data % key == 0)
{
// When node value is divisible by key
// Active the result status
find = 1;
//Calculate the sum of node value
sum += auxiliary!.data;
//Calculate product
product *= auxiliary!.data;
}
// Visit to next node
auxiliary = auxiliary!.next;
}
//Display result
if (find == 1)
{
print(" Given key : ", key, terminator: "");
print("\n Sum : ", sum, terminator: "");
print("\n Product : ", product ,"\n", terminator: "");
}
else
{
print(" Given key [", key ,"]not exist\n", terminator: "");
}
}
}
}
func main()
{
//  2 → 9 → 1 → 5 → 6 → 4 → 3 → 7 → NULL
obj.find_sum_product(3);
}
main();

#### Output

Linked List :   2 →  9 →  1 →  5 →  6 →  4 →  3 →  7 → NULL
Given key :  3
Sum :  18
Product :  162

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