Posted on by Kalkicode

# Find sum of all perfect numbers in linked list

Here given code implementation process.

``````// C Program
// Find sum of all perfect numbers in linked list

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

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;
}
void insert(struct Node **head, int data)
{
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("  %d", temp->data);
temp = temp->next;
}
}
//Get the sum of factor in a given number
int get_factor_sum(int num)
{
int sum = 0;
int i = 1;
while (i <= (num / 2))
{
if (num % i == 0)
{
// When number is divisible by number i
sum += i;
}
i++;
}
return sum;
}
//Find the sum of perfect node values in given linked list
{
{
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != NULL)
{
if (temp->data > 0 && temp->data == get_factor_sum(temp->data))
{
//When node data is perfect number
result += temp->data;
}
//Visit to next node
temp = temp->next;
}
//Display of calculated result
printf("\n Perfect number sum is : %d\n", result);
}
int main()
{
// 6 + 28  = 34
return 0;
}``````

#### Output

`````` Linked Linked :   5  28  7  6  1  21
Perfect number sum is : 34``````
``````// Java Program
// Find sum of all perfect numbers 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 constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//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("  " + temp.data);
temp = temp.next;
}
}
//Get the sum of factor in a given number
public int get_factor_sum(int num)
{
int sum = 0;
int i = 1;
while (i <= (num / 2))
{
if (num % i == 0)
{
// When number is divisible by number i
sum += i;
}
i++;
}
return sum;
}
//Find the sum of perfect node values in given linked list
public void perfect_num_sum()
{
{
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != null)
{
if (temp.data > 0 && temp.data == get_factor_sum(temp.data))
{
//When node data is perfect number
result += temp.data;
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
System.out.print("\n Perfect number sum is : " + result + "\n");
}
public static void main(String[] args)
{
obj.insert(5);
obj.insert(28);
obj.insert(7);
obj.insert(6);
obj.insert(1);
obj.insert(21);
obj.display();
// 6 + 28  = 34
obj.perfect_num_sum();
}
}``````

#### Output

`````` Linked List :   5  28  7  6  1  21
Perfect number sum is : 34``````
``````//Include header file
#include <iostream>
using namespace std;

// C++ Program
// Find sum of all perfect numbers 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 constructors
{
this->tail = NULL;
}
//insert node at last of linke list
void insert(int data)
{
//Create a node
Node * node = new Node(data);
{
this->tail = node;
}
else
{
this->tail->next = node;
this->tail = node;
}
}
void display()
{
{
return;
}
cout << "\n Linked List : ";
while (temp != NULL)
{
cout << "  " << temp->data;
temp = temp->next;
}
}
//Get the sum of factor in a given number
int get_factor_sum(int num)
{
int sum = 0;
int i = 1;
while (i <= (num / 2))
{
if (num % i == 0)
{
// When number is divisible by number i
sum += i;
}
i++;
}
return sum;
}
//Find the sum of perfect node values in given linked list
void perfect_num_sum()
{
{
cout << "\n Empty Linked List \n";
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != NULL)
{
if (temp->data > 0 && temp->data == this->get_factor_sum(temp->data))
{
//When node data is perfect number
result += temp->data;
}
//Visit to next node
temp = temp->next;
}
//Display of calculated result
cout << "\n Perfect number sum is : " << result << "\n";
}
};
int main()
{
obj.insert(5);
obj.insert(28);
obj.insert(7);
obj.insert(6);
obj.insert(1);
obj.insert(21);
obj.display();
// 6 + 28  = 34
obj.perfect_num_sum();
return 0;
}``````

#### Output

`````` Linked List :   5  28  7  6  1  21
Perfect number sum is : 34``````
``````//Include namespace system
using System;

