Frequently Asked Java Program 02: Reverse A Number | 3 Ways of Reverse a Number
ฝัง
- เผยแพร่เมื่อ 14 ต.ค. 2024
- Topic: Reverse a Number ( 3 Different ways)
#########################
Udemy Courses:
#########################
Manual Testing+Agile with Jira Tool
************************************
► shorturl.at/xNXZ4
Selenium with Java+Cucumber
********************************
► shorturl.at/aIKMW
Selenium with Python & PyTest
********************************
► shorturl.at/ELW09
Selenium with python using Robot framework
****************************************
► shorturl.at/bcDPZ
API Testing(Postman, RestAssured & SoapUI)
*****************************************
► shorturl.at/uAWY7
Web & API Automation using Cypress with Javascript
********************************************
► rb.gy/4uum2n
Playwright with Javascript
**************************
► shorturl.at/gALZ1
Jmeter-Performance Testing
************************
► rb.gy/ocj7r2
SDET Essencials(Full Stack QA)
*************************
► rb.gy/hlnf2v
Appium-Mobile Automation Testing
************************************
► rb.gy/wmzv64
Java Collections
*****************
► bit.ly/3S66ezE
Python Programming
*********************
► urlis.net/gdsf...
Cucumber BDD Framework
***************************
► bit.ly/3F7d69r
Protractor with Javascript
***************************
► urlis.net/4udd...
####################################
TH-cam Playlists:
####################################
Manual Testing & Agile
***********************
► bit.ly/3ujvaYa
► bit.ly/3OYzxQj
SQL
*************************
► bit.ly/3R6XjeG
► bit.ly/3nE0a1s
► bit.ly/3IeKuuJ
linux & Shell Scripting
**********************
► bit.ly/3up1Z6a
► bit.ly/3nzvGxx
Java
**********************
► bit.ly/3R9QfOs
► bit.ly/3AoRMKM
► bit.ly/3IbTTDn
► bit.ly/3ybXInv
► bit.ly/3yCoHdw
Selenium With Java+Cucumber
********************************
► bit.ly/3P2FMm4
► bit.ly/3yhiIt4
► bit.ly/3uokzet
► bit.ly/3a7XIND
► bit.ly/3umN2S9
► bit.ly/3aar7GW
► bit.ly/3y9kD2S
► bit.ly/3uhLskn
Python
********************************
► bit.ly/3agNgU0
► bit.ly/3NJNnoy
► bit.ly/2CTQ4mR
► bit.ly/3OIYWh1
Selenium With Python,Pytest&Behave
***************************************
► bit.ly/3OHHoC9
► bit.ly/3IeNLdv
► bit.ly/2J4tPeT
► bit.ly/3ydSkAq
Selenium With Python Using Robert Framework
(Web&API Testing)
*************************************************
► bit.ly/3nUvlpr
► bit.ly/3nUvE3z
API Testing (Postman,SoapUi,&Rest Assured)
**********************************************
► bit.ly/3OXacWY
► bit.ly/3yh0UhE
► bit.ly/3nC9DWQ
► bit.ly/3yGfhNS
► bit.ly/3OJa11H
► bit.ly/3P2slCC
Mobile App Testing Appium
****************************
► bit.ly/3Al49HG
Performance Testing Jmeter
*******************************
► bit.ly/3nz4fE7
Maven,Jenkins,Git,Github,CI/CD
*******************************
► bit.ly/3P16HPj
► bit.ly/3yhlUVA
► bit.ly/3afYfwT
► bit.ly/3yGzweG
► bit.ly/3yF73Ws
► bit.ly/3NCOmH7
SQL,DB Testing&ETL,Bigdata
*******************************
► bit.ly/3NKOcNY
► bit.ly/3OVpDyI
► bit.ly/3NEvUy0
► bit.ly/3IryxlT
► bit.ly/3RkWBLh
JavaScript Based Automation Tools
********************************
► bit.ly/3nUx51X
► bit.ly/3AqI6Q0
► bit.ly/3yeurbT
► bit.ly/3ujzpTK
Selector Hub Tools
********************
►rb.gy/h3h424
GraphQL
******************
► rb.gy/sj3gys
Cypress API Testing
********************
► shorturl.at/fjKPX
Cypress Web Testing
**********************
► shorturl.at/agjlK
Playwright with Javascipt
**************************
► shorturl.at/adSTY
#JavaNumberReversal
#ReverseNumber
#JavaProgramming
#CodeReversal
#JavaTips
#NumberManipulation
#ProgrammingHelp
#JavaBeginner
#CodingChallenge
#Java101
#CodingCommunity
#LearnJava
#JavaTricks
#CodeSnippets
#ReverseInteger
#JavaDevelopment
#ProgrammingQuestions
#Algorithm
#JavaAlgorithms
#CodingProblems
#CodingSolutions
#TechInterview
#CodingSkills
#JavaHelp
#ReverseDigits
#JavaLearning
#ProgrammingLogic
#JavaProblems
#JavaCode
#CodeExamples
#ReverseAlgorithm
#JavaCommunity
#CodingTutorials
#ReverseNumberLogic
#JavaChallenge
#SoftwareDevelopment
#ProblemSolving
#ProgrammingLanguages
#JavaCodeSnippet
#ReverseNumberFunction
#JavaPuzzle
#ProgrammingJourney
#JavaCoders
#JavaPractice
#CodeReview
#AlgorithmDesign
#JavaMentorship
#JavaDev
#ProgrammingProblems
#JavaCodingTips
2 Years later and this video explained perfectly to assist me with my AP CS Class. Thanks man.
Welcome
Everything is awesome...
just a small correction:
% : This is called remainder operator, which gives the remainder as output.
as in 123%10 gives 3 as remainder.
and
/ : this is division operator and gives the value after division.
as in 123/10 gives 12.3 as answer but if you have taken it as int then it will give 12.
It divides left-hand operand by right-hand operand and returns remainder and is called as (Modulus) not remainder operator
Thanks, I was confused and watched the video 2 times and then read this comment. Now got cleared...
The creator is using a strategy to increase watch time.
@@madhavkiagya No, the creator is doing a great job. You need to brush up you basics.
@@ibbishariff yes, thanks...
I liked here StringBuilder option the most. For 12300 it returns us 00321 but Option1 - Modulus one returns just 321 which is not good. Also, for numbers more than 9-10 digits Option 1 doesn't work but StringBuffer and StringBuilder comes over well . Thanks for the video !
th-cam.com/users/shortsH2z9YYe0Yvw?feature=share
Thanks sir.. logic explanation is really simple .. I’m referring your videos for interviews and it really helping me ..
I always wanted these logics.Guess i found the right channel .Thanks a ton sir!
You are best teacher in the world ,yours teaching skills is awesome sir
Thanks
thank you sir for this explanation it is really helpful ,i have doubt if we dont use scanner class is there any mistakes
Excellent explanation. Even a nontechnical guy like me can comprehend easily. Thanks a lot, sir
You are most welcome
Hello , I think 2nd and 3rd methods are same. I think 3rd one can be like converting number to string and then using charAt method to extract each char and then appending to the sting builder
int origNum = 123456;
String stNum = String.valueOf(k);
StringBuilder revStg = new StringBuilder();
for (int i = stNum.length()-1; i>=0;i--)
{
revStg.append(stNum.charAt(i));
}
System.out.println(revStg);
}
}
Splendid explaination sir 💯
Thanks
But sir , number enter to 1230 this no not reverse 0321 .. Output is 321 so 0 is eliminate so plz suggest logic for last digit 0
Use stringBuffer or stringBuilder methods
Int num = 12340;
int rev = 0 ;
While(num!=0) {
rev = (rev*10) + num%10;
num/=10;
}
System.out.println(rev);
Try this 👍
@@shankardeepak-pr9blStil removes the zero (:
@@shankardeepak-pr9bl not worked
Use while(num>=0)
Thank you for explaining in very easy manner
Glad it was helpful!
Great thanks you sir ☺. You are simply awesome.. coding med interest leni naaku, meerichhe explanation tho interest vachhestundi.. I am practcing well. Once again Thank you very much sir 🙏
Welcome
Who are here in 2024!
🙋
Yes
You explained programming very well, easy to understand.. Thank you sir
You are most welcome
i think we can also reverse integer or string by using FOR loops ...
My exams are nearing and I found this video. This is so useful. Thank you sir
You are most welcome
Thank you, I was looking for such series in hindi.
Thank you again
Welcome
Sir If we give input as 67890 then 0 is being ignored and output displaying 9876..What logic can we add in order to not ignore zero as well?
Respected sir please help
I came in this video from your Udemy course selenium with java+ cucumber...
I watched your video until 6(conditional statement & looping statement) and i came here for assignment...i watched your video but here I couldn't understand idk sir plz help me...and try to re upload your video...and why you didn't solve assignment in udamy😥😢
package reversestring;
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the sentence::");
String stnce=sc.nextLine();
System.out.println(new StringBuilder(stnce).reverse());
}
}
Hi Bro, There is one more way
public static String reverseNum(int num) {
String reverseString = "";
String str = Integer.toString(num);
char[] chr = str.toCharArray();
for (int i = 1; i
will it work -123?
Sir, first method won't work if we try to reverse *'00111'* right as we will get *37* as output.
static int reverseNumber(int num) {
int rev = 0;
while (num != 0) {
rev = rev * 10 + num % 10;
num /= 10;
}
return rev;
}
System.out.println(reverseNumber(00111));
Output = 37
This one was super cool
Thanks
I think the input taken sc.nextInt can result in exception if user enters non number or mixed input... we should handle it in a better way
WOWWWW.... FINALLY!!!!! Well explained. Great teaching. THANK YOU
welcome
Very Nice Explaination...Thank You Sir...!!!
You are most welcome
Explanation is excellent but,the xor u said that is not correct it would be bidirectional then if the values are same its 1 otherwise its 0
Such a good explanation , Sir 😊
Thanks
You explain it the best way thanks a lot sir...
Welcome
Thank you so much ❤did this for my assignment
Welcome
your explain is very clear ,it ease to understand
Thanks
Super sir grear teaching
Thanks
Sir can we revers float and double values by using string buffer and string builder
System.out.println("Logic_3");
System.out.println("Enter Number");
Scanner sc=new Scanner(System.in);
int num=sc.nextInt();
StringBuilder rev;
//Using String Builder Class
StringBuilder sbl=new StringBuilder(String.valueOf(num));
rev=sbl.reverse();
System.out.println("Reverse number is" + rev); This logic also workng for third logic
Thank u sir
Welcome
Thank you so much sir 🎉
Great explanation ❤
Most welcome
can we do that using a string concat
Amazing 🎉
thanks
Perfect Explanation ❤️🙌🏻
Glad it was helpful!
When there is easy methods string buffer and builder they why to go for algorithm ?
int reversed = 0;
while (num != 0) {
int digit = num % 10; // Extract the last digit
reversed = reversed * 10 + digit; // Append digit to reversed number
num /= 10; // Remove the last digit from original number
}
Why need to append when using string builder? it also has inbuilt reverse method...so why not use it directly....only that logic i didn't get...somebody please...
Hi Sir , Can you please explain how 1234/10 will be 123
Is it beacuse we are eliminating the decimal value because we have passed the variable has int =1234
Try this
public class Reverse {
public static void main(String[] args) {
int num = 12635;
String rev = "";
int rem = 0;
while(num>=1){
rem = num%10;
rev = rev + rem;
num = num/10;
}
System.out.println(rev);
}
}
Sir , If i have enetered 121820, with 1st method its showing as 28121, ideally it should be 028121, its stripping the zero here
please suggest!
Sir which editor are you using
thank you ,please upload more java programs...
See more here:th-cam.com/video/BfAJkKDHTQk/w-d-xo.html
Very neat and clean explanation sir.....keep up👏
th-cam.com/users/shortsH2z9YYe0Yvw?feature=share
Thanks
without using string buffer and builder
import java.util.Scanner;
public class Main
{
public String reverse(String sc){
String rev= "";
for(int i=sc.length()-1; i>=0;i--){
rev = rev +sc.charAt(i);
System.out.println("rev string "+rev);
}
return rev;
}
public static void main(String[] args) {
Scanner p = new Scanner(System.in);
System.out.println("enter a number: ");
String sc = p.next();
Main obj = new Main();
obj.reverse(sc);
}
}
Excellent man
Thanks
Thank you very much sir make more videos related to strings and arrays also
Excellent sir🤗
Thanks
tq sir please make more on aptitude we are waiting for ur classess anna
What about reversing a number like 67890 with the algorithm mentioned here? It will not consider 0 and will only print 9876.
we can u the recursion method, try this :)
import java.util.Scanner;
public class ReverseNumberExample3
{
//method for reverse a number
public static void reverseNumber(int number)
{
if (number < 10)
{
//prints the same number if the number is less than 10
System.out.println(number);
return;
}
else
{
System.out.print(number % 10);
reverseNumber(number/10);
}
}
public static void main(String args[])
{
System.out.print("Enter the number that you want to reverse: ");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
System.out.print("The reverse of the given number is: ");
//method calling
reverseNumber(num);
}
}
Sir direct aisa likh sakhte hai kya??...
Example 1234 ko reverse Karo bole
Aur apan ne aisa direct likha toh??
public class Main {
public static void main(String[] args) {
int rev=4321;
System.out.println("Reverse number is " + rev);
}
}
Aisa karke output 4321 aah raha
oh wow you are superb the way of explanation great sir
am preparing for gsoc wish it help me
th-cam.com/users/shortsH2z9YYe0Yvw?feature=share
Thank you so much very clear explanation
You’re welcome
How to identify that logic sir rev=(rev*10)+num/10
Thank you so much sir for providing very useful videos for free.
Welcome
Why rev×10?
How come when I use the same endpoints you're using my program isn't as reactive as yours. In other words as I hover over "rev" I should see 4 and then 43 etc.
please collection videos need for interview
Thank you sir 🙏
Sir what is the use of append method sir??
Owasome
Thanks
perfect job
sir can you take mock interview??? we will pay for that. plz reply
I take free mock interviews. You can send your profile to my email ID. I will schedule an interview.
@@sdetpavan thank you so much
Thanks you please upload for beginners
Welcome
Thank you so much.
Welcome
Thanks so much for this
You are so welcome!
Thanks for your all videos. Can you make some video for DataStructure and Algorithm
That logic got failed if you give a number at last "0" like 890
output: 98 only
nobody is going to reply to this because no one knows
😆😆
@@rijo_tthomas3785 🤣
no bro its working fine.
Yeah I tried With 120 it is giving reverse O/P as
21
Sir can u please explain about append()
for the first approach, it won't work with 230 or no module number. for 230 it will show 32 how can we achieve 032 that with the first method
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
String num=sc.next();
Integer num2 = Integer.parseInt(num);
int count = 0;
int count2 = 0;
char[] s = num.toCharArray();
for (char s1 : s) {
if(!String.valueOf(s1).equals("0")){
break;
}else {
count++;
}
}
for(int k=s.length; k > 0; k--) {
if(!String.valueOf(s[k-1]).equals("0")) {
break;
}else {
count2++;
}
}
//using algorithm
int rev=0;
while(num2!=0)
{
rev=rev*10 + num2%10;
num2=num2/10;
}
int i = 0;
String reverse = String.valueOf(rev);
if(rev == 0) {
reverse = "";
}
while(i
Best explanation ever.
Thanks
Super
thanks
how to enable explanation steps in eclipse sir
love u sir
% is not divided by operator.
Sir your StringBuffer approach is wrong as the the output will be a string type not an int type to convert this to int type you need to use below code
String s = rev.toString();
int i = Integer.parseInt(s);
System.out.println(i);
Thank you
i love you bro, May The Lord of all bless you
how do you add space in the middle
Screen isn't visible clear
what if the last digit is 0, eg: 120 (I'm getting a wrong answer when the user enter an integer ends with 0)
found the solution, thanks anyways
Sir is the below approach valid
Int num = 1234;
While(num!=0)
{
Int n = num%10;
num = num/10;
System.out.print(n);
}
Right
@@sdetpavan
Thanks for the reply sir
thank you very much
Welcome
Well explained
Thanks
sir mobileNumber how to Write reverse
but 10 20 50 can't be reverse.. because the result is 05 02 01
how the solution that problem sir.. thanks
i have written like this, is this correct way to write the code.
int i= 1234;
while(i!=0) {
System.out.print(i%10);
i=i/10;
}
first approach is totally wrong and shouldnt be in practice , try reversing 100 using this approach , it will give only 1.which is not reverse.instead take a string input and use reverse string approach to reverse a number.Just for the time.
01234 please tell us how to write program
Wow
Thanks
some noise sound is coming sir its annoying disturbing ur teaching and me also
Nageswara rao sir student😂
Hello
Hi, thanks for your videos
Could you please check this
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
System.out.println("Please enter the number");
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
System.out.println("User entered "+ input);
//Method 1
StringBuffer sb = new StringBuffer(input);
System.out.println("Reverse number is " +sb.reverse());
//Method2
StringBuilder sbi = new StringBuilder(input);
System.out.println("Reverse number is " +sbi.reverse());
}
}
Thank you!
You're welcome!