Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create armstrong.cpp #399

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions armstrong.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// C program to find Armstrong number
#include <stdio.h>

// Function to calculate N raised
// to the power D
int power(int N, unsigned int D)
{
if (D == 0)
return 1;

if (D % 2 == 0)
return power(N, D / 2)
* power(N, D / 2);

return N * power(N, D / 2)
* power(N, D / 2);
}

// Function to calculate the order of
// the number
int order(int N)
{
int r = 0;

// For each digit
while (N) {
r++;
N = N / 10;
}
return r;
}

// Function to check whether the given
// number is Armstrong number or not
int isArmstrong(int N)
{
// Calling order function
int D = order(N);

int temp = N, sum = 0;

// For each digit
while (temp) {
int Ni = temp % 10;
sum += power(Ni, D);
temp = temp / 10;
}

// If satisfies Armstrong condition
if (sum == N)
return 1;
else
return 0;
}

// Driver Code
int main()
{
// Given Number N
int N = 153;

// Function Call
if (isArmstrong(N) == 1)
printf("True\n");
else
printf("False\n");
return 0;
}