// C# Program
// Find sum of all perfect numbers 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 constructors
{
this.tail = null;
}
//insert node at last of linke list
public void insert(int data)
{
//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("  " + temp.data);
temp = temp.next;
}
}
//Get the sum of factor in a given number
public int get_factor_sum(int num)
{
int sum = 0;
int i = 1;
while (i <= (num / 2))
{
if (num % i == 0)
{
// When number is divisible by number i
sum += i;
}
i++;
}
return sum;
}
//Find the sum of perfect node values in given linked list
public void perfect_num_sum()
{
{
return;
}
//Define resultant variable
int result = 0;
//Get first node of linked list
while (temp != null)
{
if (temp.data > 0 && temp.data == get_factor_sum(temp.data))
{
//When node data is perfect number
result += temp.data;
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
Console.Write("\n Perfect number sum is : " + result + "\n");
}
public static void Main(String[] args)
{
obj.insert(5);
obj.insert(28);
obj.insert(7);
obj.insert(6);
obj.insert(1);
obj.insert(21);
obj.display();
// 6 + 28  = 34
obj.perfect_num_sum();
}
}``````

#### Output

`````` Linked List :   5  28  7  6  1  21
Perfect number sum is : 34``````
``````<?php
// Php Program
// Find sum of all perfect numbers in linked list

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

function __construct(\$data)
{
//Set node value
\$this->data = \$data;
\$this->next = null;
}
}
{
public \$tail;
//Class constructors
function __construct()
{
\$this->tail = null;
}
//insert node at last of linke list
public	function insert(\$data)
{
//Create a node
\$node = new Node(\$data);
{
\$this->tail = \$node;
}
else
{
\$this->tail->next = \$node;
\$this->tail = \$node;
}
}
public	function display()
{
{
return;
}
echo "\n Linked List : ";
while (\$temp != null)
{
echo "  ". \$temp->data;
\$temp = \$temp->next;
}
}
//Get the sum of factor in a given number
public	function get_factor_sum(\$num)
{
\$sum = 0;
\$i = 1;
while (\$i <= (intval(\$num / 2)))
{
if (\$num % \$i == 0)
{
// When number is divisible by number i
\$sum += \$i;
}
\$i++;
}
return \$sum;
}
//Find the sum of perfect node values in given linked list
public	function perfect_num_sum()
{
{
echo "\n Empty Linked List \n";
return;
}
//Define resultant variable
\$result = 0;
//Get first node of linked list
while (\$temp != null)
{
if (\$temp->data > 0 && \$temp->data == \$this->get_factor_sum(\$temp->data))
{
//When node data is perfect number
\$result += \$temp->data;
}
//Visit to next node
\$temp = \$temp->next;
}
//Display of calculated result
echo "\n Perfect number sum is : ". \$result ."\n";
}
}

function main()
{
\$obj->insert(5);
\$obj->insert(28);
\$obj->insert(7);
\$obj->insert(6);
\$obj->insert(1);
\$obj->insert(21);
\$obj->display();
// 6 + 28  = 34
\$obj->perfect_num_sum();
}
main();``````

#### Output

`````` Linked List :   5  28  7  6  1  21
Perfect number sum is : 34``````
``````// Node Js Program
// Find sum of all perfect numbers in linked list

