Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1452 of 3366
2K+ Views
ExamplesInput decimal_num = 13 => Output = 8+4+1 => 1101Input decimal_num = 11 => Output = 8+2+1 => 1011Approach to solve this problemStep 1: Define a function that accepts a decimal number, decimal_num, type is int.Step 2: Define an array to store the remainder while dividing decimal number by 2.Step 3: Start a for loop until the decimal number becomes 0.Step 4: Print the binary array in reverse order.ProgramLive Demopackage main import ( "fmt" ) func decimalToBinary(num int){ var binary []int for num !=0 { binary = append(binary, num%2) ... Read More
518 Views
We are given the length of a binary string str and the string itself. The task is to count the number of substrings that start with '1' and end with '1'. A binary string contains only '0's and '1's, and a substring is any continuous part of the given string. Let's look at a few example scenarios to understand the problem clearly: Scenario 1 Input: N = 5, str = "11101" Output: 6 Explanation: In the given binary string, there are 6 substrings that start and end with '1'. These are: ("11", "111", "1110", "11101", "1101", and "101"). Scenario ... Read More
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.ProgramLive Demopackage main import ( "fmt" "math" ) func binaryToDecimal(num int) int { var remainder int index := 0 decimalNum := 0 for num ... Read More
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.ProgramLive Demopackage main import "fmt" func findArraySum(arr []int) int{ res := 0 for i:=0; i
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
344 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
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
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
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
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