mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-04 02:59:43 +08:00
Added armstrong number program.
This commit is contained in:
BIN
Dynamic Programming/.DS_Store
vendored
Normal file
BIN
Dynamic Programming/.DS_Store
vendored
Normal file
Binary file not shown.
20
Dynamic Programming/Armstrong Number.cpp
Normal file
20
Dynamic Programming/Armstrong Number.cpp
Normal file
@@ -0,0 +1,20 @@
|
||||
//program to check whether a number is an armstrong number or not
|
||||
#include<iostream.h>
|
||||
#include<Math.h>
|
||||
int main()
|
||||
{
|
||||
int n,k,d,s=0;
|
||||
cout<<"Enter a number:";
|
||||
cin>>n;
|
||||
k=n;
|
||||
while(k!=0)
|
||||
{
|
||||
d=k%10;
|
||||
s+=(int)pow(d,3);
|
||||
k/=10;
|
||||
}
|
||||
if(s==n)
|
||||
cout<<n<<"is an armstrong number";
|
||||
else
|
||||
cout<<n<<"is not an armstrong number";
|
||||
}
|
||||
17
Dynamic Programming/Armstrong Number.txt
Normal file
17
Dynamic Programming/Armstrong Number.txt
Normal file
@@ -0,0 +1,17 @@
|
||||
Armstrong number
|
||||
|
||||
An armstrong number is a number whose sum of cube of digits is equal to the original number.
|
||||
|
||||
Example: 153
|
||||
|
||||
Here,
|
||||
Sum of cube of digits= cube(1)+cube(5)+cube(3)
|
||||
= 1+125+27
|
||||
= 153
|
||||
This is equal to the entered number. Hence, 153 is an armstrong number.
|
||||
|
||||
Code:
|
||||
|
||||
> In our code, we have extracted each digit of the number, found its cube and stored the sum of all such cubes in a variable. This sum has been compared in the end with the original number.
|
||||
|
||||
> We have used a function 'pow' to find the cube of each digit. For its implementation, a header file 'Math.h' has been used.
|
||||
Reference in New Issue
Block a user