Programming Articles - Page 1453 of 3363

C++ Program to Delete the First Node in a given Singly Linked List

Dev Prakash Sharma
Updated on 05-Feb-2021 11:54:11

10K+ Views

A linked list is a linear data structure that has multiple nodes that are connected with each other. Each node consists of two fields – Data Field and the address of the next node.Let us assume we have a singly linked list and we need to delete the first node from this linked list. For example, Input 1 − 4 → 3 → 2 → 1Output − 3 → 2 → 1 →Explanation − ‘4’ is the first node in the given singly linked list. After deleting the first node, the linked list will be 3→2→1.Input 2 − 1 → ... Read More

Write a Golang program to swap two numbers without using a third variable

Kiran P
Updated on 04-Feb-2021 10:55:07

1K+ Views

Approach to solve this problemStep 1: Define a function that accepts two numbers, type is int.Step 2: Find b = a + b;Step 3: Then a = b – a and b = b – aProgramLive Demopackage main import "fmt" func swap(a, b int){    fmt.Printf("Before swapping, numbers are %d and %d", a, b)    b = a + b    a = b - a    b = b - a    fmt.Printf("After swapping, numbers are %d and %d", a, b) } func main(){    swap(23, 45)    swap(56, 100) }OutputBefore swapping, numbers are 23 and 45 After swapping, numbers are 45 and 23 Before swapping, numbers are 56 and 100 After swapping, numbers are 100 and 56

Write a Golang program to find the factorial of a given number (Using Recursion)

Kiran P
Updated on 04-Feb-2021 10:54:40

3K+ Views

ExamplesFactorial of 5 = 5*4*3*2*1 = 120Factorial of 10 = 10*9*8*7*6*5*4*3*2*1 =Approach to solve this problemStep 1: Define a function that accepts a number (greater than 0), type is int.Step 2: If the number is 1, then return 1.Step 3: Otherwise, return num*function(num-1).ProgramLive Demopackage main import "fmt" func factorial(num int) int{    if num == 1 || num == 0{       return num    }    return num * factorial(num-1) } func main(){    fmt.Println(factorial(3))    fmt.Println(factorial(4))    fmt.Println(factorial(5)) }Output6 24 120

Write a Golang program to search an element in an array

Kiran P
Updated on 04-Feb-2021 10:54:20

441 Views

Definition: A number is that is greater than 2 and divisible only by itself and 1.Examples: Prime numbers are 2, 3, 5, 7, 11, 13, 113, 119, ..., etc.Approach to solve this problemStep 1: Find square root of the given number, sq_root = √numStep 2: If the given number is divisible by a number that belongs to [2, sq_root], then print “Non Prime Number”Step 3: If not divisible by any number, then print “Prime Number”ProgramLive Demopackage main import (    "fmt"    "math" ) func checkPrimeNumber(num int) {    if num < 2 {       fmt.Println("Number must be greater than 2.")       return    }    sq_root := int(math.Sqrt(float64(num)))    for i:=2; i

Bubble Sort in Go Lang

Dev Prakash Sharma
Updated on 05-Feb-2021 11:53:00

10K+ Views

Bubble Sort is a sorting algorithm that works by swapping the elements that are in the wrong order. In multiple passes, it checks if the adjacent elements are in the right order (increasing) or not.The Time Complexity of the Bubble Sort is O(n^2) since it takes two nested loops to check the adjacent element.For example, let’s take the following unsorted array −22 15 11 45 13Bubble Sort Algorithm first traverses the whole array and then in another loop checks if the adjacent elements are in order or not.Thus, after sorting the elements will be, 11 13 15 22 45AlgorithmIn two ... Read More

Birthday Paradox in Python

Dev Prakash Sharma
Updated on 05-Feb-2021 11:50:17

1K+ Views

The birthday paradox is a very famous problem in the section of probability.Problem Statement − There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate number of people at a birthday party on the basis of having the same birthday.In the probability, we know that the chance of getting ahead is 1/2, same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept.The chance of two people having the different birthday is $$\frac{364}{365}$$ which is $$\lgroup1-\frac{1}{365}\rgroup$$ in a Non-leap ... Read More

Birthday Paradox in C++

Dev Prakash Sharma
Updated on 05-Feb-2021 11:47:09

804 Views

The birthday paradox is a very famous problem in the section of probability. The problem statement of this problem is stated as, There are several people at a birthday party, some are having the same birthday collision. We need to find the approximate no. of people at a birthday party on the basis of having the same birthday.In the probability we know that the chance of getting ahead is 1/2 same as if we have some coins, the chance of getting 10 heads is 1/100 or 0.001.Let us understand the concept, The chance of two people having the different birthday ... Read More

Write a Golang program to check whether a given number is prime number or not

Kiran P
Updated on 04-Feb-2021 10:52:02

5K+ Views

Definition: A number is that is greater than 2 and divisible only by itself and 1.Examples: Prime numbers are 2, 3, 5, 7, 11, 13, 113, 119, ..., etc.Approach to solve this problemStep 1: Find square root of the given number, sq_root = √numStep 2: If the given number is divisible by a number that belongs to [2, sq_root], then print “Non Prime Number”Step 3: If not divisible by any number, then print “Prime Number”ProgramLive Demopackage main import (    "fmt"    "math" ) func checkPrimeNumber(num int) {    if num < 2 {       fmt.Println("Number must be greater than 2.")       return    }    sq_root := int(math.Sqrt(float64(num)))    for i:=2; i

Write a Golang program to find the element with the minimum value in an array

Kiran P
Updated on 04-Feb-2021 10:52:24

932 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Minimum number is 0;A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Minimum number is 10;Approach to solve this problemStep 1: Consider the number at the 0th index as the minimum number, min_num = A[0].Step 2: Compare min_num with every number in the given array, while iterating.Step 3: If a number is smaller than min_num, then assign that number to min_num.Step 4: At the end of iteration, return min_num;ProgramLive Demopackage main import "fmt" func findMinElement(arr []int) int {    min_num := arr[0]    for i:=0; i

Write a program in Go language to find the element with the maximum value in an array

Kiran P
Updated on 04-Feb-2021 10:52:45

667 Views

ExamplesA1 = [2, 4, 6, 7, 8, 10, 3, 6, 0, 1]; Maximum number is 10A2 = [12, 14, 16, 17, 18, 110, 13, 16, 10, 11]; Maximum number is 110Approach to solve this problemStep 1: Consider the number at the 0th index as the maximum number, max_num = A[0]Step 2: Compare max_num with every number in the given array, while iterating.Step 3: If a number is greater than max_num, then assign that number to max_num;Step 4: At the end of iteration, return max_num;ProgramLive Demopackage main import "fmt" func findMaxElement(arr []int) int {    max_num := arr[0]    for i:=0; i ... Read More

Advertisements