JavaInterview Questions
AddAllTheDigitsGivenPositiveIntegerUntillResultHasSingleDigitimport java.util.Scanner;
public class AddAllTheDigitsGivenPositiveIntegerUntillResultHasSingleDigit {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if(a!=0) {
System.out.println("the sum of digit is:"+(a==0?0:(a%9==0?9:a%9)));
}
}
}
AddTwoBinaryNumbers
import java.util.*;
public class AddTwoBinaryNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long a=sc.nextLong();
long b=sc.nextLong();
int i=0,rem=0;
int sum[]=new int[20];
while(a!=0 || b!=0) {
sum[i++]=(int)((a%10+b%10+rem)%2);
rem=(int)((a%10+b%10+rem)/2);
a=a/10;
b=b/10;
}
if(rem!=0) {
sum[i++]=rem;
}
--i;
System.out.print("Sum of two binary numbers: ");
while (i >= 0) {
System.out.print(sum[i--]);
}
System.out.print("\n");
}
}
AddTwoNumbers
import java.util.*;
public class AddTwoNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=a+b;
System.out.print("the sum of two number is:"+c);
}
}
AddTwoNumbersWithoutUsingOpeartors
import java.util.Scanner;
public class AddTwoNumbersWithoutUsingOpeartors {
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("the addition of two numbers is:"+c);
}
}
AmstrongForMoreNumbers
import java.util.*;
public class AmstrongForMoreNumbers {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=a;
int sum=0,r,count=0;
int n1=a,n=a;
while(n1!=0) {
count++;
n1=n1/10;
}
while(n>0) {
r=n%10;
n=n/10;
int ans=1;
for(int i=0;i<count;i++) {
ans=r*ans;
}
sum=sum+ans;
}
if(b==sum) {
System.out.print("the given number is Amstrong");
}
else {
System.out.print("the given number is not Amstrong");
}
}
}
AmstrongNumber
import java.util.*;
public class AmstrongNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=a;
int rem=0;
int arm=0;
while(a!=0) {
rem=a%10;
arm+=(rem*rem*rem);
a=a/10;
}
if(b==arm) {
System.out.println("the number is AmstrongNumber:"+b);
}
else {
System.out.println("the number is not a AmstrongNumber:"+b);
}
}
}
AnagramOrNot
import java.util.*;
public class AnagramOrNot {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=sc.next();
a=a.toLowerCase();
b=b.toLowerCase();
if(a.length()==b.length()) {
char c[]=a.toCharArray();
char d[]=b.toCharArray();
Arrays.sort(c);
Arrays.sort(d);
boolean e=Arrays.equals(c,d);
if(e) {
System.out.println("Anagram");
}else {
System.out.println("Not a Anagram");
}
}
}
}
ArithmeticException
import java.util.*;
public class ArithmeticException {
public static void main(String[] args) {
try {
int a=100/0;
}
catch(Exception e) {
{System.out.println(e);}
System.out.println("rest of the code is...");
}
finally {
System.out.println("welcome");
}
}
}
ArrangeStringPythonToHONPYTHONHON
import java.util.Scanner;
public class ArrangeStringPythonToHONPYTHONHON {
public static void main(String[] args) {
// TODO Auto-generated method stub
//python--honpythonhon last 3 character arrange it
Scanner sc=new Scanner(System.in);
String a=sc.next();
int len=3;
if(len>a.length()) {
len=a.length();
}
String b=a.substring(a.length()-3);
System.out.println(b+a+b);
}
}
ArrayContainsThisVlauesOrNot
import java.util.Scanner;
public class ArrayContainsThisVlauesOrNot {
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];
int res=1;
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<arr.length;i++) {
if(arr[i]==10 || arr[i]==30) {
res=0;
}
}
if(res==1) {
System.out.println(String.valueOf(false));
}
else {
System.out.println(String.valueOf(true));
}
}
}
ArrayDeepToString
import java.util.*;
public class ArrayDeepToString {
public static void main(String[] args) {
// int arr[][]= {{1,2},{3,4}};
// System.out.println(Arrays.deepToString(arr));
int arr[][]= {{1,2},{3,4}};
System.out.println(Arrays.deepToString(arr));
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[i].length;j++) {
System.out.print(arr[j][i]+" ");
}
System.out.println();
}
}
}
Arraylist
import java.util.*;
public class Arraylist {
public static void main(String[] args) {
ArrayList<String> a=new ArrayList();
a.add("obuli");
a.add("purusothaman");
a.add("k");
ArrayList<String>b=new ArrayList();
b.addAll(a);
System.out.println(b);
b.removeAll(a);
System.out.println(b);
System.out.println(a.clone());//CLONE
System.out.println(a.contains("obuli"));//CONTAINS
System.out.println(a.indexOf("purusothaman"));//INDEX OF
}
}
ArrayOddEvenNumbers
import java.util.*;
public class ArrayOddEvenNumbers {
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();
}
for(int i=0;i<arr.length;i++) {
if(arr[i]%2==0) {
System.out.println("the even numbers in array is:"+arr[i]);
}
else {
System.out.println("the odd numbers in array is:"+arr[i]);
}
}
}
}
ASCIIValueOfGivenCharacter
import java.util.*;
public class ASCIIValueOfGivenCharacter {
public static void main(String[] args) {
// TODO Auto-generated method stub
int ch='Z';
System.out.println("the ascii value is:"+ch);
}
}
AtAnyPositionIndexInArray
import java.util.*;
public class AtAnyPositionIndexInArray {
public static int[] add(int len,int value,int pos,int arr[]) {
int i;
int newarr[]=new int[len+1];
for( i=0;i<len+1;i++) {
if(i<pos-1) {
newarr[i]=arr[i];
}
else if(i==pos-1) {
newarr[i]=value;
}
else {
newarr[i]=arr[i-1];
}
}
return newarr;
}
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 len=arr.length;
int pos=sc.nextInt();
int value=sc.nextInt();
arr=add(len,value,pos,arr);
System.out.println(Arrays.toString(arr));
}
}
AtlastpositionIndexinArray
import java.util.*;
public class AtlastpositionIndexinArray {
public static int[] add(int len,int pos,int value,int arr[]) {
int i;
int newarr[]=new int[len+1];
for(i=0;i<len;i++) {
newarr[i]=arr[i];
newarr[len]=value;
}
return newarr;
}
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 len=arr.length;
int pos=sc.nextInt();
int value=sc.nextInt();
arr=add(len,pos,value,arr);
System.out.println(Arrays.toString(arr));
}
}
AverageOfNumbers
import java.util.*;
public class AverageOfNumbers {
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();
int d=sc.nextInt();
int e=sc.nextInt();
float f=(a+b+c+d+e)/5;
System.out.print(f);
}
}
CallByValue
import java.util.*;
public class CallByValue {
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();
System.out.println(a+" "+b);
swap(a,b);
System.out.println(a+" "+b);
}
public static void swap(int a,int b) {
int c=a;
a=b;
b=c;
System.out.println(a+" "+b);
}
}
ChangingRowsAndColums
import java.util.*;
public class ChangingRowsAndColums {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[][]= {
{10,20,30},
{40,50,60}
};
System.out.print("orginal array is:\n");
print_array(arr);
System.out.print("After changing rows and columns of an array:");
transpose(arr);
}
private static void transpose(int [][] arr) {
int[][] newarr=new int[arr[0].length][arr.length];
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[0].length;j++) {
newarr[j][i]=arr[i][j];
}
}
print_array(newarr);
}
private static void print_array(int[][]arr) {
for(int i=0;i<arr.length;i++) {
for(int j=0;j<arr[0].length;j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
CheckArrayContains3IncreasingAdjacentNumbers
import java.util.Scanner;
public class CheckArrayContains3IncreasingAdjacentNumbers {
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 res=1;
for(int i=0;i<=arr.length-3;i++) {
if(arr[i]+1==arr[i+1] && arr[i+1]+1==arr[i+2]) {
res=0;
}
if(res==1) {
System.out.println(String.valueOf(false));
}
else {
System.out.println(String.valueOf(true));
}
}
}
}
CheckArrayContains4or7
import java.util.Scanner;
public class CheckArrayContains4or7 {
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();
}
if(arr[0]==4 || arr[0]==7) {
System.out.println("true");
}
else {
System.out.println(arr[1]==4 || arr[1]==7);
}
}
}
CheckFirstAndLastPositionOfArrayIsSameOrNot
import java.util.Scanner;
public class CheckFirstAndLastPositionOfArrayIsSameOrNot {
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 result is:"+(arr[0]==10 || arr[arr.length-1]==10));
}
}
CheckGivenNumberIsPerfectSuareOrNot
import java.util.Scanner;
import java.util.*;
public class CheckGivenNumberIsPerfectSuareOrNot {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int c=a;
System.out.println("the perfect square number is:"+Math.sqrt(a));
}
}
CheckIf3GivenSideLengthCanMakeATriangleOrNot
import java.util.Scanner;
public class CheckIf3GivenSideLengthCanMakeATriangleOrNot {
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 3 input values makes triangle:"+A(a,b,c));
}
public static boolean A(int a,int b,int c) {
return (a+b>c&&b+c>a&&c+a>b);
}
}
CheckIfGivenNumber10isGreterThan20InGivenArray
import java.util.Scanner;
public class CheckIfGivenNumber10isGreterThan20InGivenArray {
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 ctr1=0;
int ctr2=0;
for(int i=0;i<arr.length;i++) {
if(arr[i]==10) {
ctr1++;
}
if(arr[i]==20) {
ctr2++;
}
}
System.out.println("the result is:"+String.valueOf(ctr1>ctr2));
}
}
CheckStartAndEndOfArrayIsSame
import java.util.Scanner;
public class CheckStartAndEndOfArrayIsSame {
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 res=0;
int l=2;
int start=0;
int end=arr.length-l;
for(;l>0;l--) {
if(arr[start]!=arr[end]) {
res=1;
}
else {
start++;
end++;
}
}
if(res==1) {
System.out.println(String.valueOf(false));
}
else {
System.out.println(String.valueOf(true));
}
}
}
CheckTwoArraysareEqualOrNot
import java.util.*;
public class CheckTwoArraysareEqualOrNot {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a[]= {1,2,3,4,5};
int b[]= {1,2,3,4,5};
boolean c=Arrays.equals(a, b);
if(c==true) {
System.out.println("the array is equal");
}
else {
System.out.println("the array is not equal");
}
// int a=sc.nextInt();
// int b=sc.nextInt();
// int arr[][]=new int[a][b];
//
// for(int i=0;i<a;i++) {
// for(int j=0;j<b;j++) {
// arr[i][j]=sc.nextInt();
//
// }
// }
// for(int i=0;i<a;i++) {
// for(int j=0;j<b;j++) {
// System.out.print(arr[i][j]+"");
//
// }
// System.out.println();
// }
}
}
CombinationOfArithmetic
import java.util.*;
public class CombinationOfArithmetic {
public static void main(String[] args) {
// Input first number: 125
// Input second number: 24
// Expected Output :
// 125 + 24 = 149
// 125 - 24 = 101
// 125 x 24 = 3000
// 125 / 24 = 5
// 125 mod 24 = 5
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=a+b;
System.out.println("the sum of two number is:"+c);
int d=a-b;
System.out.println("the minus of two number is:"+d);
int e=a*b;
System.out.println("the multiplication of two number is:"+e);
float f=a/b;
System.out.println("the divided of two number is:"+f);
float g=a%b;
System.out.println("the modulos of two number is:"+g);
}
}
CombinationOfNumbers
import java.util.*;
public class CombinationOfNumbers {
public static void main(String[] args) {
// TODO Auto-generated method stub
// a.-5 + 8 * 6
// b. (55+9) % 9
// c. 20 + -3*5 / 8
// d. 5 + 15 / 3 * 2 - 8 % 3
int a=-5;
int b=8;
int c=6;
int d=a+b*c;
System.out.println("a sum is:"+d);
int e=55;
int f=9;
int g=9;
int h=(e+f)%g;
System.out.println("b sum is:"+h);
int i=20;
int j=-3;
int k=5;
int l=8;
int m=i+j*k/l;
System.out.println("c sum is:"+m);
int n=5;
int o=15;
int p=3;
int q=2;
int r=8;
int s=3;
int t=n+o/p*q-r%s;
System.out.println("d sum is:"+t);
}
}
CommonDigitInTwoNumbers
import java.util.Scanner;
public class CommonDigitInTwoNumbers {
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();
System.out.println("the result is:"+CommonNumber(a,b));
}
public static boolean CommonNumber(int a,int b) {
if(a<25 || b>75)
return false;
int x=a%10;
int y=b%10;
a/=10;
b/=10;
return (a==b || a==y || x==b || x==y);
}
}
CompareTwoArrays
import java.util.*;
public class CompareTwoArrays {
public static void main(String[] args) {
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 j=0;j<b;j++) {
arr2[j]=sc.nextInt();
}
int c[]=new int[arr1.length+arr2.length];
int x=0;
for(int i:arr1) {
c[x++]=i;
}
for(int j:arr2) {
c[x++]=j;
}
System.out.println(Arrays.equals(arr1,arr2));
}
}
ConcatinationOfTwoStringExceptRemovingFirstCharacterOfEachString
import java.util.Scanner;
public class ConcatinationOfTwoStringExceptRemovingFirstCharacterOfEachString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=sc.next();
System.out.println("the result is:"+a.substring(1)+" "+b.substring(1));
}
}
ConcatTwoArrays
import java.util.*;
public class ConcatTwoArrays {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int x=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();
}
int c[]=new int[arr1.length+arr2.length];
for(int i:arr1) {
c[x++]=i;
}
for(int j:arr2) {
c[x++]=j;
}
System.out.println(Arrays.toString(c));
}
}
ContainsElement10nextTo10NextTo10orAnElemet20NextTo20ButNotBoth
import java.util.Scanner;
public class ContainsElement10nextTo10NextTo10orAnElemet20NextTo20ButNotBoth {
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();
}
boolean found1010=false;
boolean found2020=false;
for(int i=0;i<arr.length-1;i++) {
if(arr[i]==10 && arr[i+1]==10) {
found1010=true;
}
if(arr[i]==20 && arr[i+1]==20) {
found2020=true;
}
}
System.out.println(String.valueOf(found1010!=found2020));
}
}
ConvertSecondsToHours
import java.util.*;
public class ConvertSecondsToHours {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int S=sc.nextInt();
int M=S/60;
int H=M/60;
System.out.print( H + ":" + M + ":" + S);
}
}
CountLttersSpaceNumbersInString
import java.util.*;
public class CountLttersSpaceNumbersInString {
public static void main(String[] args) {
// TODO Auto-generated method stub
//Scanner sc=new Scanner(System.in);
String a="hello obuli 19CS094 BE CSE.";
count(a);
}
public static void count(String x) {
char c[]=x.toCharArray();
int letter=0;
int num=0;
int space=0;
int oth=0;
for(int i=0;i<x.length();i++) {
if(Character.isLetter(c[i])) {
letter++;
}
else if(Character.isDigit(c[i])) {
num++;
}
else if(Character.isSpaceChar(c[i])) {
space++;
}
else {
oth++;
}
}
//System.out.println(a);
System.out.println("letter:"+letter);
System.out.println("num:"+num);
System.out.println("space:"+space);
System.out.println("others:"+oth);
}
}
CountOddEvenNumberInArray
import java.util.Scanner;
public class CountOddEvenNumberInArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int odd=0;
int even=0;
int arr[]=new int[a];
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
}
for(int i=0;i<arr.length;i++) {
if(arr[i]%2==0) {
even++;
}
else {
odd++;
}
}
System.out.println("number of even numbers in array:"+even);
System.out.println("number of odd numbers in array:"+odd);
// System.out.println("Number of even and odd number in the array:")
}
}
CountTwoElementsOfTwoGiveNArrayOfIntegersWithSameLengthDiffBy1orLess
import java.util.Scanner;
public class CountTwoElementsOfTwoGiveNArrayOfIntegersWithSameLengthDiffBy1orLess {
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();
}
int ctr=0;
for(int i=0;i<arr1.length;i++) {
if(Math.abs(arr1[i]-arr2[i])<=1&&arr1[i]!=arr2[i]) {
ctr++;
}
}
System.out.println("the diffrent of 1 or less number count is:"+ctr);
}
}
CpaitalizeTheFirstLetterOfEachWordInSentence
import java.util.*;
public class CpaitalizeTheFirstLetterOfEachWordInSentence {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.nextLine();
String upp="";
Scanner b=new Scanner(a);
while(b.hasNext()) {
String c=b.next();
upp+=Character.toUpperCase(c.charAt(0))+c.substring(1)+" ";
}
System.out.println(upp.trim());
}
}
CreateArrayOfStringVlaues
import java.util.Arrays;
import java.util.Scanner;
public class CreateArrayOfStringVlaues {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
String arr[]=new String[a];
for(int i=0;i<a;i++) {
arr[i]=String.valueOf(i);
}
System.out.println("the array is:"+Arrays.toString(arr));
}
}
CreateAStringInTheFormOfShortStringLongStringShortString
import java.util.*;
public class CreateAStringInTheFormOfShortStringLongStringShortString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=sc.next();
if(a.length()>=b.length()) {
System.out.println(b+a+b);
}
else {
System.out.println(a+b+a);
}
}
}
CreateNewArrayOfLength2FromeREmainingTwoArrayFirstAndLastValues
import java.util.Arrays;
import java.util.Scanner;
public class CreateNewArrayOfLength2FromeREmainingTwoArrayFirstAndLastValues {
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();
}
int newarr[]= {arr1[0],arr2[2]};
System.out.println("the result is:"+Arrays.toString(newarr));
}
}
CreateNewStringOf4CopiesOfLast3CharactersOfTheOriginalString
import java.util.*;
public class CreateNewStringOf4CopiesOfLast3CharactersOfTheOriginalString {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String a="Python 3.0";
String b=a.substring(a.length()-3);
System.out.println(b+b+b+b);
}
}
DividedBySpecificNumbersThreeandFive
import java.util.*;
public class DividedBySpecificNumbersThreeandFive {
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=0;
for(int i=a;i<=b;i++) {
if(i%3==0) {
c++;
//System.out.print("the 3 divsible number is:"+i+",");
}
}
System.out.println("3 divisble number count is:"+c);
int d=0;
for(int i=a;i<=b;i++) {
if(i%5==0) {
d++;
//System.out.print("the 5 divsible number is:"+i+",");
}
}
System.out.println("the 5 divsible number count is:"+d);
int e=0;
for(int i=a;i<=b;i++) {
if(i%3==0 && i%5==0) {
e++;
//System.out.print("the 3 and 5 divsible number is:"+i+",");
}
}
System.out.println("the 3 and 5 divsible number count is:"+e);
}
}
DivideTwoNumbers
import java.util.*;
public class DivideTwoNumbers {
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();
float c=a/b;
System.out.println(c);
}
}
EvenAndOddNumberInRange
import java.util.*;
public class EvenAndOddNumberInRange {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=0;
int d=0;
for(int i=a;i<=b;i++) {
if(i%2==0) {
c++;
System.out.println("the even number in range is:"+i);
}
else {
d++;
System.out.println("the odd number in range is:"+i);
}
}
System.out.println("the even number count is:"+c);
System.out.println("the odd number count is:"+d);
}
}
EvenNumberDividedBy3
import java.util.*;
public class EvenNumberDividedBy3 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int sum=0;
for(int i=0;i<=a;i++) {
int c=i*3;
if(c%2==0) {
System.out.println(c);
sum++;
}
System.out.println(sum);
}
}
}
FactorialNumber
import java.util.*;
public class FactorialNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
long fac=1;
for(int i=1;i<=a;i++) {
fac=fac*i;
}
System.out.println("the factorial number is:"+fac);
}
}
FactorOfGivenNumber
import java.util.*;
public class FactorOfGivenNumber {
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 result is:"+result(a));
}
public static int result(int b) {
int c=0;
for(int i=1;i<=(int)Math.sqrt(b);i++) {
if(b%i==0 && i*i!=b) {
c+=2;
}
else if(i*i==b) {
c++;
}
}
return c;
}
}
FahrenheitToCelsiusDegree
import java.util.Scanner;
public class FahrenheitToCelsiusDegree {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
double far=sc.nextDouble();
double cel=((5*(far-32.0))/9.0);
System.out.println(far+" degree far is equal to:"+cel+" in celcisu");
}
}
FibonaciSeries
import java.util.Scanner;
public class FibonaciSeries {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int sum=0;
int totalsum=0;
System.out.println(a+" "+b);
for(int i=2;i<=10;i++) {
sum=a+b;
a=b;
b=sum;
totalsum+=sum;
System.out.println(sum);
System.out.println(totalsum);
}
}
}
FindLargest3ElementsInArray
import java.util.*;
public class FindLargest3ElementsInArray {
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();
}
int k=3;
System.out.println("the orginal string is:"+Arrays.toString(arr));
System.out.println(k+" the largest element they said");
Arrays.sort(arr,Collections.reverseOrder());
for(int i=0;i<k;i++) {
System.out.println(arr[i]+" ");
}
}
}
FindModulesOfTwoNumberWithouUsingInBuildMethod
import java.util.*;
public class FindModulesOfTwoNumberWithouUsingInBuildMethod {
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;
int res=a-(c*b);
System.out.println("the module of the given numbers is:"+res);
}
}
FindNumberOfValuesGivenRangeDivisibleByAGivenValue
import java.util.*;
public class FindNumberOfValuesGivenRangeDivisibleByAGivenValue {
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:"+result(a,b,c));
}
public static int result(int a,int b,int c) {
if(a%c==0)
return (b/c-a/c+1);
else
return (b/c-a/c);
}
}
FindSumofElementsInArray
import java.util.*;
public class FindSumofElementsInArray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int arr[]=new int[a];
int sum=0;
for(int i=0;i<a;i++) {
arr[i]=sc.nextInt();
sum+=arr[i];
}
System.out.println("the sum of array is:"+sum);
}
}
FindSystemCurrentTime
public class FindSystemCurrentTime {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.format("\nCurrent Date time: %tc%n\n", System.currentTimeMillis());
}
}
FindTheGivenNumberIsStrobogrammaticNumber
import java.util.Scanner;
import java.util.*;
public class FindTheGivenNumberIsStrobogrammaticNumber {
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 "+a+" strobogramatic number?"+A(a));
}
public static boolean A(String a) {
if(a==null || a.length()==0) {
return true;
}
HashMap<Character,Character>b=new HashMap();
b.put('0', '0');
b.put('1','1');
b.put('8', '8');
b.put('6','9');
b.put('9','6');
int left=0;
int right=a.length()-1;
while(left<=right) {
if(!b.containsKey(a.charAt(right))|| a.charAt(left)!=b.get(a.charAt(right))){
return false;
}
left++;
right--;
}
return true;
}
}
FindTheNumberOnlyOnceAppearInArray
import java.util.Scanner;
public class FindTheNumberOnlyOnceAppearInArray {
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 result;
result=getSingleNumber(arr);
System.out.println("the appear only once is:"+result);
}
public static int getSingleNumber(int array[]) {
if(array==null || array.length==0) {
return -1;
}
int result=0;
for(int i=0;i<array.length;i++) {
result^=array[i];
}
return result;
}
}
FindTheNumberThatGreaterThanAverageOfTheNumberOfSpecifiedArray
import java.util.*;
public class FindTheNumberThatGreaterThanAverageOfTheNumberOfSpecifiedArray {
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();
}
int sum=0;
for(int i=0;i<arr.length;i++) {
sum+=arr[i];
}
double ave=sum/arr.length;
System.out.println("the average is:"+ave);
System.out.println("greater than avaerage value is:");
for(int i=0;i<arr.length;i++) {
if(arr[i]>ave) {
System.out.println(arr[i]);
}
}
}
}
FirstAndLastElementArrayIsSameAndGreaterThan2
import java.util.Scanner;
public class FirstAndLastElementArrayIsSameAndGreaterThan2 {
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 result is:"+(arr.length>=2 && arr[0]==arr[arr.length-1]));
}
}
FirstNonRepeatingCharacter
import java.util.Scanner;
import java.util.*;
public class FirstNonRepeatingCharacter {
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 non repeating character position is:"+A(a));
}
public static int A(String a) {
HashMap<Character,Integer>b=new HashMap();
for(int i=0;i<a.length();i++) {
char chr=a.charAt(i);
b.put(chr,b.containsKey(chr)?b.get(chr)+1:1);
}
for(int i=0;i<a.length();i++) {
if(b.get(a.charAt(i))<2) {
return i;
}
}
return -1;
}
}
FirstOccuranceOfAArray
import java.util.Scanner;
public class FirstOccuranceOfAArray {
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= {2,4,6,7,8};
int b=7;
int lower=0;
int upper=arr.length-1;
int index=-1;
while(lower<=upper) {
int mid=(lower+upper)>>1;
if(arr[mid]==b) {
index=mid;
}
if(arr[mid]>=b) {
upper=mid-1;
}
else {
lower=mid+1;
}
}
System.out.println("position of:"+b+" is "+index);
}
}
FirstOccurancesOfAGivenString
import java.util.Scanner;
public class FirstOccurancesOfAGivenString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=sc.next();
System.out.println("first occurances is:"+a.indexOf(b));
}
}
FizzBuzzAndFizzAndBuzz
import java.util.Scanner;
public class FizzBuzzAndFizzAndBuzz {
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();
for(int i=a;i<=b;i++) {
if(i%3==0 && i%5==0) {
System.out.printf("\n%d:Fizz Buzz",i);
}
else if(i%5==0) {
System.out.printf("\n%d:Buzz",i);
}
else if(i%3==0) {
System.out.printf("\n%d:Fizz",i);
}
}
}
}
GCDSUM
import java.util.*;
public class GCDSUM {
public static int A(int x,int y) {
if(x==0||y==0) {
return 1;
}
if(x<y) {
int t=x;
x=y;
y=t;
}
if(x%y==0) {
return y;
}
else {
return A(y,x%y);
}
}
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();
int d=sc.nextInt();
System.out.println("result is:"+A(a,b));
System.out.println("result is:"+A(c,d));
}
}
GetThePositionOfAGivenPrimeNumber
import java.util.Scanner;
public class GetThePositionOfAGivenPrimeNumber {
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 position of the prime number is:"+A(a));
}
public static int A(int a) {
int arr[]=new int[10000];
int num=3;
int i=0,index=0;
arr[0]=2;
while(num<=a) {
for(i=0;i<=index;i++) {
if(num%arr[i]==0) {
break;
}
}
if(i>index) {
arr[++index]=num;
}
num++;
}
return index+1;
}
}
hashmap
import java.util.*;
public class hashmap {
public static void main(String[] args) {
// HashMap<Integer,String>a=new HashMap();
// a.put(1,"obuli");
// a.put(2,"purusothaman");
// a.put(3,"k");
// System.out.println(a);
HashSet<Integer>a=new HashSet();
a.add(10);
a.add(20);
a.add(30);
a.add(30);
System.out.println(a);
HashSet<Integer>b=new HashSet();
b.addAll(a);
b.add(55);
System.out.println(b);
}
}
InArrayOddNumbersComesFirstEvenNumbersComesAfter
import java.util.Arrays;
import java.util.Scanner;
public class InArrayOddNumbersComesFirstEvenNumbersComesAfter {
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 i=0;
while(i<arr.length && arr[i]%2==0) {
i++;
}
for(int j=i+1;j<arr.length;j++) {
if(arr[j]%2!=0) {
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
}
}
System.out.println("New Array: "+Arrays.toString(arr));
}
}
InsertaWordMiddleOfTheAnotherString
import java.util.*;
public class InsertaWordMiddleOfTheAnotherString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
String a="java 2.0";
String b="Tutorial";
System.out.println("the word is:"+a.substring(0,5)+b+a.substring(4));
}
}
IntegerToString
import java.util.Scanner;
public class IntegerToString {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
String b=Integer.toString(a);
System.out.println("the acutual integer is:"+a);
System.out.println("the string formate is:"+b);
System.out.println("the string oprators:"+b.charAt(0));
}
}
interfaces
import java.util.*;
interface C{
public void add(int a,int b);
}
class D implements C{
public void add(int a,int b) {
System.out.println(a+b);
}
public void mul(int c,int d) {
System.out.println(c*d);
}
}
public class interfaces {
public static void main(String[] args) {
D obj=new D();
obj.add(10,20);
obj.mul(20,30);
}
}
LargestElementBetweenFirstMiddleLastElementInArray
import java.util.Scanner;
public class LargestElementBetweenFirstMiddleLastElementInArray {
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 max=arr[0];
if(max<=arr[arr.length-1])
max=arr[arr.length-1];
if(max<=arr[arr.length/2])
max=arr[arr.length/2];
System.out.println("the max array value is:"+max);
}
}
LargestOf3Numbers
import java.util.*;
public class LargestOf3Numbers {
public static void main(String[] args) {
// //METHOD-1
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
int b=sc.nextInt();
int c=sc.nextInt();
// if(a>b && a>c) {
// System.out.println("the largest number is:"+a);
// }
// else if(b>a && b>c) {
// System.out.println("the largest number is:"+b);
// }
// else if(c>a && c>b) {
// System.out.println("the largest number is:"+c);
// }
// else {
// System.out.println("thank you");
// }
//METHOD-2
int Y=c>(a>b ? a:b)?c:(a>b ? a:b);
System.out.println("the largest number is:"+Y);
}
}
LeapYear
import java.util.*;
public class LeapYear {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int a=sc.nextInt();
if((a%4==0 && a%100!=0)||(a%400==0)) {
System.out.println("the year is leap year:"+a);
}
else {
System.out.println("the year is not a leap year:"+a);
}
}
}
LeftShiftOfAnArray
import java.util.Arrays;
import java.util.Scanner;
public class LeftShiftOfAnArray {
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();
}
if(arr.length>1) {
int first=arr[0];
for(int i=1;i<arr.length;i++) {
arr[i-1]=arr[i];
arr[arr.length-1]=first;
}
}
System.out.println("new array left shift is:"+Arrays.toString(arr));
}
}
LocalStaticInstanceVariables
import java.util.*;
class All{
public int a;//instance variable
All(){
this.a=10;
}
}
class B{
static int b=30;//static variable
void run() {
System.out.println("the static variable is:"+b);
}
}
public class LocalStaticInstanceVariables {
public static void main(String[] args) {
All obj=new All();
System.out.println("the instance variable is:"+obj.a);
int c=20;//Local Variable
System.out.println("the local variable is:"+c);
B obj2=new B();
obj2.run();
}
}
LowerToUpperCase
import java.util.*;
public class LowerToUpperCase {
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 lowerscase of string is:"+a);
a=a.toUpperCase();
System.out.println("the upperscase of string is:"+a);
}
}