Programming Articles - Page 1452 of 3363

Write a program in Python to count the number of digits in a given number N

Dev Prakash Sharma
Updated on 05-Feb-2021 11:58:55

8K+ Views

Let's suppose we have given a number N. the task is to find the total number of digits present in the number. For example, Input-1 −N = 891452Output −6Explanation − Since the given number 891452 contains 6 digits, we will return ‘6’ in this case.Input-2 −N = 0074515Output −5Explanation − Since the given number 0074515 contains 5 digits, we will print the output as 5.The approach used to solve this problemWe can solve this problem in the following way, Take input ‘n’ as the number.A function countDigits(n) takes input ‘n’ and returns the count of the digit as output.Iterate over all ... Read More

Count Good Meals in Python

Dev Prakash Sharma
Updated on 05-Feb-2021 11:58:15

374 Views

A good meal contains exactly two different food items with a sum of deliciousness equal to a power of two. You can pick any two different foods to make a good meal.Let us suppose we have given an array of integers arr where arr[i] is the deliciousness of the ith item of food, return the number of different good meals you can make from this list.For example, Input-1 −arr[ ] = {1, 3, 5, 7, 9}Output −4Explanation − The good meals are (1, 3), (1, 7), (3, 5) and, (7, 9). Their respective sums are 4, 8, 8, and 16, ... Read More

Write a Golang program to reverse a number

Kiran P
Updated on 04-Feb-2021 11:03:30

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

Write a Golang program to reverse an array

Kiran P
Updated on 04-Feb-2021 11:03:15

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.ProgramLive Demopackage main import "fmt" func reverseArray(arr []int) []int{    for i, j := 0, len(arr)-1; i

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

6K+ 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

548 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

Advertisements