5.16 Armstrong Number in Java

  Рет қаралды 315,295

Telusko

Telusko

Күн бұрын

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

Пікірлер: 110
@deepapn5049
@deepapn5049 6 жыл бұрын
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
@Free_Bird78
@Free_Bird78 4 жыл бұрын
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.
@ASHOKkumar-pj3oj
@ASHOKkumar-pj3oj Жыл бұрын
Bro give r*r*r*r instead of r*r*r simple
@naveentandon993
@naveentandon993 4 жыл бұрын
Thnku so much Sir for clearing the concept..... may God bless u always ❤❤🙏🙏
@Anilkumar-ec8sj
@Anilkumar-ec8sj 8 жыл бұрын
/* 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"); } } }
@maksimvolkov8892
@maksimvolkov8892 5 жыл бұрын
Nice, thank you for this video
@SA-ie6lr
@SA-ie6lr 4 жыл бұрын
What if we need to check a 4 digit number is armstrong or not?
@vinaygoraigorai8553
@vinaygoraigorai8553 3 жыл бұрын
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"); } }
@mallikarjunareddy6122
@mallikarjunareddy6122 2 жыл бұрын
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"); } } }
@ashutoshrai2717
@ashutoshrai2717 Жыл бұрын
Yes this is right program for only 3 digit number not for 4 digits
@mr.adityasingh9012
@mr.adityasingh9012 8 ай бұрын
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
@NaveenKumar-be8xl
@NaveenKumar-be8xl 2 жыл бұрын
Simply super sir🙏
@sivapradeep977
@sivapradeep977 4 жыл бұрын
int n=153; str N1=n; int length =len(N1); int ANS,FIN_ANS=0; for(i=0;i
@abdulrahim7763
@abdulrahim7763 8 ай бұрын
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
@tanveerjafri3052
@tanveerjafri3052 4 жыл бұрын
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");
@drake71000
@drake71000 7 жыл бұрын
Armstrong number can be of more than 3 digits.
@Aulin001
@Aulin001 4 жыл бұрын
Yes
@rejaulmallik9397
@rejaulmallik9397 4 жыл бұрын
Yaah of course lol🤣🤣🤣😃😂😂you don't know this
@mohammadadil456
@mohammadadil456 5 ай бұрын
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.
@digjeetkaur9519
@digjeetkaur9519 8 жыл бұрын
your explaination is superb , can u please also explain loops more how they work again and again
@simplecoding5435
@simplecoding5435 Жыл бұрын
this method is wrong armstrong number is not like that suppose if we have 4 digit no then what to do
@Naidu-wv1dy
@Naidu-wv1dy 9 ай бұрын
explaination is good but this code will works three digit number only
@akashgokul6818
@akashgokul6818 Ай бұрын
This prg is not applicable for 4digits values
@pasupuletikoteswararao9184
@pasupuletikoteswararao9184 3 жыл бұрын
It is for only 3 digits What if we have n number
@aviroxi
@aviroxi 3 жыл бұрын
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); } }
@skasif4476
@skasif4476 3 жыл бұрын
This program is valid for 3 digit numbers only...can you generalize for all the numbers we wish to check??
@aviroxi
@aviroxi 3 жыл бұрын
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); } }
@murliengineering6130
@murliengineering6130 Жыл бұрын
Is we take a 9474 it's not working because that power is 9^4+4^4+7^4+4^4=9474
@dhanalaskhmidhanu3468
@dhanalaskhmidhanu3468 3 жыл бұрын
I really not understand what you did ?????
@dharunsivakumar7584
@dharunsivakumar7584 3 жыл бұрын
Wow.. I thought I am the only one watching this in 2020.
@dhanalaskhmidhanu3468
@dhanalaskhmidhanu3468 3 жыл бұрын
@@dharunsivakumar7584 nice
@ozlemcetin5868
@ozlemcetin5868 5 ай бұрын
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.
@sekharraj525
@sekharraj525 Жыл бұрын
If you given numbers 4digit
@ujjwalgoyal7215
@ujjwalgoyal7215 3 жыл бұрын
Hello sir , This program is not working for 4 digit numbers.
@aviroxi
@aviroxi 3 жыл бұрын
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); } }
@aadilhussain4556
@aadilhussain4556 2 жыл бұрын
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
@shubhammhatre5089
@shubhammhatre5089 Жыл бұрын
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
@elonmusk.0
@elonmusk.0 2 жыл бұрын
/* 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"); } } }
@NirajPRoxx
@NirajPRoxx 6 жыл бұрын
Thank you :)
@balapramothc.m.
@balapramothc.m. 3 жыл бұрын
why only cube
@mangeshshelke1846
@mangeshshelke1846 2 жыл бұрын
If user enter number 4 digits then?.. It's just hard coded value
@shuvshaw9594
@shuvshaw9594 4 жыл бұрын
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");
@murliengineering6130
@murliengineering6130 Жыл бұрын
Sir this program is not work for 4 digit
@fatemehrahmani6740
@fatemehrahmani6740 4 жыл бұрын
Thank u very much for your tutorial.
@अभयसिंहयादव
@अभयसिंहयादव 3 жыл бұрын
Its is applicablr only for 3 digit number It is not useful How to calculate 4 /2/5 digit number are armstrong
@aviroxi
@aviroxi 3 жыл бұрын
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); } }
@anushreebachhav9299
@anushreebachhav9299 3 жыл бұрын
why didnt we simply do, (n==sum) ?
@aviroxi
@aviroxi 3 жыл бұрын
to check if they are same, if yes then it is an armstrong number
@RohitRaj-bt7qx
@RohitRaj-bt7qx 3 жыл бұрын
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.
@atulgupta3407
@atulgupta3407 3 ай бұрын
why you have so many browsers , i think you do sussy things
@yasseralkindi7350
@yasseralkindi7350 2 жыл бұрын
brilliant, if you can add some explanation of the math logic. code wise, obviously its perfect!
@DAME-RAJATHS
@DAME-RAJATHS Жыл бұрын
I wish you had done it n^n not n^3
@sekharraj525
@sekharraj525 Жыл бұрын
How to work on scanner class
@perpetual23
@perpetual23 7 жыл бұрын
it's nt working fr the input 1 to 9.. shows nt armstrng..
@SmartProgramming
@SmartProgramming 6 жыл бұрын
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
@Dinesh_kuku
@Dinesh_kuku 2 жыл бұрын
Superbly explained!
@sumeetsajjan
@sumeetsajjan Жыл бұрын
Thank u sir now easy to understand
@shampachakraborty2515
@shampachakraborty2515 6 жыл бұрын
Thanks
@mr_mouli_10
@mr_mouli_10 7 ай бұрын
chala thanks andi meku
@karnataka8863
@karnataka8863 2 жыл бұрын
Used r x r x r for 3digit no if user use any no then how we come to know the power ?
@ts9dream
@ts9dream Жыл бұрын
We use another loop to get the size of number and then we use math.pow(digit,size)
@InInferno
@InInferno Жыл бұрын
@@ts9dream thank you
@PraveenKumar-uj2ih
@PraveenKumar-uj2ih 2 жыл бұрын
Clear Explanation ......Thanks Bro
@sanketkeluskar5583
@sanketkeluskar5583 2 жыл бұрын
really good :)
@dragonbooster5618
@dragonbooster5618 5 жыл бұрын
Nice explanation sir.Thank you so much
@mayankmittal9269
@mayankmittal9269 3 жыл бұрын
Thank you
@karan_yt_family
@karan_yt_family 3 жыл бұрын
Ossam
@jaishukla5054
@jaishukla5054 6 жыл бұрын
8208 is Also an Armstrong number but it's doesn't work
@aviroxi
@aviroxi 3 жыл бұрын
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); } }
@argonxgaming2045
@argonxgaming2045 3 жыл бұрын
Thankyou sir:)
@algorithmgaming7416
@algorithmgaming7416 5 жыл бұрын
Why did we use while loop
@shivaprakashreddyyeredla1705
@shivaprakashreddyyeredla1705 3 жыл бұрын
I didn't understand this vedio 😭😭
@thegrimare2917
@thegrimare2917 8 жыл бұрын
sorry dude it does not work
@SmartProgramming
@SmartProgramming 6 жыл бұрын
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
@shuvshaw9594
@shuvshaw9594 4 жыл бұрын
very helpful
@abdulravoof6444
@abdulravoof6444 6 жыл бұрын
thanks bro
@sudhakar3991
@sudhakar3991 Жыл бұрын
This logic is not working......dont refer this
@anusmitasen2009
@anusmitasen2009 7 ай бұрын
It's completely correct, check your program
@Shiko99FF
@Shiko99FF Ай бұрын
​​@@anusmitasen2009No he is right what if we have 4 digit number then this logic will not going to work on that 4 digit number
@koushikaisrat3670
@koushikaisrat3670 8 жыл бұрын
what about if the number n has four or five digits ?
@karnveerayush
@karnveerayush 8 жыл бұрын
+koushika israt It will make no difference.
@AbhiShek-nv3vq
@AbhiShek-nv3vq 7 жыл бұрын
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.
@drake71000
@drake71000 7 жыл бұрын
Or even more than that. If we are not sure how many digits are there in a number than what to do?
@NirajPRoxx
@NirajPRoxx 6 жыл бұрын
we have to multiply each digit only three times in order to get cube.
@SmartProgramming
@SmartProgramming 6 жыл бұрын
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
@venkateswarluguntaka7370
@venkateswarluguntaka7370 4 жыл бұрын
@sumitchaturvedi5933
@sumitchaturvedi5933 6 жыл бұрын
thank u sir
@saikumarn3547
@saikumarn3547 6 жыл бұрын
//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"); } } }
@ravinsr69
@ravinsr69 5 жыл бұрын
It does not works for 4 digits or more
@PramodKumar1207
@PramodKumar1207 7 жыл бұрын
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"); } } }
@Fbielijah
@Fbielijah 6 жыл бұрын
Video is not good
@pranavgupta9132
@pranavgupta9132 7 жыл бұрын
CAN YOU GIVE PROGRAM TO PRINT MAGIC NUMBERS FROM 1 TO 1000 ?
@AbhiShek-nv3vq
@AbhiShek-nv3vq 7 жыл бұрын
What Magic numbers??
@tharunchinna3375
@tharunchinna3375 28 күн бұрын
for any digits use this code class Main{ public static void main(String[] args) { int d = 54748; int k =d; int t =0; String g = Integer.toString(d); int m =g.length(); while(d>0){ int f = d%10; t+=Math.pow(f,m); d=d/10;} if(t==k){ System.out.println("it an armstrong number"); }else{ System.out.println("it ! an armstrong number"); } } }
@Sumit.Kr.singh93
@Sumit.Kr.singh93 Жыл бұрын
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.
Java Program #11 - Find Armstrong Number in Java
11:09
Programming For Beginners
Рет қаралды 22 М.
Миллионер | 1 - серия
34:31
Million Show
Рет қаралды 2,2 МЛН
А ВЫ ЛЮБИТЕ ШКОЛУ?? #shorts
00:20
Паша Осадчий
Рет қаралды 9 МЛН
Please Help This Poor Boy 🙏
00:40
Alan Chikin Chow
Рет қаралды 23 МЛН
How to solve any Star Pattern Program
18:47
Simply Coding
Рет қаралды 1,1 МЛН
Riot just Confirmed HUGE Valorant Update!
9:44
Dittozkul
Рет қаралды 33 М.
Armstrong Number | To check if a given number is an Armstrong number or not | Java
10:21
Java Program #8 - Fibonacci Series of Numbers in Java
6:36
Programming For Beginners
Рет қаралды 24 М.
Solve Any Pattern Question With This Trick!
57:20
Kunal Kushwaha
Рет қаралды 2,4 МЛН
Миллионер | 1 - серия
34:31
Million Show
Рет қаралды 2,2 МЛН