Computer Science, asked by subhaq, 1 year ago

write a java program to check the entered number is Armstrong or not

Answers

Answered by nitish8089
2
check \: out \: the \: code....
/*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'||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);
}
}
}
}
Answered by shardul1925
2
Hello!

Your question-
Write a java program to check the entered number is Armstrong or not.

Answer-

import java.util.Scanner;

class ArmstrongNumber
{
public static void main(String args[])
{
int n, sum = 0, temp, remainder, digits = 0;

Scanner in = new Scanner(System.in);
System.out.println("Input a number to check if it is an Armstrong number");
n = in.nextInt();

temp = n;

while (temp != 0) {
digits++;
temp = temp/10;
}

temp = n;

while (temp != 0) {
remainder = temp%10;
sum = sum + power(remainder, digits);
temp = temp/10;
}

if (n == sum)
System.out.println(n + " is an Armstrong number.");
else
System.out.println(n + " is not an Armstrong number.");
}

static int power(int n, int r) {
int c, p = 1;

for (c = 1; c <= r; c++)
p = p*n;

return p;
}
}

Output--

Input a number to check if it is an Armstrong number
9926315
9926315 is an Armstrong number.

Input a number to check if it is an Armstrong number
15781
15781 is not an Armstrong number.

Hope it helps:)

nitish8089: nice
nitish8089: :)
shardul1925: thxx
shardul1925: ye.. Fibonacci series kya hota hain
subhaq: thanks
nitish8089: fibonacci series:: 1 1 2 3 5 8...
nitish8089: as you seen 3 =1+2 and 5=3+2
shardul1925: hmm..samaj gaya
Similar questions