Skip to content

Commit

Permalink
Merge pull request #1315 from VanshGarg06/VanshGarg06-patch-21
Browse files Browse the repository at this point in the history
Algorithm for Armstrong Number from 1 to N
  • Loading branch information
pankaj-bind authored Oct 27, 2024
2 parents 4ae3a24 + d280623 commit fafc61a
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Mathematical Algorithms/Armstrong Number/Armstrong_number.c
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;
}
16 changes: 16 additions & 0 deletions Mathematical Algorithms/Armstrong Number/Readme.md
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.

0 comments on commit fafc61a

Please sign in to comment.