Answers for "how to find armstrong number"

1

armstrong number in c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int cube(int a)
{
    int c;
    c =  a*a*a;
    return c;
}

int armnum(int *a)
{
    int x = *a, n = 0, rem, r = 0;
    while (x != 0) {
        x /= 10;
        n++;
    }
    x = *a;
    while (x != 0) {
        rem = x % 10;
        r += cube(rem);
        x /= 10;
    }
    if(r == *a){
        return 1;
    }
}

int main()
{
    int a, y;
    scanf("%d", &a);
    y = armnum(&a);
    if(y == 1){
        printf("It is an Armstrong number.");
    }
    else{
        printf("It is not an Armstrong number.");
    }
}
Posted by: Guest on August-26-2020
0

armstrong number function

def is_armstrong_number(number: int)-> bool:
    arm = str(number)
    lenght = len(arm)
    sum = 0
    for digit in arm:
        sum += int(digit)**lenght
    return sum == number
Posted by: Guest on June-01-2021
0

How do you calculate Armstrong number in maths?

import java.util.Scanner;  
import java.lang.Math;  
public class one    //ArmstsrongNumberExample  
{  
//function to check if the number is Armstrong or not  
static boolean isArmstrong(int n)   
{   
int temp, digits=0, last=0, sum=0;   
//assigning n into a temp variable  
temp=n;   
//loop execute until the condition becomes false  
while(temp>0)    
{   
temp = temp/10;   
digits++;   
}   
temp = n;   
while(temp>0)   
{   
//determines the last digit from the number      
last = temp % 10;   
//calculates the power of a number up to digit times and add the resultant to the sum variable  
sum +=  (Math.pow(last, digits));   
//removes the last digit   
temp = temp/10;   
}  
//compares the sum with n  
if(n==sum)   
//returns if sum and n are equal  
return true;      
//returns false if sum and n are not equal  
else return false;   
}   
//driver code  
public static void main(String args[])     
{     
int num;   
Scanner sc= new Scanner(System.in);  
System.out.print("Enter the limit: ");  
//reads the limit from the user  
num=sc.nextInt();  
System.out.println("Armstrong Number up to "+ num + " are: ");  
for(int i=0; i<=num; i++)  
//function calling  
if(isArmstrong(i))  
//prints the armstrong numbers  
System.out.print(i+ ", ");  
}   
}
Posted by: Guest on July-10-2021

Code answers related to "how to find armstrong number"

Browse Popular Code Answers by Language