If you are searching for Armstrong number in c | c++ and Java then you are at the right article.
Table of contents
- What is an armstrong number?
- How to check a number is armstrong or not?
- How to extract a digit from a number in a program?
- Armstrong number in c
- Armstrong number in c++
- Armstrong number in java
- Print all armstrong numbers below 1000 in c
- Print all armstrong numbers below 1000 in c++
- Print all armstrong numbers below 1000 in java
What is an armstrong number?
Armstrong number is a number in which the sum of cubes of digits of that number is equals to that number itself. Example: 0, 1, 153, 370, 371, 407.
How to check a number is armstrong or not?
- First split the digits of that number.
- Cube the digits.
- Sum up the cubes of the digits.
- If the sum is equal to that number then it is an armstrong number.
Number | Digits | Sum of its cubes |
0 | 0 | 0*0*0=0 |
1 | 1 | 1*1*1=1 |
153 | 1 , 5, 3 | (1*1*1)+(5*5*5)+(3*3*3)=153 |
370 | 3,7,0 | (3*3*3)+(7*7*7)+(0*0*0)=370 |
371 | 3,7,1 | (3*3*3)+(7*7*7)+(1*1*1)=371 |
407 | 4,0,7 | (4*4*4)+(0*0*0)+(7*7*7)=407 |
How to extract a digit from a number in a program?
The method for extracting a digit is simply as follows:
- Copy the number to a temporary variable.
- Find the digit which is in unit’s place of that number i.e. the remainder after dividing by 10. For this % operator can be used.
- Then divide the temporary number by 10.
- perform the digit finding operation and dividing by 10 until the number is greater than 0.
Armstrong number in c
Note: follow-up comment lines.
#include<stdio.h>
int main(int argc, char const *argv[])
{
int number;
//read a number
printf("\nEnter a number to check Armstrong number");
scanf("%d",&number);
//find the sum of its cubes
int sum_of_digit_cube=0,temp=number;
while( temp>0 )
{
int digit=temp%10;
sum_of_digit_cube+=(digit*digit*digit);
temp=temp/10;
}
if( sum_of_digit_cube == number)
{
printf("The number %d is an Armstrong number",number);
}
else
{
printf("The number %d is not Armstrong number",number);
}
return 0;
}
Armstrong number in c++
Note: follow-up comment lines.
#include<iostream>
#include<cmath>
using namespace std;
int main(int argc, char const *argv[])
{
int number;
//read a number
cout<<"\nEnter a number to check Armstrong number";
cin>>number;
//find the sum of its cubes
int sum_of_digit_cube=0,temp=number;
while( temp>0 )
{
int digit=temp%10;
sum_of_digit_cube+=pow(digit,3);
temp=temp/10;
}
if( sum_of_digit_cube == number)
{
cout<<"The number "<<number<<" is an Armstrong number";
}
else
{
cout<<"The number "<<number<<" is not Armstrong number";
}
return 0;
}
Armstrong number in java
Note: follow-up comment lines.
import java.io.*;
import java.util.Scanner;
public class HelloWorld {
//finds sum of the cubes of the digit
static int sumCubes(int n) {
int rv = 0;
int temp = n;
int digit;
while (temp > 0)
{
digit = temp % 10;
rv += Math.pow(digit, 3);
temp = temp / 10;
}
return rv;
}
public static void main(String[] args) {
//read a number
Scanner stdin=new Scanner(System.in);
System.out.println("Enter a number to check Armstrong Number "); int
num=stdin.nextInt();
if( num==sumCubes(num) )
System.out.println("It is Armstrong Number ");
else
System.out.println("It is not Armstrong Number ");
stdin.close();
}
}
Print all armstrong numbers below 1000 in c
Note: follow-up comment lines.
#include<stdio.h>
int main(int argc, char const *argv[])
{
printf("Armstrong numbers below 1000\n");
for( int i=0 ; i<1000 ; i++ )
{
int number=i;
int sum_of_digit_cube=0;
while( number>0 )
{
int digit=number%10;
sum_of_digit_cube+=digit*digit*digit;
number=number/10;
}
if( sum_of_digit_cube==i )
{
printf( "%d\n",i );
}
}
return 0;
}
Print all armstrong numbers below 1000 in c++
Note: follow-up comment lines.
#include<iostream>
#include<cmath>
using namespace std;
int main(int argc, char const *argv[])
{
cout<<"Armstrong numbers below 1000 in c++\n";
for( int i=0 ; i<1000 ; i++ )
{
int number=i;
int sum_of_digit_cube=0;
while( number>0 )
{
int digit=number%10;
sum_of_digit_cube+=pow(digit,3);
number=number/10;
}
if( sum_of_digit_cube==i )
{
cout<<i<<endl;
}
}
return 0;
}
Print all armstrong numbers below 1000 in java
Note: follow-up comment lines.
import java.io.*;
public class HelloWorld {
static int sumCubes(int n) {
int rv = 0;
int temp = n;
int digit;
while (temp > 0) {
digit = temp % 10;
rv += Math.pow(digit, 3);
temp = temp / 10;
}
return rv;
}
public static void main(String[] args) {
System.out.println("Armstrong Numbers below 1000 in java ");
for( int i=0 ;i<10000000 ;i++ )
{
if( i==sumCubes(i) )
System.out.println(i);
}
}
}