Q-17: Write a C/C++ prgram to check whether the given number is Armstrong number or not.
    

Using - C


#include<stdio.h> #include<math.h> int sumOfCube(int num); void main() { int num; printf("Enter a number: "); scanf("%d",&num); if(num == sumOfCube(num)) printf("It is a Armstrong Number!"); else printf("It is not a Armstrong Number!"); } int sumOfCube(int num) { int sum=0, cubeOfdigit, digit; while(num!=0) { digit = num % 10; cubeOfdigit = pow(digit,3); sum += cubeOfdigit; num /= 10; } return sum; }

OUTPUT

Case-1
----------------------------------------
Enter a number:  153
It is a Armstrong Number!

Case-2 ---------------------------------------- Enter a number: 123 It is not a Armstrong Number!
        

Using - C++


#include<iostream> #include<math.h> using namespace std; int sumOfCube(int n); int main() { int n, sum=0; cout<<"Enter a number: "; cin>> n; if(n == sumOfCube(n)) cout<< "It is a Armstrong Number!"; else cout<< "It is not a Armstrong Number!"; return 0; } int sumOfCube(int n) { int sum=0, cubeOfdigit, digit; while(n!=0) { digit = n % 10; cubeOfdigit = pow(digit,3); sum += cubeOfdigit; n /= 10; } return sum; }

OUTPUT

Case-1
----------------------------------------
Enter a number:  407
It is a Armstrong Number!

Case-2 ---------------------------------------- Enter a number: 315 It is not a Armstrong Number!