0% found this document useful (0 votes)
17 views3 pages

Mostly Asked Que

The document contains code snippets for reversing numbers, strings, and swapping two numbers in Java. 1) The code shows 5 ways to swap two numbers in Java - using a third variable, addition/subtraction without a third variable, multiplication/division without a third variable, bitwise XOR operator, and a single statement. 2) It demonstrates 3 ways to reverse a number - using a while loop and modulo/division, StringBuffer reverse method, and StringBuilder reverse method. 3) Methods to reverse a string include a for loop with string concatenation, using a character array, and StringBuffer reverse method.

Uploaded by

BEST DEALS SHOP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Mostly Asked Que

The document contains code snippets for reversing numbers, strings, and swapping two numbers in Java. 1) The code shows 5 ways to swap two numbers in Java - using a third variable, addition/subtraction without a third variable, multiplication/division without a third variable, bitwise XOR operator, and a single statement. 2) It demonstrates 3 ways to reverse a number - using a while loop and modulo/division, StringBuffer reverse method, and StringBuilder reverse method. 3) Methods to reverse a string include a for loop with string concatenation, using a character array, and StringBuffer reverse method.

Uploaded by

BEST DEALS SHOP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

1) Swaping 2 Numbers

package MAQ1;
import java.util.*;
public class swap2nums
{
public static void main(String args[])
{
System.out.println("Enter Number");
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int temp;
System.out.println("Before swaping numbers A=" + a +" B="+b);
// 5 Ways we can swap
/*
//Logic 1 ==> Using 3rd variable
temp = a;
a= b;
b=temp;
*/

/*
//Logic 2 ==> Using + & - Operator (without 3rd variable)
a=a+b; //30+40=70
b=a-b; //70-40=30
a=a-b; //70-30=40
*/

/*
//Logic 3 ==> Using * & / Operators (without 3rd variable)
//a & b should not be zero
a= a*b; //10*20=200 (a)
b= a/b; //200/20=10 (b)
a= a/b; //200/10=20 (a)
*/

/*
//Logic 4 ==> Using Bitwise XOR Operator(^) XOR
Table
a=a^b; // 10(1010 binary) ^ 20(10100 binary) = 30(a)(11110 binary) 0 0
1
b=a^b; // 30(11110 binary) ^ 20(10100 binary) = 10(b)(1010 binary) 0 1
0
a=a^b; // 30(11110 binary) ^ 10(1010 binary) = 20(a)(10100 binary) 1 0
0
// 1 1
1
*/

/*
//Logic 5==> Single statement
// A= 10 & B = 20
b=a+b-(a=b); // <--Execution start from right to left
// a=20;
// 10+20-(20)=30-20 = 10 (b)
*/
System.out.println("After swaping numbers A="+a +" B="+b);
}
}

2) Reverse A Number in Java

package MAQ1;

import java.util.Scanner;

public class Reverse_Number {

public static void main(String[] args)


{
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number");
int num = sc.nextInt(); //1234
sc.close();

// 3 Ways
//Logic 1 ==> algorithum

int rev = 0; //rev ==> 4=>43=>432=>4321 final no 4321


while( num!=0 )
{
rev = rev *10 + num%10; // 1)0+4=4 2)4*10+123%10=43
3)43*10+12%10=432 4)432*10+1%10=4321
num = num/10; // 1)1234/10=123 2)123/10=12 3)12/10=1
}

/*
//Logic 2 ==> Using String Buffer Class reverse method in Java
StringBuffer rev;

StringBuffer sb = new StringBuffer(String.valueOf(num)); //stringbuffer


chi valueof method use karun number la string madhe conver kartoy
rev = sb.reverse();
*/

/*
//Logic 3 ==> Using String Builder Reverse Method
StringBuilder ss= new StringBuilder(); //StringBuilder ss= new
StringBuilder(num); error nhi pn o/p blank yete
ss.append(num);// ss chya shevati ye add hot rahil
StringBuilder rev= ss.reverse();
*/

System.out.println("Reverse No is ==> " + rev);


}
}

3) Reverse A String
package MAQ1;
import java.util.Scanner;

public class Reverse_String


{
public static void main(String[] args)
{
System.out.println("Enter A String");
Scanner sc = new Scanner(System.in);
String str = sc.next();
String rev = "";

sc.close();
/*
// Logic 1 => Using + (String Concatination) operator
int len = str.length(); //4
for(int i = len-1;i>=0;i--) // 3 2 1 0
{
rev=rev+str.charAt(i); //D==>DC==>DCB==>DCBA
}
*/

/*
//Logic 2 ==> Using Character Array
char[] a= str.toCharArray();
int len = a.length;

for(int i=len-1;i>=0;i--)
{
rev = rev+a[i];
}
*/

/*
//Logic 3 ==> Using StringBuffer class
StringBuffer sb=new StringBuffer();
System.out.println(sb.reverse());
*/

System.out.println("Reverse String " + rev);

4)

You might also like