Server Side Programming Articles

Page 1141 of 2109

Write a Golang program to find the sum of digits for a given number

Kiran P
Kiran P
Updated on 11-Mar-2026 2K+ Views

Examplesnum = 125 => 1 + 2 + 5 = 8num = 101 => 1 + 0 + 1 = 2num = 151 => 1 + 5 + 1 = 7Approach to solve this problemStep 1: Define a function that accepts numbers(num); type is int.Step 2: Start a true loop until num becomes 0 and define res:=0.Step 3: Find modulo and add to res.Step 4: Divide num by 10.Step 5: Return res.Programpackage main import "fmt" func findDigitSum(num int) int {    res := 0    for num>0 {       res += num % 10       num /= 10    }    return res } func main(){    fmt.Println(findDigitSum(168))    fmt.Println(findDigitSum(576))    fmt.Println(findDigitSum(12345)) }Output15 18 15

Read More

Write a Golang program to reverse a string

Kiran P
Kiran P
Updated on 11-Mar-2026 5K+ Views

ExamplesInput str = “himalaya” => Reverse String would be like => “ayalamih”Input str = “mountain” => Reverse String would be like => “niatnuom”Approach to solve this problemStep 1: Define a function that accepts a string, i.e., str.Step 2: Convert str to byte string.Step 3: Start iterating the byte string.Step 4: Swap the first element with the last element of converted byte string.Step 5: Convert the byte string to string and return it.Programpackage main import "fmt" func reverseString(str string) string{    byte_str := []rune(str)    for i, j := 0, len(byte_str)-1; i < j; i, j = i+1, j-1 {   ...

Read More

Write a Golang program to reverse an array

Kiran P
Kiran P
Updated on 11-Mar-2026 3K+ Views

ExamplesInput arr = [3, 5, 7, 9, 10, 4] => [4, 10, 9, 7, 5, 3]Input arr = [1, 2, 3, 4, 5] => [5, 4, 3, 2, 1]Approach to solve this problemStep 1: Define a function that accepts an array.Step 2: Start iterating the input array.Step 3: Swap first element with last element of the given array.Step 4: Return array.Programpackage main import "fmt" func reverseArray(arr []int) []int{    for i, j := 0, len(arr)-1; i

Read More

Write a Golang program to reverse a number

Kiran P
Kiran P
Updated on 11-Mar-2026 3K+ Views

ExamplesInput num = 1432 => output = 2341Input num = 9878 => output = 8789Input num = 6785 => output = 5876Approach to solve this problemStep 1: Define a function that accepts a number (num); type is int.Step 2: Define res = 0 variable and start a loop until num becomes 0.Step 3: Find remainder = num % 10 and make a number.Step 4: Divide num by 10.Step 5: Return res.Programpackage main import "fmt" func reverseNumber(num int) int {    res := 0    for num>0 {       remainder := num % 10       res = (res * 10) + remainder       num /= 10    }    return res } func main(){    fmt.Println(reverseNumber(168))    fmt.Println(reverseNumber(576))    fmt.Println(reverseNumber(12345)) }Output861 675 54321

Read More

Write a Golang program to calculate the sum of elements in a given array

Kiran P
Kiran P
Updated on 11-Mar-2026 4K+ Views

ExamplesInput arr = [1, 3, 4, 5, 6] => 1+3+4+5+6 = 19Input arr = [5, 7, 8, 9, 1, 0, 6] => 5+7+8+9+1+0+6 = 36Approach to solve this problemStep 1: Define a function that accepts an array.Step 2: Declare a variable, res = 0.Step 3: Iterate the array and add elements to res.Step 4: Return res.Programpackage main import "fmt" func findArraySum(arr []int) int{    res := 0    for i:=0; i

Read More

Write a Golang program to convert a binary number to its decimal form

Kiran P
Kiran P
Updated on 11-Mar-2026 2K+ Views

ExamplesInput binary_num = 1010111 => decimal number = 64+0+16+4+2+1 = 87Input binary_num = 10101 => decimal number = 21Approach to solve this problemStep 1: Define a function that accepts a binary number, binary_num, declare decimalNum = 0, index = 0Step 2: Start for loop until binary_num becomes 0.Step 3: Find the remainder of binary_num and divide by 10.Step 4: Calculate the decimal number using decimalNum and remainder*pow(2, index).Step 5: Return decimalNum.Programpackage main import (    "fmt"    "math" ) func binaryToDecimal(num int) int {    var remainder int    index := 0    decimalNum := 0    for num != ...

Read More

Counting elements in two arrays using C++

Dev Prakash Sharma
Dev Prakash Sharma
Updated on 11-Mar-2026 843 Views

Let us assume that we have given two unsorted arrays arr1[] and arr2[]. The task is to count the total number of elements in arr2[] for which each element of arr1[] are less than or equal to the elements present in arr2[]. However, the element in both the array may contain duplicates too.For example, Input-1 −N = 6 M = 7 arr1[N] = {1, 2, 5, 0, 6, 3} arr2[M] = {0, 0, 1, 2, 1, 3, 4, 6, 8}Output −4 5 7 2 8 6The approach used to solve this problemTo count every element of arr1[] and check if ...

Read More

Write a Golang program to find the frequency of an element in an array

Kiran P
Kiran P
Updated on 11-Mar-2026 354 Views

ExamplesIn the Input array, arr = [2, 4, 6, 7, 8, 1, 2]Frequency of 2 in given array is 2Frequency of 7 is 1Frequency of 3 is 0.Approach to solve this problemStep 1: Define a function that accepts an array and a numStep 2: Declare a variable count = 0.Step 3: Iterate the given array and increase the count by 1 if num occurs in the array.Step 4: Print count for the given num.Programpackage main import "fmt" func findFrequency(arr []int, num int){    count := 0    for _, item := range arr{       if item == num{ ...

Read More

Write a Golang program to find prime numbers in a given range

Kiran P
Kiran P
Updated on 11-Mar-2026 3K+ Views

ExamplesInput num1=3 and num2=8 => Prime numbers are: 3, 5, 7Input num1=8 and num2=23 => Prime numbers are: 11, 13, 17, 19, 23Approach to solve this problemStep 1: Define a function that accepts two numbers, num1 and num2, type is int.Step 2: Iterate between num1 and num2.Step 3: If the number is prime, then print that number, else break.Programpackage main import (    "fmt"    "math" ) func printPrimeNumbers(num1, num2 int){    if num1

Read More

Write a Golang program to sort an array using Bubble Sort

Kiran P
Kiran P
Updated on 11-Mar-2026 699 Views

Definition: Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.ExamplesInput arr = [7, 5, 1, 6, 3]1st iteration => swap(7, 5) => swap(7, 1) => swap(7, 6) => swap(7, 3) => [5, 1, 6, 3, 7]2nd iteration => [1, 5, 3, 6, 7]3rd iteration => [1, 3, 5, 6, 7]4th iteration => [1, 3, 5, 6, 7]5th iteration => [1, 3, 5, 6, 7]Approach to solve this problemStep 1: Iterate the array from 0th index to n-1.Step 2: Iterate the array from the 0th index to n-1-i, ...

Read More
Showing 11401–11410 of 21,090 articles
Advertisements