Found 33676 Articles for Programming

Write a Golang program to reverse a string

Kiran P
Updated on 04-Feb-2021 11:02:57

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.ProgramLive Demopackage 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 find the sum of digits for a given number

Kiran P
Updated on 04-Feb-2021 11:02:02

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.ProgramLive Demopackage 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

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

Kiran P
Updated on 04-Feb-2021 11:01:44

4K+ Views

Definition: A palindrome is a number which is similar when read from the front and from the rear.Examplesnum = 121 => Palindromenum = 13131 => Palindromenum = 123 => Not a PalindromeApproach to solve this problemStep 1: Define a function that accepts a numbers(num); type is int.Step 2: Start making the number from the input number.Step 3: If the given number is same as the output number, then return “Palindrome”Step 4: Else, return “Not A Palindrome”ProgramLive Demopackage main import "fmt" func checkPalindrome(num int) string{    input_num := num    var remainder int    res := 0    for num>0 {   ... Read More

Write a Golang program to print the Fibonacci series

Kiran P
Updated on 04-Feb-2021 11:01:25

5K+ Views

Definition: In a Fibonacci series, the next number would be the summation of its two previous numbers, series starting from 0 and 1.ExamplesPrint a fibonacci series up to num = 10;Series: 1, 2, 3, 5, 8, next is 13 but greater than 10;Approach to solve this problemStep 1: Define a function that accepts a numbers(num) type is int, till then need to print the series.Step 2: Take two initial numbers for the series, i.e., 0 and 1.Step 3: Start a true for loop and declare a third variable to store previous two values.Step 4: Print the summation of two numbers until ... Read More

Write a program in Java to check if a string can be obtained by rotating another string by 2 places

Dev Prakash Sharma
Updated on 05-Feb-2021 11:56:59

1K+ Views

Suppose we’ve two strings ‘a’ and ‘b’, the task is to find whether we can obtain string ‘b’ by rotating string ‘a’ exactly by 2 places in an anticlockwise or clockwise direction. For example, Input-1 −a = google b = legoogOutput −TrueExplanation − String ‘google’ can be rotated in an anticlockwise direction by two places, which results in the string ‘legoog’. Thus, we return True.Input-2 −a = tuorialst b = tutorialsOutput −FalseExplanation − String ‘tuorialst’ cannot be rotated by two places in any direction to get another string ‘tutorials’. Thus, we return False.The approach used to solve this problemFor the ... Read More

Write a program in C++ to check if a string can be obtained by rotating another string by two places

Nishu Kumari
Updated on 20-Aug-2025 17:21:04

498 Views

We are given two strings, a and b and the task is to find whether we can obtain the string b by rotating string a exactly two places in either an anticlockwise (left) or clockwise (right) direction. Let's take an example scenario to understand the problem clearly — Scenario 1- Input: a = "google", b = "legoog" Output: True Explanation: Rotating "google" two places anticlockwise gives "legoog", which matches string b. Thus, we return True. Scenario 2- Input 2: a = "tuorialst", b = "tutorials" Output: False Explanation: String "tuorialst" cannot be ... Read More

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

395 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

Advertisements