JavaInterview Questions
MainMethodInvoking
import java.util.*;
public class MainMethodInvoking {
public static void main(Integer args) {
System.out.println("the main method invoked at Integer");
}
public static void main(Character args) {
System.out.println("the main method invoked at Character");
}
public static void main(String[] args) {
System.out.println("original main method has been invoked");
}
}
MaxNumbersInArray
import java.util.*;
public class MaxNumbersInArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
int max=arr[0];
for(int i=1;i<arr.length;i++) {
if(arr[i]>max) {
max=arr[i];
}
}
System.out.println("the max number in array is:"+max);
}
}
MedianOfUnSortedArrayInIntegers
import java.util.Scanner;
public class MedianOfUnSortedArrayInIntegers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int arr1[]=new int[a];
int arr2[]=new int[b];
for(int i=0;i<a;i++) {
arr1[i]=sc.nextInt();
}
for(int i=0;i<b;i++) {
arr2[i]=sc.nextInt();
}
System.out.println("Median of the said array of Integers:"+getMedian(arr1));
System.out.println("Median of the said array of Integers:"+getMedian(arr2));
}
public static int getMedian(int array[]) {
if(array.length%2==0) {
int mid=array.length/2;
return (array[mid]+array[mid-1])/2;
}
return array[array.length/2];
}
}
MergeTwoArrays
import java.util.Arrays;
import java.util.Scanner;
public class MergeTwoArrays {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
//int a=sc.nextInt();
//int b=sc.nextInt();
//int arr1[]=new int[a];
//int arr2[]=new int[a];
//for(int i=0;i<a;i++) {
// arr1[i]=sc.nextInt();
//}
//for(int i=0;i<b;i++) {
// arr2[i]=sc.nextInt();
//}
int arr1[]=new int[8];
arr1[0]=1;
arr1[1]=2;
arr1[2]=3;
arr1[3]=4;
int arr2[]= {2,5,7,8};
int m=4;
int n=4;
int i=m-1,j=n-1,index=m+n-1;
while(i>=0 && j>=0) {
if(arr1[i]>arr2[j]) {
arr1[index--]=arr1[i--];
}
else {
arr1[index--]=arr2[j--];
}
}
while(i>=0) {
arr1[index--]=arr1[i--];
}
while(j>=0) {
arr1[index--]=arr2[j--];
}
System.out.println("the merged array is:"+Arrays.toString(arr1));
}
}
MinNumberInArray
import java.util.*;
public class MinNumberInArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
int min=arr[0];
for(int i=1;i<arr.length;i++) {
if(arr[i]<min) {
min=arr[i];
}
}
System.out.println("the min value is:"+min);
}
}
MoveEveryZeroTORightSideInArray
import java.util.Scanner;
import java.util.*;
public class MoveEveryZeroTORightSideInArray {
public static int[]A(int[]arr){
if(arr==null) {
throw new IllegalArgumentException("Null Array!!!");
}
boolean swap=true;
while(swap) {
swap=false;
for(int i=0;i<arr.length-1;i++) {
if(arr[i]==0 && arr[i+1]!=0) {
swap(arr,i,i+1);
swap=true;
}
}
}
return arr;
}
public static void swap(int arr[],int a,int b) {
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
System.out.println("the orginal string is:"+Arrays.toString(arr));
int res[]=A(arr);
System.out.println("the moved zero value is:"+Arrays.toString(arr));
}
}
MovePositiveNumberToRightMoveNegativeNumberToLeftInArray
import java.util.Scanner;
import java.util.*;
public class MovePositiveNumberToRightMoveNegativeNumberToLeftInArray {
public static int[] splitarray(int arr[]){
if(arr==null) {
throw new IllegalArgumentException("Null Array!!!");
}
boolean flag=true;
while(flag) {
flag=false;
for(int i=0;i<arr.length-1;i++) {
if(arr[i]>arr[i+1]) {
swap(arr,i,i+1);
flag=true;
}
}
}
return arr;
}
private static void swap(int arr[],int right,int left) {
int temp=arr[right];
arr[right]=arr[left];
arr[left]=temp;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
System.out.println("the original array is:"+Arrays.toString(arr));
int res[]=splitarray(arr);
System.out.println("\nresult:"+Arrays.toString(res));
}
}
MultipleInterfaces
import java.util.*;
interface E{
public void add(int a,int b);
}
interface F{
public void mul(int c,int d);
}
class G{
public void add(int a,int b) {
System.out.println(a+b);
}
public void mul(int c,int d) {
System.out.println(c*d);
}
void min(int e,int f) {
System.out.println(e-f);
}
}
public class MultipleInterfaces {
public static void main(String[] args) {
G obj=new G();
obj.add(10,20);
obj.mul(20,30);
obj.min(30,40);
}
}
MultiplicationOfNumbers
import java.util.*;
public class MultiplicationOfNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
for(int i=1;i<=10;i++) {
System.out.println(a+"x"+i+"="+a*i);
}
}
}
MultiplicationOfTables
import java.util.*;
public class MultiplicationOfTables {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
for(int i=1;i<=b;i++) {
System.out.println(a+"*"+i+"="+a*i);
}
}
}
MultiplyingCorrespondingTwoElementsInArray
import java.util.Scanner;
public class MultiplyingCorrespondingTwoElementsInArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
String res="";
int arr1[]=new int[a];
int arr2[]=new int[b];
for(int i=0;i<a;i++) {
arr1[i]=sc.nextInt();
}
for(int i=0;i<b;i++) {
arr2[i]=sc.nextInt();
}
for(int i=0;i<arr1.length;i++) {
int num1=arr1[i];
int num2=arr2[i];
res+=Integer.toString(num1*num2)+" ";
}
System.out.println("the multiplication of Array is:"+res);
}
}
MultiplyTwoBinaryNumbers
import java.util.*;
public class MultiplyTwoBinaryNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
long a=sc.nextLong();
long b=sc.nextLong();
int fac=1,digit;
long mul=0;
while(b!=0) {
digit=(int)(b%10);
if(digit==1) {
a=a*fac;
mul=binaryproduct((int)a,(int)mul);
}
else {
a=a*fac;
}
b=b/10;
fac=10;
}
System.out.print("Product of two binary numbers: " + mul+"\n");
}
static int binaryproduct(int a,int b) {
int i=0,rem=0;
int sum[]=new int[20];
int bin_res=0;
while(a!=0 || b!=0) {
sum[i++]=(a%10+b%10+rem)%2;
rem=(a%10+b%10+rem)/2;
a=a/10;
b=b/10;
}
if(rem!=0) {
sum[i++]=rem;
}
--i;
while(i>=0) {
bin_res=bin_res*10+sum[i--];
}
return bin_res;
}
}
MultiplyTwoNumbersWithoutUsingOperators
import java.util.Scanner;
public class MultiplyTwoNumbersWithoutUsingOperators {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int sum=0;
for(int i=1;i<=a;i++) {
sum+=b;
System.out.println(sum);
}
System.out.println("the multiplication of:"+a+" and "+b+"="+sum);
}
}
NeonNumber
import java.util.*;
public class NeonNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int sum=0;
int sqr=a*a;
while(sqr!=0) {
int digit=sqr%10;
sum+=digit;
sqr=sqr/10;
}
if(sum==a) {
System.out.println("the number is NeonNumber:"+sum);
}
else {
System.out.println("the number is not a NeonNumber:"+sum);
}
}
}
NextToLastWordOfSentence
import java.util.*;
public class NextToLastWordOfSentence {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
String b[]=a.split("[ ]+");
System.out.println("the penultimate word is:"+b[b.length-2]);
}
}
NODigitsInNumber
import java.util.Scanner;
public class NODigitsInNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int count=0;
while(a!=0) {
a=a/10;
count++;
}
System.out.println(count);
}
}
NOEvenOddNumber
import java.util.*;
public class NOEvenOddNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int even=0;
int odd=0;
while(a!=0) {
int rem=a%10;
if(rem%2==0) {
even++;
}
else {
odd++;
}
a=a/10;
}
System.out.println("the number of Even count:"+even);
System.out.println("the number of Odd count:"+odd);
}
}
NoOccurencesInAString
import java.util.*;
public class NoOccurencesInAString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
char b=sc.next().charAt(0);
int count=0;
for(int i=0;i<a.length();i++) {
if(a.charAt(i)==b) {
count++;
}
}
System.out.println("the number of Occurences is:"+count);
}
}
NullPointerException
import java.util.*;
class A{
public static void add(String a) {
System.out.println(a.length());
}
}
public class NullPointerException extends A {
public static void main(String[] args) {
String a=null;
add(a);
}
}
NumbersOfLettersInAString
import java.util.*;
public class NumbersOfLettersInAString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
HashMap<Character,Integer>H=new HashMap();
for(int i=a.length()-1;i>=0;i--) {
if(H.containsKey(a.charAt(i))) {
int c=H.get(a.charAt(i));
H.put(a.charAt(i),++c);
}
else {
H.put(a.charAt(i),1);
}
}
System.out.println(H);
}
}
NumberToBinary
import java.util.Scanner;
public class NumberToBinary {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
String binaryNumber=Integer.toBinaryString(a);
System.out.println("the binary representation of:"+a+" is:"+binaryNumber);
}
}
OddNumbersInRange
import java.util.*;
public class OddNumbersInRange {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int count=0;
for(int i=a;i<=b;i++) {
if(i%2!=0) {
count++;
System.out.println("the odd numbers are is:"+i);
}
}
System.out.println("the number count is:"+count);
}
}
palindromenumber
import java.util.Scanner;
public class palindromenumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=a;
int rev=0;
while(a!=0) {
rev=rev*10+a%10;
a=a/10;
}
if(rev==b) {
System.out.println("the number is plaindrome:"+rev);
}
else {
System.out.println("the number is not a plaindrome:"+rev);
}
}
}
palindromString
import java.util.Scanner;
public class palindromString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=a;
String rev=" ";
int len=a.length();
for(int i=len-1;i>=0;i--){
rev=rev+a.charAt(i);
}
if(rev.equals(b)){
System.out.println("the string is plaindrome:"+rev);
}
else{
System.out.println("the string is not a plaindrome:"+rev);
}
}
}
PatterOfCombinationsOfAlphabet
import java.util.*;
public class PatterOfCombinationsOfAlphabet {
public static void main(String[] args) {
// TODO Auto-generated method stub
// J a v v a
// J a a v v a a
// J J aaaaa V V aaaaa
// JJ a a V a a
System.out.println(" J a v v a ");
System.out.println(" J a a v v a a");
System.out.println("J J aaaaa V V aaaaa");
System.out.println(" JJ a a V a a");
}
}
PerfectNumber
import java.util.*;
public class PerfectNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int perno=0;
int i=1;
while(i<=a/2) {
if(a%i==0) {
perno+=i;
i++;
}
}
if(a==perno) {
System.out.println("the number is perfect");
}
else {
System.out.println("the number is not perfect");
}
}
}
PerfectSquareNumber
import java.util.*;
public class PerfectSquareNumber {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
for(int i=1;i<a;i++) {
if(i*i==a) {
System.out.println("the number is square root:"+i);
break;
}
}
}
}
PrimeNumber
import java.util.*;
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int c=0;
for(int i=1;i<=a;i++) {
if(a%i==0) {
c++;
}
}
if(c==2) {
System.out.println("the number is primeNumber:"+c);
}
else {
System.out.println("the number is not primeNumber:"+c);
}
}
}
PrintBollenArrayItContainsTrueFalse
import java.util.*;
public class PrintBollenArrayItContainsTrueFalse {
public static void main(String[] args) {
// TODO Auto-generated method stub
boolean arr[][]= {{true,false,true},
{false,true,false}};
int row=arr.length;
int col=arr[0].length;
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
if(arr[i][j]) {
System.out.print("t");
}
else {
System.out.print("f");
}
}
System.out.println();
}
}
}
PrintingDateAndTimeinSpecificFormate
import java.util.*;
import java.util.Calendar;
import java.util.TimeZone;
import java.text.SimpleDateFormat;
public class PrintingDateAndTimeinSpecificFormate {
public static void main(String[] args) {
// TODO Auto-generated method stub
SimpleDateFormat a=new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
a.setCalendar(Calendar.getInstance(TimeZone.getTimeZone("GMT")));
System.out.println("\nNow:"+a.format(System.currentTimeMillis()));
}
}
PrintOddEvenNumbers
import java.util.*;
public class PrintOddEvenNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
int even=0;
int odd=0;
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
if(arr[i]%2==0) {
even++;
}else {
odd++;
}
}
System.out.println("the even count is:"+even);
System.out.println("the odd count is:"+odd);
}
}
PrintWithoutDuplicateNumbersInArray
import java.util.*;
public class PrintWithoutDuplicateNumbersInArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
TreeSet<Integer>t=new TreeSet();
for(int i=0;i<arr.length;i++) {
t.add(arr[i]);
}
System.out.println(t);
}
}
ProductOfTwoNumbers
import java.util.*;
public class ProductOfTwoNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=a*b;
System.out.println("product of two number is:"+c);
}
}
QUEUE
import java.util.*;
public class QUEUE {
public static void main(String[] args) {
Queue<String>a=new LinkedList();
a.add("obuli");
a.add("purusothaman");
a.add("k");
String b=a.peek();
System.out.println("peek value is:"+b);
String c=a.poll();
System.out.println("the poll value is:"+c);
String d=a.remove();
System.out.println("the remove value is:"+d);
}
}
RemoveDuplicatesInArray
import java.util.Arrays;
import java.util.Scanner;
public class RemoveDuplicatesInArray {
public static int remove(int b[]) {
if(b==null) {
return 0;
}
if(b.length<=1) {
return b.length;
}
int currentpos=0;
int movingpos;
for(movingpos=1;movingpos<b.length;movingpos++) {
if(b[currentpos]!=b[movingpos]) {
b[currentpos+1]=b[movingpos];
currentpos++;
}
}
return currentpos+1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
System.out.println("the original String is:"+Arrays.toString(arr));
System.out.println("the length of the original string is:"+arr.length);
System.out.println("after removing duplicates the length of the array is:"+remove(arr));
}
}
RemoveDuplicatesPrestTwiceInArray
import java.util.Arrays;
import java.util.Scanner;
public class RemoveDuplicatesPrestTwiceInArray {
static int remove(int b[]) {
if(b==null || b.length==0) {
return 0;
}
int c=1;
for(int i=2;i<b.length;i++) {
if(b[i]!=b[c]||(b[i]==b[c]&&b[i]!=b[c-1])) {
c++;
b[c]=b[i];
}
}
return c+1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
System.out.println("the orginal String is:"+Arrays.toString(arr));
System.out.println("the orginal String length is:"+arr.length);
System.out.println("after removing Twice Duplicate the length of the String is:"+remove(arr));
}
}
RemoveNumbersInString
import java.util.*;
public class RemoveNumbersInString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
char b[]=a.toCharArray();
String c="";
for(int i=0;i<b.length;i++) {
if(!Character.isDigit(b[i])) {
c+=b[i];
}
}
System.out.println("the string is:"+c);
}
}
RemoveSpecialChracterFromString
import java.util.*;
public class RemoveSpecialChracterFromString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a="vin%ay$";
ArrayList<Integer>b=new ArrayList();
for(int i=0;i<a.length();i++) {
if(a.charAt(i)>='a'&& a.charAt(i)<='z') {
continue;
}
else {
b.add(i);
}
}
String c="";
int d=0;
for(int i=a.length()-1;i>=0;i--) {
if(b.contains(i)) {
c+=a.charAt(d++);
}
else {
c+=a.charAt(i);
}
}
System.out.println(c);
}
}
RemovingVowels
import java.util.*;
public class RemovingVowels {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=" ";
String b=sc.next();
String c=sc.next();
for(int i=0;i<c.length();i++) {
if(b.indexOf(c.charAt(i))==-1){
a+=c.charAt(i);
}
}
System.out.print(a);
}
}
RevereseStringLeftToRight
import java.util.Arrays;
import java.util.Scanner;
public class RevereseStringLeftToRight {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next();
char b[]=a.toCharArray();
int offset=3;
int len=b.length;
offset%=len;
reverse(b,0,len-offset-1);
reverse(b,len-offset,len-1);
reverse(b,0,len-1);
System.out.println(Arrays.toString(b));
}
private static void reverse(char B[],int start,int end) {
while(start<end) {
char d=B[start];
B[start]=B[end];
B[end]=d;
start++;
end--;
}
}
}
RevereseTheContentOfASentenceWithoutReverseEveryWord
import java.util.Scanner;
import java.util.*;
public class RevereseTheContentOfASentenceWithoutReverseEveryWord {
public static String A(String a) {
if(a==null) {
throw new IllegalArgumentException("Input param can't be null");
}
StringBuilder b=new StringBuilder();
String c[]=a.split(" ");
for(int i=c.length-1;i>=0;i--) {
b.append(c[i]);
if(i!=0) {
b.append(" ");
}
}
return b.toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
System.out.println("the orginal string is:"+a);
System.out.println("the modified string is:"+A(a));
}
}
ReverseAString
import java.util.*;
public class ReverseAString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b="";
char c=0;
for(int i=0;i<a.length();i++) {
c=a.charAt(i);
b=c+b;
}
System.out.println("the revesed string is:"+b);
}
}
RotateArrayInLeftDirection
import java.util.Arrays;
import java.util.Scanner;
public class RotateArrayInLeftDirection {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
int newarr[]= {arr[1],arr[2],arr[0]};
System.out.println("the right rotation of array is:"+Arrays.toString(newarr));
}
}
SmallestAndLargestElementInArray
import java.util.*;
public class SmallestAndLargestElementInArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
Integer arr[]=new Integer[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
System.out.println("the original array is:"+Arrays.toString(arr));
int k=2;
Arrays.sort(arr);
System.out.println(arr[k-1]+" "+"smallest element in array");
System.out.println("the largest value in array:");
Arrays.sort(arr,Collections.reverseOrder());
System.out.println(arr[k-1]);
}
}
SquareRootOfaNumber
import java.util.*;
public class SquareRootOfaNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int i=2;
while(i<=a/2) {
if(a/i==i) {
System.out.print("the number is square root");
break;
}
i++;
}
}
}
SquareRootOfNumber
import java.util.Scanner;
public class SquareRootOfNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
System.out.println("the squareRoot of the number is:"+(Math.sqrt(a)));
}
}
stack
import java.util.*;
public class stack {
public static void main(String[] args) {
Stack<String>a=new Stack();
a.push("obuli");
a.push("purusothaman");
a.push("k");
System.out.println(a);
System.out.println(a.pop());
System.out.println(a.peek());
System.out.println(a.search("obuli"));
boolean c=a.empty();
System.out.println(c);
}
}
startwithanintegerNdivideNby2ifNisEvenorMultiplyby3andAdd1ifNisOdd
import java.util.*;
public class startwithanintegerNdivideNby2ifNisEvenorMultiplyby3andAdd1ifNisOdd {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
while(a!=1) {
if(a%2==0) {
a=a/2;
}
else {
a=(3*a+1)/2;
}
}
System.out.println("value of a is:"+a);
}
}
StringBufferAndStringBuilder
import java.util.*;
public class StringBufferAndStringBuilder {
public static void main(String[] args) {
StringBuffer a=new StringBuffer("Obuli");
a.append("purusothaman");
System.out.println("the StringBuffer is:"+a);
StringBuilder b=new StringBuilder("sachin");
b.append("tendulkar");
System.out.println("teh string builder is:"+b);
}
}
StringEquals
import java.util.*;
public class StringEquals {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b="";
int d=0;
for(int i=a.length()-1;i>=0;i--) {
b+=a.charAt(i);
}
System.out.println(b);
for(int i=0;i<a.length();i++) {
if(a.charAt(i)==b.charAt(i)) {
d++;
}
}
System.out.println(d);
}
}
StringLengthIsLessThen3UseSplCharacter
import java.util.Scanner;
public class StringLengthIsLessThen3UseSplCharacter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next();
int len=a.length();
if(len>=3) {
System.out.println(a.substring(0,3));
}
else if(len==1) {
System.out.println(a.charAt(0)+"##");
}
else {
System.out.println("###");
}
}
}
StringStartsWith
import java.util.Scanner;
public class StringStartsWith {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next();
System.out.println(a.startsWith("hello"));
}
}
StringToInteger
import java.util.*;
public class StringToInteger {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
System.out.println("the string is:"+a);
int b=Integer.parseInt(a);
System.out.println("the integer is:"+b);
}
}
SUMOfDigitsInNumber
import java.util.*;
public class SUMOfDigitsInNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int sum=0;
while(a!=0) {
sum=sum+a%10;
a=a/10;
}
System.out.println("the sum of given number is:"+sum);
}
}
SumOfPrimeNumbersUpToHundered
import java.util.*;
public class SumOfPrimeNumbersUpToHundered {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=1;
int c=0;
int n=0;
while(c<100) {
n++;
if(n%2!=0) {
//check even number
if(isPrime(n)) {
sum+=n;
}
}
c++;
}
System.out.println("the sum of upto 100 prime number is:"+sum);
}
public static boolean isPrime(int a) {
for(int i=3;i*i<=a;i+=2) {
if(a%i==0) {
return false;
}
}
return true;
}
}
SumofTwoNumbersIsEqualToThirdNumber
import java.util.*;
public class SumofTwoNumbersIsEqualToThirdNumber {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
System.out.println("the result is:"+sumoftwo(a,b,c));
System.out.print("\n");
}
public static boolean sumoftwo(int d,int e,int f) {
return((d+e)==f || (e+f)==d || (f+d)==e);
}
}
SwappingANumber
import java.util.*;
public class SwappingANumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
System.out.println("the value of a is:"+a);
System.out.println("the value of b is:"+b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("the value of a is:"+a);
System.out.println("the value of b is:"+b);
}
}
SwappingOfFirstAndLastElementOfArray
import java.util.Arrays;
import java.util.Scanner;
public class SwappingOfFirstAndLastElementOfArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
System.out.println("the original Array is:"+Arrays.toString(arr));
int x=arr[0];
arr[0]=arr[arr.length-1];
arr[arr.length-1]=x;
System.out.println("the swapping array is:"+Arrays.toString(arr));
}
}
TheNumberDivisbleByTwoAndThree
import java.util.*;
public class TheNumberDivisbleByTwoAndThree {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=0;
for(int i=a;i<=b;i++) {
if(i%2==0) {
c++;
System.out.println("the 2 divisble number is:"+i);
if(i%3==0) {
System.out.println("the 3 divisble number is:"+i);
}
else {
System.out.println("not divisble by 3 numbers is:"+i);
}
}
}
}
}
ThreeIntValueIsOneOfThemIsLessorMoreThanTheOtherValues
import java.util.Scanner;
public class ThreeIntValueIsOneOfThemIsLessorMoreThanTheOtherValues {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
System.out.println("the result is:"+(Math.abs(a-b)>=20 || Math.abs(b-c)>=20 || Math.abs(c-a)>=20));
}
}
ThreeNumbersInBetweenRanges
import java.util.*;
public class ThreeNumbersInBetweenRanges {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
for(int i=a;i<=b;i++) {
if(iscontains(i)) {
System.out.print(i+" ");
}
}
}
static boolean iscontains(int n) {
while(n!=0) {
int rem=n%10;
if(rem==3) {
return true;
}
n=n/10;
}
return false;
}
}
treeset
import java.util.*;
public class treeset {
public static void main(String[] args) {
// TODO Auto-generated method stub
TreeSet<Integer>a=new TreeSet();
a.add(10);
a.add(20);
a.add(30);
Iterator<Integer>b=a.iterator();
while(b.hasNext()) {
System.out.println(b.next());
}
}
}
TwoStringAndTestIfSecondStringContainsTheFirstString
import java.util.Scanner;
public class TwoStringAndTestIfSecondStringContainsTheFirstString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
String b=sc.nextLine();
System.out.println("result is:"+a.contains(b));
}
}
UniqueThreeDigitNumber1234
import java.util.*;
public class UniqueThreeDigitNumber1234 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=0;
for(int i=1;i<=4;i++) {
for(int j=1;j<=4;j++){
for(int k=1;k<=4;k++) {
if(k!=i && k!=j && i!=j ) {
a++;
System.out.println(i+" "+j+" "+k);
}
}
}
}
System.out.println("total number of three digit number is:"+a);
}
}
UpperCaseToLowerCase
import java.util.*;
public class UpperCaseToLowerCase {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next();
System.out.println("the uppercase of string is:"+a);
a=a.toLowerCase();
System.out.println("the lowerscase of string is:"+a);
}
}
VowelConsonentCountInString
import java.util.*;
public class VowelConsonentCountInString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=sc.next();
int vcount=0;
for(int i=0;i<a.length();i++) {
if(b.contains(a.substring(i,i+1))) {
vcount++;
}
}
int count=a.length()-vcount;
System.out.println("consonent:"+count);
System.out.println("vowel:"+vcount);
}
}
VowelOrConstant
import java.util.*;
public class VowelOrConstant {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
char a=sc.next().charAt(0);
switch(a) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
System.out.println("vowel");
break;
default:
System.out.println("Constant");
}
}
}
VuramQuestion
import java.util.*;
public class VuramQuestion {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int count=0;
int arr1[]=new int[a];
int arr2[]=new int[b];
for(int i=0;i<a;i++) {
arr1[i]=sc.nextInt();
}
for(int j=0;j<b;j++) {
arr2[j]=sc.nextInt();
}
for(int i=0;i<arr1.length;i++) {
for(int j=0;j<arr2.length;j++) {
if(arr1[i]==arr2[j]) {
count++;
}
}
if(count==0) {
System.out.println(arr1[i]);
}
}
}
}