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 0
  • We 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 get the value from the user and pass the same value to the function.
  • We print the result.


Output :


Comments

  1. Armstrong Program in C++

    Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits is equal to the number itself.
    For example 153 is armstrong number, 132 is not prime number. Armstrong program in c++ is very simple and easy to write.

    ReplyDelete

Post a Comment

Popular posts from this blog

Finding the Longest Palindrome in a String

Finding a Single Number amongst Repeated Numbers [Repeated Twice]

Finding the Longest Path From One Point To Another - Dynamic Programming