Showing posts from November, 2015

Checking for Balanced Paranthesis using Stack in C++

Balanced equations ? Not it is not the one you see in Chemistry. Balanced equations are those equations in which the brackets in the equation are balanced. Consider the following expression :

This equation is balanced considered to be balanced as the closing ) matches the opening (. Consider another example below :
In the above equation you could see that the opening flower bracket doesn't have an matching closing flower bracket. So this is considered to be a balanced equation. 
So lets write a program for that. We will be using stack in the program and this is one of the most important application of stack. 
Algorithm : Get the expression from the userRun a loop for the entire length of the expression.If the character is (, { or [ then push it on to the stack,If the character is ), } or ] then pop a character from the stack and check if it is (, { or [ respectively.If so continue on to the next iteration of the loop else print it out as unbalanced equation.After th…

Finding Maximum Element in an Array using Recursion

Recursion is something that may be a little confusing to understand but when you master it would be very much easier and will actually consume less space compared to a normal iterative function. So now we will write a program to find the maximum element in an array using recursion.

Algorithm :

Get the numbers from the user and pass the array and size of array to the functionDeclare max as global and initialise it to a[0].Inside the function check for the condition of maximum element and if the index value reaches -1 return the maximum element.

Checking for an Armstrong Number using Recursion - C++

So Hey There ! New Here ? Even I am ! You can find a brief introduction about me in the about author section of the post.

In this post lets see on how to find if a given number is armstrong or not using recursion in C++. Armstrong numbers are those numbers in which the sum of the cubes of the individual digits of a number is equal to the number itself.
Ex : 3^3 + 7^3 + 1^3 = 371 and so 371 is an armstrong number
Let us first write the program without using recursion so as to get a better understanding of what the program does. Take a look at the code below.
Non-recursive Program : 

Recursive Program : 

So what we do here :
We create a global variable sum and initialise to 0We write the function taking two arguments. First one is the number and second is a temporary variable similar to the variable temp used in the non recursive program.We check for the condition a = 0 and if so we just check for the condition for an armstrong number and return 1 if armstrong and 0 if not.In main we just …