Computer Science, asked by mroberoi, 1 year ago

how to write program of Armstrong no in java?

Answers

Answered by nitish8089
1
check \: out \: the \: program \: and \: instruction in java
/*_____________________________________________________
Author:Nitish kumar jha
What's Armstrong number:
Armstrong number is a number that is equal to the sum
of cube of it's digits as:
Example=>
:::let's take a number 153:::
where>>>
(1*1*1)=1:
(5*5*5)=125:
(3*3*3)=27:
so now let's add it:
1+125+27=153 as you see out it's equal to original number so this is an Armstrong number
______________________________________________________
/*
=====================================================
let's inform you about the input of this code so:::
1. if you want to check(c) the no. is a Armstrong number 10000
just enter the input:::
===>c10000 or C10000;
==========((==========================(=====)))
2. if you want Armstrong number in a range then
(i) enter Range(r) in any case:
(ii) enter the ranging no. like 10000
------------input--------------
===>r10000 or R10000;
=====================================================
_______________________________________________________
:::now let's program:::*/
import java.io.*;
class Armstrong_number{
public static void main(String...args) throws IOException{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String b=(reader.readLine());
if(b.charAt(0)=='c'){
int a=Integer.parseInt(b.substring(1,b.length()));
int sum,temp;
sum=0;
temp=a;
do{
int x=temp%10;
sum+=(x*x*x);
temp/=10;
}while(temp>0);
if(sum==a)
System.out.printf("%s %d %s","entered no :",a," is an armstrong number");
else if(sum!=a)
System.out.printf("%s %d %s","entered no :",a," is not an armstrong number");
}
else if(b.charAt(0)=='r'||b.charAt(0)=='R'){
int d=Integer.parseInt(b.substring(1,b.length()));
for(int i=1;i<=d;i++){int nsum,ntemp;
nsum=0;
ntemp=i;
do{
int x=ntemp%10;
nsum+=(x*x*x);
ntemp/=10;
}while(ntemp>0);
if(nsum==i)
System.out.println(nsum);
}
}
}
}

mroberoi: thanks
nitish8089: welcome
nitish8089: :)
nitish8089: try to modify i seen one...
nitish8089: if(b.charAt(0)=='c') to if(b.charAt(0)=='c'|| b.charAt(0)=='C')
Similar questions