From 2cc1d5ffdee9364646a9db9c5b4df464324f97f8 Mon Sep 17 00:00:00 2001 From: kumarsyadav2 <77455527+kumarsyadav2@users.noreply.github.com> Date: Thu, 23 Sep 2021 18:23:20 +0530 Subject: [PATCH] chore(CPlusPlus): add armstrong (#455) Co-authored-by: Rahul Rajeev Pillai <66192267+Ashborn-SM@users.noreply.github.com> --- algorithms/CPlusPlus/Maths/armstrong.cpp | 30 ++++++++++++++++++++++++ algorithms/CPlusPlus/README.md | 1 + 2 files changed, 31 insertions(+) create mode 100644 algorithms/CPlusPlus/Maths/armstrong.cpp diff --git a/algorithms/CPlusPlus/Maths/armstrong.cpp b/algorithms/CPlusPlus/Maths/armstrong.cpp new file mode 100644 index 00000000..f61ced4f --- /dev/null +++ b/algorithms/CPlusPlus/Maths/armstrong.cpp @@ -0,0 +1,30 @@ +#include +using namespace std; +int main () +{ + int a = 0, t, n, s, r, p, i, sum = 0; + cin >> n; + t = n; + while (t > 0) + { + t /= 10; + a++; + } + s = n; + while (s > 0) + { + r = s % 10; + p = r; + for (i = 1; i < a; i++) + { + p *= r; + } + sum += p; + s /= 10; + } + if (sum == n) + cout << n << " is an armstrong number."; + else + cout << n << " is not an armstrong number."; + return 0; +} diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 184faf19..bd539d77 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -118,6 +118,7 @@ 2. [Prime Number](Maths/prime-check.cpp) 3. [Prime Sieve](Maths/prime-sieve.cpp) 4. [Fibonacci Series](Maths/fibonaccci-series.cpp) +5. [Armstrong Number](Maths/armstrong.cpp) # Recursion