-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1315 from VanshGarg06/VanshGarg06-patch-21
Algorithm for Armstrong Number from 1 to N
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Mathematical Algorithms/Armstrong Number/Armstrong_number.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//Objective: To find the Armstrong Number from 1 to n | ||
//Input parameters: the value of n | ||
//Output parameters: the armstrong numbers from 1 to n | ||
#include<stdio.h> | ||
#include<math.h> | ||
|
||
//Function to calculate power of a number | ||
int power(int number,int exponent){ | ||
int prod=1; | ||
for(int i=0;i<exponent;i++){ | ||
prod = prod*number; | ||
} | ||
return prod; | ||
} | ||
|
||
|
||
int main(){ | ||
int n,num,sum,counter; | ||
//Taking user input for the n | ||
printf("Enter the value of n:"); | ||
scanf("%d",&n); | ||
//Printing all the armstrong numbers | ||
printf("The Armstrong numbers are:\n"); | ||
for(int i=1;i<=n;i++){ | ||
num = i; | ||
sum = 0; | ||
counter=0; | ||
//Calculating the number of digits | ||
while(num>0){ | ||
counter++; | ||
num = num/10; | ||
} | ||
num = i; | ||
//Evaluating whether the number in range is Armstrong or not | ||
if(counter>3){ | ||
while(num>0){ | ||
int digit = num%10; | ||
sum = sum+power(digit,counter); | ||
num=num/10; | ||
} | ||
} | ||
else{ | ||
while(num>0){ | ||
int digit = num%10; | ||
sum = sum+power(digit,3); | ||
num=num/10; | ||
} | ||
} | ||
//Armstrong number is number if the sum of the length number(cube i.e 3 in case of 132) of the digit | ||
//is equal to number | ||
if(sum==i){ | ||
printf("%d ",i); | ||
} | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Armstrong Number | ||
|
||
## Problem Description | ||
|
||
The Armstrong number is the number in which the sum of the digits of power length of the number. | ||
The number is equal to the number got from the summation of the cube of the number. | ||
|
||
## Example | ||
|
||
If the User entered any number like 153 then we will calculate the sum of the cube of the digits of the number | ||
like 153 -> (3)^3 + (5)^3 + (1)^3 = 153. | ||
So, 153 is an armstrong number. | ||
|
||
## Additional Context | ||
|
||
This is the problem statement which is commonly asked in many interviews for various roles. |