C program to check armstrong number using recursive function or recursion

Armstrong number is an integer which has sum of cubes of its individual digits equal to the number itself.

Example: 0,1,153,370,..

logic

  • Seperate each digit of input integer and then calculate its cube.
  • Add the cube of all digits
  • check final sum against input integer

Program

program to check armstrong number using recursive function or recursion

#include<stdio.h>
#include<conio.h>
int armstrong(int num);
void main()
{
   int num, n;
   printf("n Enter a number: ");
   scanf("%d", &num);
   n = armstrong(num);
   printf("n %d is armstrong number",n);
   getch();
}
int armstrong(int num)
{ 
    int s = 0;
    if(num)
    {
      s = ((num%10)*(num%10)*(num%10)) + armstrong(num/10);
    }
    return s;
}

Output


input = 407
output = (4*4*4) + (0*0*0) + (7*7*7)
input  number equals sum of cube of all its digits.
So 407 is armstrong number

Leave a Reply

error: Content is protected !!
%d bloggers like this: