Found 7346 Articles for C++

Recursive sum of digits of a number is prime or no in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:40:46

522 Views

Given an integer variable number as input. The goal is to calculate the sum of digits of the input number and check if that sum is prime or not. Do this till the obtained number with sum of digits becomes a single digit number. Check if that number is prime or not. If the input number is 123 than the sum of digits will be 1+2+3=6 which is non-prime and also 6 is a single digit number.Let us see various input output scenarios for thisInput − number=12341Output − Recursive sum of digits of a number is PRIMEExplanation −1+2+3+4+1=111+1=22 is a prime number.Input − ... Read More

Recursive sum of digits of a number formed by repeated appends in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:36:06

301 Views

Given two integers ‘number’ and ‘repeat’ as input. The goal is to calculate the sum of digits of the input number repeated ‘repeat’ number of times until the sum becomes a single digit. Do this till the obtained number with sum of digits becomes a single digit number. If the input number is 123 and repeat=2 than the sum of digits of 123123 will be 1+2+3+1+2+3=12 which is not a single digit number. Now the sum of digits of 12 is 1+2=3. Output will be 3Let us see various input output scenarios for thisInput − number=32 repeat=3Output − Recursive sum of digits ... Read More

Recursively print all sentences that can be formed from list of word lists in C++

Sunidhi Bansal
Updated on 03-Nov-2021 08:14:36

391 Views

Given a list of words. The goal is to create all possible sentences that can be formed by taking words from the list using a recursive approach. You can only take one word at a time from both the lists.Let us see various input output scenarios for thisInput −sentence[row][col] = {{"I", "You"},    {"Do", "do not like"},    {"walking", "eating"}}Output   −I Do walking I Do eating I like walking I like eating You Do walking You Do eating You like walking You like eatingExplanation − Taking one word from each list in sentence[0-2] gives above sentences.Input −sentence[row][col] = {{"work", "live"}, {"easy", "happily"}}Output −work ... Read More

Rotate a matrix by 90 degree without using any extra space in C++

Sunidhi Bansal
Updated on 03-Nov-2021 08:41:32

380 Views

We are given a 2-D array that will be used to form a matrix pattern. The task is to rotate a matrix by 90 degrees in an anti-clockwise direction such that the first row becomes the first column, second row becomes second column and third becomes third column and the challenge is that we don’t have to use any extra space.Let us see various input output scenarios for this −Input −int arr[row_col_size][row_col_size] = { { 5, 1, 4},    { 9, 16, 12 },    { 2, 8, 9}}Output   −Rotation of a matrix by 90 degree without using any extra space ... Read More

Rotate a matrix by 90 degree in clockwise direction without using any extra space in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:23:33

3K+ Views

We are given a 2-D array that will be used to form a matrix pattern. The task is to rotate a matrix by 90 degrees in a clockwise direction such that the last row becomes the first column, second row becomes second column and first becomes third column and the challenge is that we don’t have to use any extra space.Let us see various input output scenarios for this −Input −int arr[row_col_size][row_col_size] = { { 5, 1, 4},    { 9, 16, 12 },    { 2, 8, 9}}Output −Rotation of a matrix by 90 degree in clockwise direction without using any ... Read More

Refactorable number in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:19:18

86 Views

We are given an integer type value, let's say, number. The task is to check whether the given number is Refactorable or not. If yes print that the number is a refactorable number else print not possible.What is a Refactorable Number?A number is refactorable when it is divisible by its total count of factors available. For example, number 9 is refactorable as it has a total number of factors i.e. 3(1, 3, 9) and 9 is divisible by 3 hence its a refactorable number.Let us see various input output scenarios for this −Input − int number = 9Output − It is a ... Read More

Rearrange characters in a string such that no two adjacent are same in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:13:17

803 Views

We are given a string, let's say, str of any given length. The task is to rearrange the given string in such a manner that there won't be the same adjacent characters arranged together in the resultant string.Let us see various input output scenarios for this −Input − string str = "itinn"Output − Rearrangement of characters in a string such that no two adjacent are same is: initn.Explanation − We are given a string type variable let’s say, str. Now we will rearrange the characters of an input string in such a manner that no two same characters occur at the same position ... Read More

Rectangle with minimum possible difference between the length and the width in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:09:52

241 Views

Given an area of rectangle as input. The goal is to find the sides of a rectangle such that the difference between the length and breadth is minimum.Area of Rectangle = length * breadth.ExamplesInput − Area = 100Output −Side of Rectangle with minimum difference:Length = 10, Breadth = 10Explanation − Sides with area = 100.2 - 50, 4 - 25, 5 - 20, 10 - 10. Sides with minimum difference are 10-10 with difference = 0. As we know tha square is a rectangle with equal length on all sides.Input − Area = 254Output − Side of Rectangle with minimum difference :Length = 127, Breadth ... Read More

Recursive program to insert a star between pair of identical characters in C++

Sunidhi Bansal
Updated on 03-Nov-2021 07:06:09

596 Views

Given a string str1 as input. The goal is to insert a ‘*’ between a pair of identical characters in the input string and return the resultant string using a recursive approach.If the input string is str1= "wellness" then output will be "wel*lnes*s"ExamplesInput − str1="happiness"Output − String after adding * : hap*pines*sExplanation − Adding * between pairs pp and ss will gives us resultant string hap*pines*sInput − str1=”swimmmmingggg pooool”Output − String after adding * : swim*m*m*ming*g*g*g po*o*o*olExplanation − Adding * between pairs mm, gg and oo will gives us resultant string swim*m*m*ming*g*g*g po*o*o*olApproach used in the below program is as followsIn this approach take string ... Read More

Recursive Approach to find nth node from the end in the linked list in C++

Sunidhi Bansal
Updated on 03-Nov-2021 06:59:53

401 Views

Given a Singly linked list and positive integer N as input. The goal is to find the Nth node from the end in the given list using recursion. If the input list has nodes a → b → c → d → e → f and N is 4 then the 4th node from the end will be c.We will first traverse till the last node in the list and while returning from recursion (backtracking) increment count. When count is equal to N then return pointer to current node as result.Let us see various input output scenarios for this −Input − ... Read More

Advertisements