As salamo alaikum wa rahmatullah
3 different way to reverse a number
package examples;
import java.util.Scanner;
public class ReverseNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
System.out.println("Enter number to be reversed");
int num=sc.nextInt();
// Logic - 01
int rev=0;
while(num!=0) {
rev=rev*10 + num%10;
num=num/10;
}
System.out.println(rev);
// Logic - 02
System.out.println("Enter a number again");
num=sc.nextInt();
StringBuffer rev1;
StringBuffer sb=new StringBuffer(String.valueOf(num));
rev1=sb.reverse();
System.out.println("Reverse number"+rev1);
// Logic - 03
StringBuilder rev2;
StringBuilder sbl=new StringBuilder();
sbl.append(num);
rev2=sbl.reverse();
System.out.println("Reverse number"+rev2);
}
}
Zajak allah khair
No comments:
Post a Comment