Found 7347 Articles for C++

Reverse a number using stack in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:11:32

2K+ Views

We are given an integer number Num as input. The goal is to find the reverse of the number using stack.Stack:- A stack is a data structure in C++ which stores data in LIFO ( Last in First Out) manner. Major operations of stack are-:Declaration-: stack stck; //stck is now a stack variable.Finding Top using top(). Function stck.top() returns reference of top element in the stckRemoving Top using pop(). Function removes topmost element from the stckAdding element to top using push(). Function stck.push( value ) adds item value in stack. Value should be of type stck.Check if staxk is ... Read More

Recursive Programs to find Minimum and Maximum elements of array in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:07:30

11K+ Views

We are given an integer array Arr[] as input. The goal is to find maximum and minimum elements among the array using recursive methods.Since we are using recursion, we will traverse the whole array till we reach length=1 then return A[0] which forms the base case. Else compare current element with present minimum or maximum and update its value by recursion for later elements.Let us see various input output scenarios for this −Input − Arr= {12, 67, 99, 76, 32};Output − Maximum in the array :99Explanation − Out of all elements 99 is maximum among them.Input − Arr= {1, 0, -99, 9, 3};Output − Minimum ... Read More

Recursive Program for Binary to Decimal in C++

Sunidhi Bansal
Updated on 03-Nov-2021 05:03:49

1K+ Views

We are given a string containing a binary number. The goal is to find the equivalent decimal number using the recursive method.A binary number can be converted to decimal using following method-: Traverse from LSB to MSB and multiply each with power of 2i Where 0

Recursive program to print formula for GCD of n integers in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:29:54

149 Views

We are given an integer as input. The goal is to print the formula of GCD of n numbers using recursion.We know that the GCD of three numbers say a1, b1 and c1 will be gcd(a1, gcd(b1, c1)).Similarly for more than three numbers, gcd can be obtained by formula as gcd ( a1, gcd(b1, gcd(c1….., gcd(y1, z1)).ExamplesInput − Num = 4;Output − Formula is:GCD(int a3, GCD(int a2, GCD(int a1, int b1)))Input − Num = 6;Output − Formula is: GCD(int a5, GCD(int a4, GCD(int a3, GCD(int a2, GCD(int a1, int b1)))))Approach used in the below program is as followsIn this approach we are using the ... Read More

Recursive program to check if number is palindrome or not in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:28:12

5K+ Views

We are given an integer as input. The goal is to find whether the input number Num is a palindrome or not using recursion.To check if a number is palindrome, reverse that number and check if both numbers are the same or not. If the reversed number is equal to the original number, then it is palindrome.ExamplesInput − Num = 34212;Output − 34212 is not a Palindrome!Explanation − If we reverse 34212 then we get 21243. 34212 != 21243 hence the input number is not palindrome.Input − Num = 32123;Output − 32123 is Palindrome!Explanation − If we reverse 32123 then we get 32132. 32123!= 32123 ... Read More

Recursive program for prime number in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:26:06

6K+ Views

We are given an integer as input. The goal is to find whether the input number Num is a prime or non-prime using recursion.To check if a number is prime or not, start traversing from i=2 to i

Recursive Implementation of atoi() in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:20:05

1K+ Views

We are given a string containing a number. The goal is to find the equivalent number using the recursive atoi() method. int atoi(const char *str) converts the string argument str to an integer (type int).Example-:Input − Str[] = "58325"Output − The Equivalent decimal is :58325Explanation − The string contains the equivalent number 58325Input − Str[] = "00010"Output − The Equivalent decimal is :1Explanation − The string contains the equivalent number 10.Approach used in the below program is as followsIn this approach we are using the recursive function recurAtoi() which takes input string and its length and for each character convert it to decimal and multiply ... Read More

Recursive function to do substring search in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:18:19

1K+ Views

Given two strings Str and subStr as input. The goal is to find whether text present in subStr exists in Str as substring or not. The string X is called a substring of Y if whole X is present in Y at least once. We will use a recursive approach to do this.For ExampleInput − Str = “tutorialspoint” subStr=”Point”Output − Given string does not contain substring!Explanation − The string Point is not substring of tutorialspointInput − Str = “globalization” subStr=”global”Output − Given string contains substring!Explanation − The string global is substring of globalizationApproach used in the below program is as followsIn this approach we check ... Read More

C++ Program Recursive Insertion Sort

Sunidhi Bansal
Updated on 02-Nov-2021 08:14:39

951 Views

Insertion Sort is one of the sorting algorithms used to sort data by inserting elements like a deck of cards. All the elements are arranged from left to right then considering the first one as already sorted, insert rest to the sorted list on the left. Each element is compared with each element in the left list until it is inserted at its correct position.Insertion Sort Algorithmint arr[5]= { 5, 4, 2, 1, 3 };int i, j ;Traverse from index j=i+1 to jarr[i+1], swap those elements.Set temp=arr[i], arr[i]=arr[i+1] and arr[i+1]=temp.Now decrement length by 1 as the previous loop placed the ... Read More

Recursive function to check if a string is palindrome in C++

Sunidhi Bansal
Updated on 02-Nov-2021 08:09:17

5K+ Views

We are given a string Str as input. The goal is to find if the input string is a palindrome word or not using a recursive function. Palindrome strings are those strings that when read from front or end form the same word. The strings of length 0 are considered as palindromes. Reversing the palindromes character wise forms, the same string as the original.Examples of palindromes are:- madam, abcba, malayalam etcExamplesInput − Str = “malayalam”Output − Input string is palindrome.Explanation −Str[ 0 to 8 ] = malayalamReverse Str [ 8 to 0 ] = malayalamBoth strings are same.Input − Str = “tutorial”Output − Input string ... Read More

Advertisements