[go: up one dir, main page]

0% found this document useful (0 votes)
19 views2 pages

Swaping of Two Numbers in Java

The document contains Java code examples demonstrating various programming concepts such as swapping two numbers, reversing a number, reversing a string, and checking for palindrome numbers. Each section includes multiple methods for achieving the tasks, including using third variables, arithmetic operations, bitwise operations, and built-in classes like StringBuffer and StringBuilder. The code snippets are commented out, indicating different approaches to solve the problems presented.

Uploaded by

eswarvineet
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)
19 views2 pages

Swaping of Two Numbers in Java

The document contains Java code examples demonstrating various programming concepts such as swapping two numbers, reversing a number, reversing a string, and checking for palindrome numbers. Each section includes multiple methods for achieving the tasks, including using third variables, arithmetic operations, bitwise operations, and built-in classes like StringBuffer and StringBuilder. The code snippets are commented out, indicating different approaches to solve the problems presented.

Uploaded by

eswarvineet
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/ 2

1) swaping of two numbers...............

-5-
public class swapingof2numbers {
public static void main(String[] args) {
int a=10 , b=20;
System.out.println("Before swaping values are.."+a+" "+b);
//logical- third value
/* int t=a;
a=b;
b=t;*/
// logic2 addition & sub opertors (without using thrid variable
/* a=a+b;
b=a-b;
a=a-b;*/
//logic3 multiplication and division for non zero numbers
/* a=a*b;
b=a/b;
a=a/b; */
// logic4 bitwise xor operator(^)
/* a=a^b;//10^20=30
b=a^b;//30^20=10
a=a^b; //30^10=20 */
// logic5 single statement
b=a+b-(a=b);

System.out.println("after swaping values are.."+a+" "+b);


}
}
2) Reverse of a Numbers.........-3-
import java.util.Scanner;
public class reversenumber {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
System.out.println("Enter any one nmber: ");
int num=sc.nextInt();//1234
//1.using algorithm
/* int rev=0;
while(num!=0)
{
rev=rev*10 + num%10;
num=num/10;
} */

//Using StingBuffer class

/* StringBuffer rev;
StringBuffer sb=new StringBuffer(String.valueOf(num));
rev=sb.reverse(); */

//Using StringBuilder class


StringBuilder sbl=new StringBuilder();
sbl.append(num);
StringBuilder rev= sbl.reverse();
System.out.println("Reverse number is:"+rev);
}
}
3) Reverse of a String.......-3-
import java.util.Scanner;
public class ReverseString{
public static void main(String agrs[]){
System.out.println("Enter any string:");
Scanner sc =new Scanner(System.in);
String str=sc.nextLine();
System.out.println("You have entered:\n"+str );
//String str = "ABCD";
String rev=" ";
//1) Using (string concatenation operator +)
/* int len=str.length();//4
for(int i=len-1;i>=0;i--) //3210 -1
{
rev=rev+str.charAt(i); //DCBA
} */
//2) Using Character array
/* char a[]=str.toCharArray();
int len=a.length; //4
for(int i=len-1;i>=0;i--)//3210 -1
{
rev=rev+a[i]; //DCBA
} */
// 3) Using String Buffer Class
/* StringBuffer sb=new StringBuffer(str);
System.out.println(sb.reverse()); */
System.out.println("Reversed string is:"+rev);
}
}

4) PALINDROME NUMBER 16461

You might also like