class Node
{
constructor(data)
{
//Set node value
this.data = data;
this.next = null;
}
}
{
//Class constructors
constructor()
{
this.tail = null;
}
//insert node at last of linke list
insert(data)
{
//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("  " + temp.data);
temp = temp.next;
}
}
//Get the sum of factor in a given number
get_factor_sum(num)
{
var sum = 0;
var i = 1;
while (i <= (parseInt(num / 2)))
{
if (num % i == 0)
{
// When number is divisible by number i
sum += i;
}
i++;
}
return sum;
}
//Find the sum of perfect node values in given linked list
perfect_num_sum()
{
{
return;
}
//Define resultant variable
var result = 0;
//Get first node of linked list
while (temp != null)
{
if (temp.data > 0 && temp.data == this.get_factor_sum(temp.data))
{
//When node data is perfect number
result += temp.data;
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
process.stdout.write("\n Perfect number sum is : " + result + "\n");
}
}

function main()
{
obj.insert(5);
obj.insert(28);
obj.insert(7);
obj.insert(6);
obj.insert(1);
obj.insert(21);
obj.display();
// 6 + 28  = 34
obj.perfect_num_sum();
}
main();``````

#### Output

`````` Linked List :   5  28  7  6  1  21
Perfect number sum is : 34``````
``````#  Python 3 Program
#  Find sum of all perfect numbers in linked list

class Node :

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

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

# insert node at last of linke list
def insert(self, data) :
# 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("\n Linked List : ", end = "")
while (temp != None) :
print("  ", temp.data, end = "")
temp = temp.next

# Get the sum of factor in a given number
def get_factor_sum(self, num) :
sum = 0
i = 1
while (i <= (int(num / 2))) :
if (num % i == 0) :
#  When number is divisible by number i
sum += i

i += 1

return sum

# Find the sum of perfect node values in given linked list
def perfect_num_sum(self) :
print("\n Empty Linked List \n", end = "")
return

# Define resultant variable
result = 0
# Get first node of linked list
while (temp != None) :
if (temp.data > 0 and temp.data == self.get_factor_sum(temp.data)) :
# When node data is perfect number
result += temp.data

# Visit to next node
temp = temp.next

# Display of calculated result
print("\n Perfect number sum is : ", result ,"\n", end = "")

def main() :
obj.insert(5)
obj.insert(28)
obj.insert(7)
obj.insert(6)
obj.insert(1)
obj.insert(21)
obj.display()
#  6 + 28  = 34
obj.perfect_num_sum()

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

#### Output

`````` Linked List :    5   28   7   6   1   21
Perfect number sum is :  34``````
``````#  Ruby Program
#  Find sum of all perfect numbers 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 constructors
def initialize()

self.tail = nil
end
# insert node at last of linke list
def insert(data)

# 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("  ", temp.data)
temp = temp.next
end
end
# Get the sum of factor in a given number
def get_factor_sum(num)

sum = 0
i = 1
while (i <= (num / 2))

if (num % i == 0)

#  When number is divisible by number i
sum += i
end
i += 1
end
return sum
end
# Find the sum of perfect node values in given linked list
def perfect_num_sum()

return
end
# Define resultant variable
result = 0
# Get first node of linked list
while (temp != nil)

if (temp.data > 0 && temp.data == self.get_factor_sum(temp.data))

# When node data is perfect number
result += temp.data
end
# Visit to next node
temp = temp.next
end
# Display of calculated result
print("\n Perfect number sum is : ", result ,"\n")
end
end
def main()

obj.insert(5)
obj.insert(28)
obj.insert(7)
obj.insert(6)
obj.insert(1)
obj.insert(21)
obj.display()
#  6 + 28  = 34
obj.perfect_num_sum()
end
main()``````

#### Output

`````` Linked List :   5  28  7  6  1  21
Perfect number sum is : 34
``````
``````// Scala Program
// Find sum of all perfect numbers in linked list

class Node(var data: Int,
var next: Node)
{
def this(data: Int)
{
this(data, null);
}
}
var tail: Node)
{
//Class constructors
def this()
{
this(null, null);
}
//insert node at last of linke list
def insert(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("  " + temp.data);
temp = temp.next;
}
}
//Get the sum of factor in a given number
def get_factor_sum(num: Int): Int = {
var sum: Int = 0;
var i: Int = 1;
while (i <= ((num / 2).toInt))
{
if (num % i == 0)
{
// When number is divisible by number i
sum += i;
}
i += 1;
}
return sum;
}
//Find the sum of perfect node values in given linked list
def perfect_num_sum(): Unit = {
{
return;
}
//Define resultant variable
var result: Int = 0;
//Get first node of linked list
while (temp != null)
{
if (temp.data > 0 && temp.data == get_factor_sum(temp.data))
{
//When node data is perfect number
result += temp.data;
}
//Visit to next node
temp = temp.next;
}
//Display of calculated result
print("\n Perfect number sum is : " + result + "\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
obj.insert(5);
obj.insert(28);
obj.insert(7);
obj.insert(6);
obj.insert(1);
obj.insert(21);
obj.display();
// 6 + 28  = 34
obj.perfect_num_sum();
}
}``````

#### Output

`````` Linked List :   5  28  7  6  1  21
Perfect number sum is : 34``````
``````// Swift Program
// Find sum of all perfect numbers 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 constructors
init()
{
self.tail = nil;
}
//insert node at last of linke list
func insert(_ data: Int)
{
//Create a node
let node: Node? = Node(data);
{
self.tail = node;
}
else
{
self.tail!.next = node;
self.tail = node;
}
}
func display()
{
{
return;
}
print("\n Linked List : ", terminator: "");
while (temp != nil)
{
print("  ", temp!.data, terminator: "");
temp = temp!.next;
}
}
//Get the sum of factor in a given number
func get_factor_sum(_ num: Int) -> Int
{
var sum: Int = 0;
var i: Int = 1;
while (i <= (num / 2))
{
if (num % i == 0)
{
// When number is divisible by number i
sum += i;
}
i += 1;
}
return sum;
}
//Find the sum of perfect node values in given linked list
func perfect_num_sum()
{
{
print("\n Empty Linked List \n", terminator: "");
return;
}
//Define resultant variable
var result: Int = 0;
//Get first node of linked list
while (temp != nil)
{
if (temp!.data > 0 && temp!.data == self.get_factor_sum(temp!.data))
{
//When node data is perfect number
result += temp!.data;
}
//Visit to next node
temp = temp!.next;
}
//Display of calculated result
print("\n Perfect number sum is : ", result ,"\n", terminator: "");
}
}
func main()
{
obj.insert(5);
obj.insert(28);
obj.insert(7);
obj.insert(6);
obj.insert(1);
obj.insert(21);
obj.display();
// 6 + 28  = 34
obj.perfect_num_sum();
}
main();``````

#### Output

`````` Linked List :    5   28   7   6   1   21
Perfect number sum is :  34``````

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