///---Java Programs---///
1. Write a Program to swap two numbers without using temp variable?
public class SwapTwoNumber {
public static void main(String[] args) {
int a=10;
int b=20;
System.out.println("Before Swapping numbers are: A="+a +" B="+b);
/*//Method 1
a=a^b;
b=a^b;
a=a^b;*/
/*//Method2
a=a*b;
b=a/b;
a=a/b;*/
/*//Method 3
a=a+b;
b=a-b;
a=a-b;*/
//Method 4
b=a+b -(a=b);
System.out.println("After Swapping numbers are: A="+a +" B="+b);
}
}
2. Polindrom:
public class MyClass {
public static void main(String args[]) {
String strr="hserus";
String rev="";
for (int i=strr.length()-1;i>=0;i--) {
rev=rev+strr.charAt(i);
}
System.out.println("Polindrom " + rev);
}
}
3. Reverse Number/Polindrom
public class MyClass {
public static void main(String args[]) {
int num, revnum=0;
num=4321;
while(num !=0){
revnum= revnum * 10 + num % 10;
num /= 10;
}
// Using StringBuffer
//StringBuffer sb= new StringBuffer(String.valueOf(num));
// StringBuffer revnum= sb.reverse();
System.out.println("number is " + revnum);
}
}
4. Reverse a String
public class ReverseString {
public static void main(String[] args) {
String str= "hseruS";
String rev="";
int len=str.length(); //4
for(int i=len-1;i>=0;i--){
rev=rev+str.charAt(i);
}
//Using Character Array
//char a[]=str.toCharArray();
// int len=a.length;
//for(int i=len-1;i>=0;i--){
// rev=rev+a[i];
// }
System.out.println("Reverse string is " + rev);
}
}
5. Count Number of digits in a number
public class CountNumber {
public static void main(String[] args) {
int num= 43243341, count=0;
while(num>0){
num=num/10;
count++;
}
System.out.println("NUmber of digits are " + count);
}
}
6. Even and Odd Count
public class EvenorOdd {
public static void main(String[] args) {
int num= 4324, Even=0, odd=0,rem=0;
while(num>0){
rem=num%10;
if(rem%2==0){
Even++;
}
else{
odd++;
}
num=num/10;
}
System.out.println("Even Numbers are " + Even);
System.out.println("Odd Number are " + odd);
}
}
7. Sum of Numbers
public class FindSumofDigits {
public static void main(String[] args) {
int num= 4324, sum=0, odd=0,rem=0;
while(num>0){
sum=sum+ num%10;
num=num/10;
}
System.out.println("Sum of Numbers are " + sum);
}
}
8.Swap Two Strings without using third variable
class SwapTwoStrings
{
public static void main(String args[])
{
String a = "Hello";
String b = "World";
System.out.println("Strings before swap: a = " +
a + " and b = "+b);
a = a + b;
b = a.substring(0,a.length()-b.length());
a = a.substring(b.length());
System.out.println("Strings after swap: a = " +
a + " and b = " + b);
}
}
9. Reverse a string only in given string not numbers and position
public class reverseOnlyString {
public static void main(String[] args) {
String str="123abc1234abcde";
String reverse="";
String re="";
String res="";
int j;
for(j=0;j<str.length();j++) {
char dig= str.charAt(j);
if(Character.isDigit(dig)) {
reverse= reverse + dig;
}
else {
for(int i=j;i<str.length();i++) {
char rev= str.charAt(i);
if(Character.isDigit(rev)) {
for(int k=re.length()-1;k>=0;k--) {
res=res + re.charAt(k);
}
reverse=reverse+res;
j--;
re="";
res="";
break;}
else {
re= re + rev;
j++;
}
}
}
}
for(int k=re.length()-1;k>=0;k--) {
res=res + re.charAt(k);
}
reverse=reverse+res;
System.out.println("Reverse String is : "+ reverse );
}
}
9. To count repeated words in a string using HashMap
public class countRepeatedwords {
public static void main(String[] args) {
String str="Hi this is repeated words count is this repeated";
String[] words = str.split(" ");
HashMap<String, Integer> map= new HashMap<String, Integer>();
for(int i=0;i<words.length;i++) {
if(map.containsKey(words[i])) {
int count= map.get(words[i]);
map.put(words[i], count+1);}
else {
map.put(words[i], 1);
}
}
System.out.println(map);
}
}
10. Duplicate char in a string
public class duplicateCharaters {
public static void main(String are[])
{
String str="Suresh Meti";
int len=str.length();
System.out.println("Repeated words are: ");
char[] ch=str.toLowerCase().toCharArray();
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
if(ch[i]==ch[j])
{
System.out.println(ch[j]);
break;
}
}
}
}
}
11. Armstrong number (Armstrong number is the number which is the sum of the cubes of all its unit 153 = 1*1*1 + 5*5*5 + 3*3*3 = 1 + 125 + 27 = 153)
public class armstrongnumber {
public static void main(String areg[])
{
int n=153,a,c=0;
int temp=n;
while(n>0)
{
a=n%10;
c=c+(a*a*a);
n=n/10;
}
if(temp==c)
{
System.out.println("Given "+temp+" number is Armstrang number ");
}
else
{
System.out.println("Given "+temp+" number is not Armstrang number ");
}
}
}
12. Write the Java code to Sort the words by Ascending order? (Cat, God, Ant, Bell)
public class sortStringbyAsc {
public static void main(String arg[])
{
String[] str= {"Suresh","Meti","Apple","Dog","Ball"};
String temp;
for(int i=0;i<str.length;i++)
{
for(int j=i+1;j<str.length;j++)
{
if(str[i].compareTo(str[j])>0)
{
temp= str[i];
str[i]=str[j];
str[j]=temp;
}
}
}
System.out.println("The names in alphabetical order are: ");
for(int i=0;i<str.length;i++)
{
System.out.println(str[i]);
}
}
}
13.Removing duplicate values in arrays
public class duplicatevaluesremoved {
public static void main(String args[]) {
int a[] = { 1, 3, 3, 4, 2, 1, 4, 6, 7, 7, 10, 10 };
Arrays.sort(a);
int j=0;
for(int i=0;i<a.length-1;i++) {
if(a[i]!=a[i+1]) {
a[j]=a[i];
j++;
}
}
a[j]=a[a.length-1];
for (int i = 0; i <= j; i++) {
System.out.println(a[i]);
}
}
}
14. First Non Repeat Character
String str="abc ade bcd efg ghi";
For(char i: str.toCharArray()) {
if(str.indexOf(i)==str.lastIndexof(i)) {
System.out.println("First non Repeat character is : "+i);
break;
}
}
15. Reverse only string not numbers using string buffer
public class Reverseonlystring {
public static void main(String[] args) {
String input = "123CBA456ZYX789";
String[] parts = input.split("(?=\\d)");
StringBuilder sb = new StringBuilder();
for (String part : parts) {
if (part.matches("\\d+")) {
sb.append(part);
}
else {
StringBuilder num = new StringBuilder(part);
sb.append(num.reverse());
}
}
System.out.println(sb.toString());
}
}
16. Reverse string only without special character
public class Reverseonlycharnotspecialcharachers {
public static void main(String[] args) {
String str1 = "a!!!b.c.d,e'f,ghi";
char[] str = str1.toCharArray();
int j = str.length - 1, i = 0;
String rev="";
while (i < j)
{
if (!Character.isAlphabetic(str[i]))
i++;
else if(!Character.isAlphabetic(str[j]))
j--;
else
{
char tmp = str[i];
str[i] = str[j];
str[j] = tmp;
i++;
j--;
}
}
for(char a : str) {
rev= rev+a;
}
System.out.println("Output string: " + rev);
}
}
17. Write a program to find missing number in array using c#
Public class MissingArray()
{
int[] arr={4,5,1,4,9,1};
for(int i=0;i<arr.Length;i++)
{
if(Array.Indexof(arr,i+1) <0)
{
Console.WriteLine("Missing Number is : "+ (i+1));
}
}
}
Output: Missing Number is : 2
Output: Missing Number is : 3
Output: Missing Number is : 6
18. Write a program to find missing number in array and duplicate values.
Public class MissingAndDuplicateArray()
{
int[] arr={4,5,1,4,9,1};
for(int i=0;i<arr.Length;i++)
{
if(Array.Indexof(arr,i+1) <0)
{
Console.WriteLine("Missing Number is : "+ (i+1));
}
}
Console.WriteLine("=========");
//Duplicate values in array
for(int i=0;i<arr.Length;i++)
{
for(int j=i+1;j<arr.Length;j++)
{
if(arr[i]==arr[j])
{
Console.WriteLine("Duplicate number is :"+ arr[j]);
break;
}
}
}
}
Output: Missing Number is : 2
Output: Missing Number is : 3
Output: Missing Number is : 6
===========
Duplicate number : 4
Duplicate number : 1
////--- Oracle ----////
1. Explain About Oops Concepts in terms of Framework?
2. What would be answer the if we have different type of return type
ex. Sum(int x, int y)
{ return int;}
Sum(int x, int y)
{ return double;}
3. Can we access public method
Super Class--> Private Sum(int x, int y)
Subclass --> Public Sum(int x, int y)
4.
5.Can i