5.16 Armstrong Number in Java
HTML-код
- Опубликовано: 16 янв 2016
- An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 153 is an Armstrong number since 1^3 + 5^3 + 3^3 = 1+ 125+ 27 = 153.
Check out our website: www.telusko.com
Follow Telusko on Twitter: / navinreddy20
Follow on Facebook:
Telusko : / teluskolearnings
Navin Reddy : / navintelusko
Follow Navin Reddy on Instagram: / navinreddy20
Subscribe to our other channel:
Navin Reddy : / @navinreddy
Telusko Hindi :
/ @teluskohindi
Subscribe to the channel and learn Programming in easy way.
Java Tutorial for Beginners: goo.gl/p10QfB
C Tutorial Playlist : goo.gl/8v92pu
Android Tutorial for Beginners Playlist : goo.gl/MzlIUJ
XML Tutorial : goo.gl/Eo79do
Design Patterns in Java : goo.gl/Kd2MWE
Socket Programming in Java : goo.gl/jlMEbg
Spring MVC Tutorial : goo.gl/9ubbG2
OpenShift Tutorial for Beginners : goo.gl/s58BQH
Spring Framework with Maven : goo.gl/MaEluO
Sql Tutorial for Beginners : goo.gl/x3PrTg
String Handling in Java : goo.gl/zUdPwa
Array in Java : goo.gl/uXTaUy
Java Servlet : goo.gl/R5nHp8
Exception Handling in Java : goo.gl/N4NbAW
Thank you very much sir tomorrow is my java exam i didnt understood the concept which my tchr taught me but you made me understtand it thank u very much god bless u
Thnku so much Sir for clearing the concept..... may God bless u always ❤❤🙏🙏
For those who noticed that this formula only works with three digit numbers...
public boolean isArmstrongNumber(int input) {
if (input == (input % 10)) return true;
int temp1 = input;
int temp2 = input;
int num,sum = 0;
int digits = 0;
while (temp2 > 0 ) {
digits++;
temp2 /= 10;
}
while (temp1 > 0) {
num = temp1 % 10;
temp1 /= 10;
sum += Math.pow(num, digits);
}
if (input == sum) return true;
else return false;
}
Armstrong number is not what all are thinking
Armstrong number mean
Take an example
12
How many digits it is having 2
So now we have to solve it like this 1²+2² but it is not equal to twelve so it's not an Armstrong number
Take another example
153
How many digits is is having 3 so we have to add the cube of each of its digit because the number of digits are 3
So the conclusion is that first check how many digits the number is having and then we have to send it to the power of each of its digit and make the sum of that number and check it whether it is equal to that number
Bro just give r*r*r*r instead r*r*r simple
@@ASHOKkumar-pj3oj I am sorry but I don't agree,we have to use a formula valid for every number
Superbly explained!
your explaination is superb , can u please also explain loops more how they work again and again
Nice explanation sir.Thank you so much
This is not a right method because it only implies when a number is of 3 digit only like 153, 370, 371 but if the number is of 4 digit like 1634(which is an armstrong number), it will show ,number is not an armstrong number' because it cubes the digit rather than multiplying it 4 times. Basically, there is another logic or program which implies in every digit number.
Bro give r*r*r*r instead of r*r*r simple
Thank u very much for your tutorial.
Nice, thank you for this video
Clear Explanation ......Thanks Bro
Simply super sir🙏
Thank you :)
Yes this is right program for only 3 digit number not for 4 digits
Where -> sum = sum + r*r*r*r this will work on four digit....if you want check 5digit then....sum = sum+r*r*r*r*r
Thank u sir now easy to understand
brilliant, if you can add some explanation of the math logic. code wise, obviously its perfect!
I wish the calculation (r*r*r) could be dynamic because the number of multiplications is determined by the digit count. For instance, when determining if 1345 is an Armstrong number, as it has four digits, it should be r*r*r*r.
Thanks
very helpful
Thankyou sir:)
thanks bro
Thank you
really good
:)
but what if the number is 4 digit then we will have to use num raise to 4, so how will we change the r*r*r*r that time @telusko
Sir it is applicable only for 3 digit numbers if we have no like 8208, then we have write the condition like this sum=sum+ rev*rev*rev*rev; but sir is it not good practies
chala thanks andi meku
Armstrong number can be of more than 3 digits.
Yes
Yaah of course lol🤣🤣🤣😃😂😂you don't know this
/* code */
public class Armstrong {
public static void main(String args[]){
int n =370 , r,sum=0;
int temp = n ;
while(n>0){
r=n%10;
n=n/10;
sum = sum+r*r*r;
}
if(temp==sum){
System.out.println("Armstrong number");
}
else
{
System.out.println("Not Armstrong Number");
}
}
}
What if we need to check a 4 digit number is armstrong or not?
import java .util .*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("input no.");
int n = sc.nextInt();
int value = n;
int r = 0, sum = 0, c = 0;
for (int i = n; i > 0; i /= 10) {
c++;
}
while (n > 0) {
r = n % 10;
sum += (Math.pow(r, c));
n = n / 10;
}
if (sum == value)
System.out.print("armstrong no.");
else
System.out.print("not");
}
}
public class Armstrong {
public static void main(String args[])
{
int n= 153;
int temp=n;
int sum=0;
while(n>0)
{
int r=n%10;
n=n/10;
sum = sum+r*r*r;//Note :-if n=a number of digits then that maney r's has to multipliy.
}
//Note:for ex:n=153,n has a 3 digits 1,5 and 3...so r*r*r.,n=1634,n has a 4 digits 1,6,3 and 4...so r*r*r.
if(temp==sum)
System.out.println(temp+ " is a armstrong");
else
{
System.out.println(temp+ " is not an armstrong");
}
}
}
This program is valid for 3 digit numbers only...can you generalize for all the numbers we wish to check??
this code is take input from the user and find whether given number is armstrong or not
import java.util.*;
//armstrong number or not?
public class armstrongnumber{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int x=sc.nextInt();
int t=x,s=x;
int n=0,sum=0,ind=0;
while(x>0){
++n;
x=x/10;
}
while(t>0){
ind=t%10;
sum=sum+power(ind,n);
t=t/10;
}
if(s==sum)
System.out.print("true");
else
System.out.print("false");
}
static int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}
}
int num;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the number for ArmStrong Number");
num = scanner.nextInt();
int temp = num;
int save = 0;
int sum = 0;
save = num%10;
sum = (num/10)%10;
num = (num/10)/10;
num = num*num*num + sum*sum*sum + save*save*save;
if (temp == num)
System.out.println("This is ArmStrong Number: " + num);
else
System.out.println("This is not a ArmStrong Number");
int n=153;
str N1=n;
int length =len(N1);
int ANS,FIN_ANS=0;
for(i=0;i
Why did we use while loop
thank u sir
Hello sir ,
This program is not working for 4 digit numbers.
this code is take input from the user and find whether given number is armstrong or not
import java.util.*;
//armstrong number or not?
public class armstrongnumber{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int x=sc.nextInt();
int t=x,s=x;
int n=0,sum=0,ind=0;
while(x>0){
++n;
x=x/10;
}
while(t>0){
ind=t%10;
sum=sum+power(ind,n);
t=t/10;
}
if(s==sum)
System.out.print("true");
else
System.out.print("false");
}
static int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}
}
it's nt working fr the input 1 to 9.. shows nt armstrng..
import java.util.Scanner;
class ArmstrongNumber
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter The Number : ");
int no=s.nextInt();
int t1=no;
int leng=0;
while(t1 != 0)
{
leng=leng+1;
t1=t1/10;
}
int t2=no;
int arm=0;
int rem;
while(t2 != 0)
{
int mul=1;
rem=t2%10;
for(int i=1;i
I am new to java I was tasked to write a program to find Armstrong number my code:
int a=125;
do {System.out.println((a%10)*(a%10)*(a%10));
a=a/10;
} while(a!=0);
Output:
125
8
1
can someone explain my mistake.
explaination is good but this code will works three digit number only
Is we take a 9474 it's not working because that power is 9^4+4^4+7^4+4^4=9474
If user enter number 4 digits then?..
It's just hard coded value
It is for only 3 digits
What if we have n number
this code is take input from the user and find whether given number is armstrong or not
import java.util.*;
//armstrong number or not?
public class armstrongnumber{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int x=sc.nextInt();
int t=x,s=x;
int n=0,sum=0,ind=0;
while(x>0){
++n;
x=x/10;
}
while(t>0){
ind=t%10;
sum=sum+power(ind,n);
t=t/10;
}
if(s==sum)
System.out.print("true");
else
System.out.print("false");
}
static int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}
}
Ossam
How to work on scanner class
Used r x r x r for 3digit no if user use any no then how we come to know the power ?
We use another loop to get the size of number and then we use math.pow(digit,size)
@@ts9dream thank you
why only cube
❤
this method is wrong armstrong number is not like that suppose if we have 4 digit no then what to do
Sir this program is not work for 4 digit
int x=153,j=x,r,sum=0;
while(x>0)
{
r=x%10;
sum=sum+(r*r*r);
x=x/10;
}
if(sum==j)
System.out.println("y");
else
System.out.println("n");
what about if the number n has four or five digits ?
+koushika israt It will make no difference.
It'll definitely make a difference. We'll have to multiply the remainder the number of times there are digits in the number. e.g. 9474 is an armstrong number which is equal to: 9*9*9*9+ 4*4*4*4+ 7*7*7*7+ 4*4*4*4= 9474.
Or even more than that. If we are not sure how many digits are there in a number than what to do?
we have to multiply each digit only three times in order to get cube.
import java.util.Scanner;
class ArmstrongNumber
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter The Number : ");
int no=s.nextInt();
int t1=no;
int leng=0;
while(t1 != 0)
{
leng=leng+1;
t1=t1/10;
}
int t2=no;
int arm=0;
int rem;
while(t2 != 0)
{
int mul=1;
rem=t2%10;
for(int i=1;i
why didnt we simply do, (n==sum) ?
to check if they are same, if yes then it is an armstrong number
If we check (n == sum) it won't work because..
After completion of the while loop,
As we wrote that n = n/10;
The value will be 0.153 (in case of number 153)
So.. We stored the given input in temp to check it at the end of the program.
As if we wrote (n == sum)
0.153 never matches to 153.
I wish you had done it n^n not n^3
If you given numbers 4digit
I really not understand what you did ?????
Wow.. I thought I am the only one watching this in 2020.
@@dharunsivakumar7584 nice
Its is applicablr only for 3 digit number
It is not useful
How to calculate 4 /2/5 digit number are armstrong
this code is take input from the user and find whether given number is armstrong or not
import java.util.*;
//armstrong number or not?
public class armstrongnumber{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int x=sc.nextInt();
int t=x,s=x;
int n=0,sum=0,ind=0;
while(x>0){
++n;
x=x/10;
}
while(t>0){
ind=t%10;
sum=sum+power(ind,n);
t=t/10;
}
if(s==sum)
System.out.print("true");
else
System.out.print("false");
}
static int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}
}
why you have so many browsers , i think you do sussy things
/* improvised status: below code works for a number with any number of digits user wishes(i.e. user input - any number of digits) */
import java.lang.Math;
import java.util.*;
public class armstrong_num {
public boolean check_armstrong(int n){
int sum = 0,i=0,r,t,dgts,m; // variable 't' and 'dgts' are used to store original value of 'n' because we need to compare changed value with original value since later in loops 'n' value is changed.
t = n;
dgts = n;
while (dgts!=0){ // This loop is to count the total digits in 'n'. e.g. n=123, so 'i' will be 3. We will use this 'i' to as the power of r. So with this we can find any armstrong number not just 3 digit number.
dgts = dgts / 10;
i++;
}
System.out.println("Total digits in numbers: "+i);
while (n>0){
r = n % 10; //seperate last digit and stored in 'r'
n = n / 10; //n will no longer have last digit (it will have digits other than last one)
m = (int)Math.pow(r, i); // (r) ^ (i) // if n = 2345 then 'i' will be 4(from upper while loop)
sum += m;
//System.out.println(sum); // this shows 'r' with power of 'i' one by one.
}
if(t == sum){
return true;
}
return false;
}
public static void main(String[] args) {
armstrong_num an = new armstrong_num();
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number to check whether it is armstrong number or not");
int n = sc.nextInt();
//int n = 9474; //e.g. 9474 , 1634 are armstrong numbers
boolean b = an.check_armstrong(n); //if function(check_armstrong) is static, we can call function directly i.e. no need for an object in main i.e. 'an'.
if(b){//true
System.out.println(n+" is a armstrong number.");
}
else{
System.out.println(n+" is not a armstrong number");
}
}
}
8208 is Also an Armstrong number but it's doesn't work
CAN YOU GIVE PROGRAM TO PRINT MAGIC NUMBERS FROM 1 TO 1000 ?
What Magic numbers??
It does not works for 4 digits or more
tried without while loop..this is working fine for me (give any number within the limit of integer data type)
Scanner sc=new Scanner(System.in);
System.out.println("Enter any number to find out if it is a Armstrong number or not");
int input=sc.nextInt();
String s="";
String count=s.valueOf(input);
int input_size = count.length();
int digit;
double Armstrong=0;
double result=0;
int actual=input;
if(input>0)
{
for(int i=1;i
sorry dude it does not work
import java.util.Scanner;
class ArmstrongNumber
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter The Number : ");
int no=s.nextInt();
int t1=no;
int leng=0;
while(t1 != 0)
{
leng=leng+1;
t1=t1/10;
}
int t2=no;
int arm=0;
int rem;
while(t2 != 0)
{
int mul=1;
rem=t2%10;
for(int i=1;i
I didn't understand this vedio 😭😭
This logic is not working......dont refer this
It's completely correct, check your program
//To check whether given number is armstrong number or not
import java.util.Scanner;
class ArmstrongNumber
{
public static void main(String[] args)
{
Scanner scanner=null;
int n=0,r=0,sum=0,temp=0;
scanner=new Scanner(System.in);
System.out.println("Enter number to check armstrong or not : ");
n=scanner.nextInt();
temp=n;
int length=(int)Math.log10(temp)+1;
while(n>0){
r=n%10;
n=n/10;
sum=sum+((int)Math.pow(r,length));
}
if(temp==sum){
System.out.println(temp+" is armstrong number");
}
else{
System.out.println(temp+" is not a armstrong number");
}
}
}
Video is not good
import java.util.Scanner;
public class arm {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("-- Armstrong number --");
System.out.println("Enter the no");
Scanner sc=new Scanner(System.in);
int n = sc.nextInt();
int original=n;
int count=0;
int arm=0;
while(n>0)
{
n =n/10;
count++;
}
System.out.println("Length of the number: "+count);
n = original;
while(n>0)
{ int rem = n%10;
n=n/10;
int temp=1;
for(int i=count;i>0;i--)
{
temp=temp*rem;
}
arm=arm+temp;
}
if(arm == original)
{
System.out.println(original+ " is an Armstrong number");
}
else
{
System.out.println(original+ " is not an Armstrong number");
}
}
}
this code is take input from the user and find whether given number is armstrong or not
import java.util.*;
//armstrong number or not?
public class armstrongnumber{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
int x=sc.nextInt();
int t=x,s=x;
int n=0,sum=0,ind=0;
while(x>0){
++n;
x=x/10;
}
while(t>0){
ind=t%10;
sum=sum+power(ind,n);
t=t/10;
}
if(s==sum)
System.out.print("true");
else
System.out.print("false");
}
static int power(int N, int P)
{
if (P == 0)
return 1;
else
return N * power(N, P - 1);
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
// int n = 371;
Scanner scn =new Scanner(System.in);
int n=scn.nextInt();
int nod = numOfDigits(n);
int ans = 0;
int backup = n;
while (n > 0) {
int digit = n % 10;
ans = ans + (int)Math.pow(digit, nod);
n = n / 10;
}
if (backup == ans) {
System.out.println("true");
} else {
System.out.println("false");
}
}
public static int numOfDigits(int n) {
int ans = 0;
while (n != 0) {
n =n/ 10;
ans++;
}
return ans;
}
}
this will work ffor your every input.