├── .gitignore ├── Collections ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── com │ └── cdac │ └── collections │ ├── bean │ └── Student.java │ ├── collectionsutils │ └── CollectionsDemo.java │ ├── list │ ├── ArrayListDemo.java │ ├── LinkedListDemo.java │ └── VectorDemo.java │ ├── map │ └── HashMapDemo.java │ ├── set │ ├── HashSetDemo.java │ ├── LinkedHashSetDemo.java │ ├── MarksComparator.java │ ├── NameComparator.java │ ├── StringLengthComparator.java │ ├── StringSetDemo.java │ ├── StudentsTreeSet.java │ └── TreeSetDemo.java │ └── threadsafe │ ├── EmployeeRunnable.java │ ├── MainProgram.java │ ├── ShallowCopy_DeepCopy.java │ └── StudentRunnable.java ├── EncryptionDecryption ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ ├── .gitignore │ └── com │ │ └── cdac │ │ └── encrypt │ │ ├── aes │ │ └── AESEncryption.class │ │ └── file │ │ └── EncryptFile.class └── src │ ├── com │ └── cdac │ │ └── encrypt │ │ ├── aes │ │ └── AESEncryption.java │ │ └── file │ │ └── EncryptFile.java │ └── snippet │ └── Snippet.java ├── ExceptionHandling ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ └── com │ │ └── cdac │ │ ├── CheckedExceptions.class │ │ ├── ExceptionChaining.class │ │ ├── ExceptionWithReturn.class │ │ ├── FileHandlingException.class │ │ ├── UnCheckedExceptions.class │ │ ├── customexception │ │ ├── Student.class │ │ ├── StudentDB.class │ │ ├── StudentMain.class │ │ └── StudentNotFoundException.class │ │ └── errors │ │ ├── OutOfMemoryError.class │ │ └── StackOverflowError.class └── src │ └── com │ └── cdac │ ├── CheckedExceptions.java │ ├── ExceptionChaining.java │ ├── ExceptionWithReturn.java │ ├── FileHandlingException.java │ ├── UnCheckedExceptions.java │ ├── customexception │ ├── Student.java │ ├── StudentDB.java │ ├── StudentMain.java │ └── StudentNotFoundException.java │ └── errors │ ├── OutOfMemoryError.java │ └── StackOverflowError.java ├── FileIO ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── com │ └── cdac │ ├── fileio │ └── bytestream │ │ ├── ByteStream.java │ │ ├── CharacterStream.java │ │ ├── Employee.java │ │ ├── SerializationDemo.java │ │ └── TestErroStream.java │ └── nio │ └── NioBasicDemo.java ├── Generics ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── com │ └── cdac │ └── generics │ ├── Database.java │ ├── GenericCollectionDisplay.java │ ├── GenericCollectionDisplayMain.java │ ├── NumberDatabase.java │ ├── NumberDatabaseMain.java │ └── StringDB.java ├── InterviewPrograms ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── com │ ├── algorithm │ ├── search │ │ ├── BinarySearch.java │ │ └── LinearSearch.java │ └── sorting │ │ ├── BubbleSort.java │ │ ├── InsertionSort.java │ │ ├── MergeSort.java │ │ ├── QuickSort.java │ │ └── SelectionSort.java │ ├── hackerrank │ ├── AllCombinationsOfBrackets.java │ └── BalancedBrackets.java │ ├── java │ ├── array │ │ ├── ArrayRotation.java │ │ ├── Average.java │ │ ├── FindAllDuplicates.java │ │ ├── FindMissingNo.java │ │ ├── FindMostFrequent.java │ │ ├── FindMostFrequentUsingHashtable.java │ │ ├── FindMostFrequentUsingSorting.java │ │ ├── FindPairForZ.java │ │ ├── FindUniqueElt.java │ │ ├── FindingAllSubsets.java │ │ ├── IntersectionOfSortedArrays.java │ │ ├── IntersectionOfTwoArrays.java │ │ ├── MoveAllZerosToEnd.java │ │ ├── MoveAllZerosToFront.java │ │ ├── ReverseArray.java │ │ ├── SecondMaximum.java │ │ ├── SecondMinimum.java │ │ ├── UnionArrayUsingHashSet.java │ │ ├── UnionOfSortedArrays.java │ │ └── UnionOfTwoArrays.java │ ├── basic │ │ ├── AddWithoutPlus.java │ │ ├── DigitsOfNumber.java │ │ ├── Divisors.java │ │ ├── EvenOrOdd.java │ │ ├── Factorial.java │ │ ├── FactorialUsingRecursion.java │ │ ├── FactorsOfANumber.java │ │ ├── FindTheSmallest.java │ │ ├── LeapYear.java │ │ ├── PositiveOrNegative.java │ │ ├── Print10WithoutLoop.java │ │ ├── SwapApproach1.java │ │ ├── SwapApproach2.java │ │ └── SwapApproach3.java │ ├── coin │ │ └── change │ │ │ ├── MaxNoOfWays.java │ │ │ └── MinimumCoins.java │ ├── convertor │ │ ├── BinaryToDecimal.java │ │ ├── BinaryToOctal.java │ │ ├── DecimalToBinary.java │ │ ├── DecimalToHexadecimal.java │ │ ├── DecimalToOctal.java │ │ ├── HexaToDecimal.java │ │ ├── OctalToBinary.java │ │ └── OctalToDecimal.java │ ├── matrix │ │ ├── IdentityMatrix.java │ │ ├── LowerTriangularMatrix.java │ │ ├── MatrixAddition.java │ │ ├── MatrixInSpiralForm.java │ │ ├── MatrixMultiplication.java │ │ ├── MatrixSubtraction.java │ │ ├── MatrixTranspose.java │ │ ├── RowSumColumnSum.java │ │ ├── SparseMatrix.java │ │ └── UpperTriangularMatrix.java │ ├── numbers │ │ ├── AmicablePair.java │ │ ├── ArmstrongNumber.java │ │ ├── ArmstrongNumberList.java │ │ ├── AutomorphicNumber.java │ │ ├── AutomorphicNumberList.java │ │ ├── FindTheSmallest.java │ │ ├── GCD.java │ │ ├── KaprekarNumber.java │ │ ├── KaprekarNumberList.java │ │ ├── LCM.java │ │ ├── LCMUsingFactors.java │ │ ├── LargestDivisor.java │ │ ├── PalindromeCheck.java │ │ ├── PerfectNumber.java │ │ ├── PerfectNumberList.java │ │ ├── PrimeFactors.java │ │ ├── PrimeNumber.java │ │ ├── PrimeNumberList.java │ │ ├── RamanujamNumber.java │ │ ├── ReverseTheNumber.java │ │ ├── SmithNumber.java │ │ ├── SmithNumberList.java │ │ └── SumOfDigits.java │ ├── parentheses │ │ ├── BalancedParentheses.java │ │ └── GenerateBalancedParentheses.java │ ├── patterns │ │ ├── FloydsTriangle.java │ │ ├── Pattern1.java │ │ ├── Pattern10.java │ │ ├── Pattern11.java │ │ ├── Pattern12.java │ │ ├── Pattern13.java │ │ ├── Pattern14.java │ │ ├── Pattern15.java │ │ ├── Pattern16.java │ │ ├── Pattern17.java │ │ ├── Pattern18.java │ │ ├── Pattern19.java │ │ ├── Pattern2.java │ │ ├── Pattern20.java │ │ ├── Pattern21.java │ │ ├── Pattern22.java │ │ ├── Pattern23.java │ │ ├── Pattern24.java │ │ ├── Pattern25.java │ │ ├── Pattern26.java │ │ ├── Pattern27.java │ │ ├── Pattern28.java │ │ ├── Pattern29.java │ │ ├── Pattern3.java │ │ ├── Pattern30.java │ │ ├── Pattern31.java │ │ ├── Pattern32.java │ │ ├── Pattern33.java │ │ ├── Pattern34.java │ │ ├── Pattern35.java │ │ ├── Pattern36.java │ │ ├── Pattern37.java │ │ ├── Pattern38.java │ │ ├── Pattern39.java │ │ ├── Pattern4.java │ │ ├── Pattern40.java │ │ ├── Pattern41.java │ │ ├── Pattern42.java │ │ ├── Pattern43.java │ │ ├── Pattern44.java │ │ ├── Pattern45.java │ │ ├── Pattern46.java │ │ ├── Pattern47.java │ │ ├── Pattern48.java │ │ ├── Pattern49.java │ │ ├── Pattern5.java │ │ ├── Pattern50.java │ │ ├── Pattern51.java │ │ ├── Pattern52.java │ │ ├── Pattern53.java │ │ ├── Pattern54.java │ │ ├── Pattern55.java │ │ ├── Pattern6.java │ │ ├── Pattern7.java │ │ ├── Pattern8.java │ │ └── Pattern9.java │ ├── series │ │ ├── AlternateTenSeries.java │ │ ├── ArithmeticProgression.java │ │ ├── EvenNumberSeries.java │ │ ├── FibonacciSeries.java │ │ ├── GeometricProgression.java │ │ ├── OddNumberSeries.java │ │ ├── PatternSeries.java │ │ ├── PowersOf2.java │ │ ├── PrimeNumberSeries.java │ │ ├── QuadraticSequence.java │ │ ├── Series1.java │ │ ├── Series5.java │ │ └── TriangularSeries.java │ ├── singleton │ │ ├── Account.java │ │ └── SingletonMain.java │ └── strings │ │ ├── AllSubsetsUsingBitwise.java │ │ ├── AllSubsetsUsingRecursion.java │ │ ├── Anagram.java │ │ ├── CombinationOfString.java │ │ ├── CountVowels.java │ │ ├── CountWords.java │ │ ├── CountWords2.java │ │ ├── MostRepeatedChar.java │ │ ├── Palindrome.java │ │ ├── PermutationOfString.java │ │ ├── ReverseString.java │ │ ├── StringRotationOfAnother.java │ │ └── SwapTwoStrings.java │ └── techgig │ └── java │ └── AddWithoutUsingPlus.java ├── JavaEnums ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ └── com │ │ └── cdac │ │ └── java │ │ └── eum │ │ ├── Directions.class │ │ ├── Level.class │ │ ├── MainProgram.class │ │ └── TrafficSignal.class └── src │ └── com │ └── cdac │ └── java │ └── eum │ ├── Directions.java │ ├── Level.java │ ├── MainProgram.java │ └── TrafficSignal.java ├── Networking ├── .classpath ├── .gitignore ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs └── src │ └── com │ └── cdac │ └── networking │ ├── NetworkInfo.java │ ├── TCPClient.java │ └── TCPServer.java ├── README.md ├── Reflections ├── .classpath ├── .project ├── .settings │ └── org.eclipse.jdt.core.prefs ├── bin │ └── com │ │ └── cdac │ │ ├── Account.class │ │ └── ReflectionsDemo.class └── src │ └── com │ └── cdac │ ├── Account.java │ └── ReflectionsDemo.java └── Threads ├── .classpath ├── .gitignore ├── .project ├── .settings └── org.eclipse.jdt.core.prefs └── src └── com └── cdac ├── thread └── atmcenter │ ├── ATMCenter.java │ ├── MainProgram.java │ └── MoneyWithdrawRunnable.java └── threads ├── GoodEveningThread.java ├── GoodMorningThread.java ├── JoinDemo.java ├── ThreadMain.java ├── deadlock ├── DeadLock1.java ├── DeadLock2.java └── DeadLockRunnable.java ├── sorting └── QuickSortMultiThreaded.java ├── synchronization ├── ConsumerRunnable.java ├── Factory.java ├── ProducerConsumeMain.java └── ProducerRunnable.java └── synchronization2 ├── MessageSender.java ├── MessageSenderMain.java └── SenderRunnable.java /.gitignore: -------------------------------------------------------------------------------- 1 | /.metadata/ 2 | -------------------------------------------------------------------------------- /Collections/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Collections/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Collections/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Collections 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Collections/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/bean/Student.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.bean; 2 | 3 | public class Student implements Comparable,Cloneable{ 4 | 5 | private int rollNo; 6 | private String name; 7 | private int marks; 8 | 9 | public Student(){ 10 | rollNo = -1; 11 | name = ""; 12 | marks = 0; 13 | } 14 | 15 | public Student(int rollNo, String name,int marks) { 16 | this.rollNo = rollNo; 17 | this.name = name; 18 | this.marks = marks; 19 | } 20 | 21 | public int getRollNo() { 22 | return rollNo; 23 | } 24 | 25 | public void setRollNo(int rollNo) { 26 | this.rollNo = rollNo; 27 | } 28 | 29 | public String getName() { 30 | return name; 31 | } 32 | 33 | public void setName(String name) { 34 | this.name = name; 35 | } 36 | 37 | public int getMarks() { 38 | return marks; 39 | } 40 | 41 | public void setMarks(int marks) { 42 | this.marks = marks; 43 | } 44 | 45 | @Override 46 | public String toString() { 47 | return rollNo+" "+name+" "+marks; 48 | } 49 | 50 | @Override 51 | public int compareTo(Student o) { 52 | return this.rollNo - o.rollNo; 53 | //DECREASING ORDER BY ROLL NO 54 | // return o.rollNo - this.rollNo ; 55 | } 56 | 57 | @Override 58 | public Object clone() throws CloneNotSupportedException { 59 | return super.clone(); 60 | } 61 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/list/ArrayListDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.list; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Iterator; 5 | 6 | public class ArrayListDemo { 7 | 8 | public static void main(String[] args) { 9 | ArrayList osList = new ArrayList<>(); 10 | 11 | //ADD 12 | osList.add("Android"); 13 | osList.add("iOS"); 14 | osList.add("Windows"); 15 | 16 | //ADD ELEMENT AT INDEX 17 | osList.add(0, "Linux"); 18 | 19 | //GET THE SIZE OF ARRAYLIST 20 | int size = osList.size(); 21 | System.out.println("Size of the list is : "+size); 22 | 23 | //DISPLAY THE ELEMENTS USING 'FOR' LOOP 24 | for(int i=0;i iterator = osList.iterator(); 29 | while( iterator.hasNext() ){ 30 | String countryName = iterator.next(); 31 | System.out.println(countryName); 32 | } 33 | 34 | //REMOVE ELEMENT BY INDEX 35 | osList.remove(0); 36 | 37 | //REMOVE BY OBJECT 38 | osList.remove("ABCD"); 39 | 40 | //CHECK LIST IS EMPTY OR NOT 41 | boolean emptyState = osList.isEmpty(); 42 | 43 | //UPDATE THE VALUE 44 | osList.set(0, "Android"); 45 | 46 | //to check whether an item is available in the list 47 | System.out.println("Does list contains Android :: "+osList.contains("iOS")); 48 | 49 | //to get the index of an item 50 | System.out.println("Index of Android :: "+osList.indexOf("Android")); 51 | System.out.println("Last Index of Android : "+osList.lastIndexOf("Android")); 52 | 53 | System.out.println(osList.toString()); 54 | } 55 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/list/VectorDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.list; 2 | 3 | import java.util.Enumeration; 4 | import java.util.Vector; 5 | 6 | public class VectorDemo { 7 | 8 | public static void main(String[] args) { 9 | Vector vector = new Vector<>(); 10 | 11 | //ADD 12 | vector.add("ONE"); 13 | vector.add("ONE"); 14 | vector.addElement("ONE"); 15 | 16 | //ADD ELEMENT AT INDEX 17 | vector.add(1, "ONE"); 18 | 19 | //GET OR ITERATE 20 | String value = vector.get(1); 21 | String value2 = vector.elementAt(2); 22 | 23 | //DISPLAY THE ELEMENTS USING ITERATOR 24 | Enumeration enumeration = vector.elements(); 25 | while ( enumeration.hasMoreElements() ){ 26 | String s = enumeration.nextElement(); 27 | System.out.println(s); 28 | } 29 | 30 | //UPDATE OR SET 31 | vector.setElementAt("Hello", 0); 32 | vector.set(0, "Hello"); 33 | 34 | //REMOVE 35 | vector.removeElementAt(1); 36 | } 37 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/set/HashSetDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.set; 2 | 3 | import java.util.HashSet; 4 | import java.util.Iterator; 5 | import com.cdac.collections.bean.Student; 6 | 7 | public class HashSetDemo { 8 | 9 | public static void main(String[] args) { 10 | HashSet nosList = new HashSet<>(); 11 | //ADD 12 | nosList.add(10); 13 | nosList.add(20); 14 | nosList.add(30); 15 | nosList.add(null); 16 | 17 | //Adding duplicate element will be ignored 18 | nosList.add(40); 19 | 20 | //SIZE AND EMPTY 21 | int size = nosList.size(); 22 | boolean emptyState = nosList.isEmpty(); 23 | 24 | //CHECK AVAILABILITY OF AN ELEMENT 25 | boolean contains = nosList.contains(40); 26 | 27 | //Remove element by Object 28 | boolean isRemoved = nosList.remove(10); 29 | 30 | //Remove all elements 31 | nosList.clear(); 32 | 33 | //Read all elements using Iterator 34 | Iterator iterator = nosList.iterator(); 35 | while(iterator.hasNext()){ 36 | int element = iterator.next(); 37 | System.out.println(element); 38 | } 39 | } 40 | 41 | public static void studentsSet(){ 42 | HashSet students = new HashSet<>(); 43 | Student s1 = new Student(101,"Sathish",350); 44 | Student s2 = new Student(102, "Atul",450); 45 | Student s3 = new Student(101,"Sathish",350); 46 | 47 | //s1 and s3 are different, because s1 and s3 are having different hashCode() values 48 | students.add(s1); 49 | students.add(s2); 50 | students.add(s3); 51 | 52 | System.out.println("Student Set size : "+students.size()); 53 | //override hascode method to overcome the abvoe problem 54 | } 55 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/set/LinkedHashSetDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.set; 2 | 3 | import java.util.Iterator; 4 | import java.util.LinkedHashSet; 5 | 6 | public class LinkedHashSetDemo { 7 | 8 | public static void main(String[] args) { 9 | 10 | LinkedHashSet set = new LinkedHashSet<>(); 11 | 12 | //ADD ELEMENTS 13 | set.add("Android"); 14 | set.add("Android"); 15 | set.add("Windows"); 16 | 17 | //SIZE AND ISEMPTY 18 | int size = set.size(); 19 | boolean emptyState = set.isEmpty(); 20 | 21 | //CHECK AVAILABILITY OF AN ELEMENT 22 | boolean isAvailable = set.contains("iOS"); 23 | 24 | //GET AND RETRIEVAL 25 | Iterator iterator = set.iterator(); 26 | while(iterator.hasNext()){ 27 | String element = iterator.next(); 28 | System.out.println(element); 29 | } 30 | 31 | //REMOVE THE GIVEN ELEMENT 32 | boolean isRemoved = set.remove("Android"); 33 | 34 | //REMOVE ALL THE ELEMENTS 35 | set.clear(); 36 | } 37 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/set/MarksComparator.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.set; 2 | 3 | import java.util.Comparator; 4 | import com.cdac.collections.bean.Student; 5 | 6 | public class MarksComparator implements Comparator{ 7 | @Override 8 | public int compare(Student o1, Student o2) { 9 | if(o1.getMarks() > o2.getMarks()) 10 | return 1; 11 | else if(o1.getMarks() < o2.getMarks()) 12 | return -1; 13 | return 0; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/set/NameComparator.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.set; 2 | 3 | import java.util.Comparator; 4 | import com.cdac.collections.bean.Student; 5 | 6 | public class NameComparator implements Comparator{ 7 | @Override 8 | public int compare(Student o1, Student o2) { 9 | //calling comparable implementation of String class 10 | return o1.getName().compareTo(o2.getName()); 11 | } 12 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/set/StringLengthComparator.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.set; 2 | 3 | import java.util.Comparator; 4 | 5 | //This comparator, compares and sorts the string based on the LENGTH 6 | //of the string. 7 | public class StringLengthComparator implements Comparator{ 8 | @Override 9 | public int compare(String o1, String o2) { 10 | if(o1.length() > o2.length()) 11 | return 1; 12 | if(o1.length() < o2.length()) 13 | return -1; 14 | return 0; 15 | } 16 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/set/StringSetDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.set; 2 | 3 | import java.util.Iterator; 4 | import java.util.TreeSet; 5 | 6 | public class StringSetDemo { 7 | 8 | public static void main(String[] args) { 9 | StringLengthComparator comparator = new StringLengthComparator(); 10 | TreeSet stringSet = new TreeSet<>(comparator); 11 | 12 | stringSet.add("one"); 13 | stringSet.add("four"); 14 | stringSet.add("hundred"); 15 | stringSet.add("c"); 16 | stringSet.add("seven"); 17 | 18 | Iterator iterator = stringSet.iterator(); 19 | while( iterator.hasNext() ) 20 | System.out.println(iterator.next()); 21 | 22 | } 23 | 24 | } 25 | /* 26 | OUTPUT 27 | 28 | c 29 | one 30 | four 31 | seven 32 | hundred 33 | 34 | */ -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/threadsafe/EmployeeRunnable.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.threadsafe; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class EmployeeRunnable implements Runnable{ 6 | 7 | ArrayList names; 8 | 9 | public EmployeeRunnable(ArrayList list) { 10 | this.names = list; 11 | } 12 | 13 | @Override 14 | public void run() { 15 | for(int i=0;i<100;i++) 16 | names.add("Employee :: "+i); 17 | } 18 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/threadsafe/MainProgram.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.threadsafe; 2 | 3 | import java.util.ArrayList; 4 | 5 | import com.cdac.collections.bean.Student; 6 | 7 | public class MainProgram { 8 | public static void main(String[] args) throws CloneNotSupportedException { 9 | ArrayList list = new ArrayList<>(); 10 | EmployeeRunnable employeeRunnable = new EmployeeRunnable(list); 11 | StudentRunnable studentRunnable = new StudentRunnable(list); 12 | 13 | Thread t1 = new Thread(employeeRunnable); 14 | Thread t2 = new Thread(studentRunnable); 15 | 16 | t1.start(); 17 | t2.start(); 18 | 19 | 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/threadsafe/ShallowCopy_DeepCopy.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.threadsafe; 2 | 3 | import com.cdac.collections.bean.Student; 4 | 5 | public class ShallowCopy_DeepCopy { 6 | public static void main(String[] args) throws CloneNotSupportedException { 7 | //Shallow copy 8 | Student s1 = new Student(101, "Atul", 80); 9 | Student s2 = s1; 10 | s2.setMarks(85); 11 | System.out.println("Shallow Copy"); 12 | System.out.println(s2.getMarks()+" "+s1.getMarks()); 13 | 14 | Student s3 = (Student) s1.clone(); 15 | s3.setMarks(90); 16 | System.out.println("Deep Copy"); 17 | System.out.println(s3.getMarks() +" "+s1.getMarks()); 18 | } 19 | } -------------------------------------------------------------------------------- /Collections/src/com/cdac/collections/threadsafe/StudentRunnable.java: -------------------------------------------------------------------------------- 1 | package com.cdac.collections.threadsafe; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class StudentRunnable implements Runnable{ 6 | 7 | ArrayList names; 8 | 9 | public StudentRunnable(ArrayList list) { 10 | this.names = list; 11 | } 12 | @Override 13 | public void run() { 14 | for(int i=100;i<200;i++) 15 | names.add("Student :: "+i); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /EncryptionDecryption/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /EncryptionDecryption/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | EncryptionDecryption 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /EncryptionDecryption/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /EncryptionDecryption/bin/.gitignore: -------------------------------------------------------------------------------- 1 | /snippet/ 2 | -------------------------------------------------------------------------------- /EncryptionDecryption/bin/com/cdac/encrypt/aes/AESEncryption.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/EncryptionDecryption/bin/com/cdac/encrypt/aes/AESEncryption.class -------------------------------------------------------------------------------- /EncryptionDecryption/bin/com/cdac/encrypt/file/EncryptFile.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/EncryptionDecryption/bin/com/cdac/encrypt/file/EncryptFile.class -------------------------------------------------------------------------------- /EncryptionDecryption/src/snippet/Snippet.java: -------------------------------------------------------------------------------- 1 | package snippet; 2 | 3 | public class Snippet { 4 | Divisors are 5 | } 6 | 7 | -------------------------------------------------------------------------------- /ExceptionHandling/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /ExceptionHandling/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | ExceptionHandling 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /ExceptionHandling/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/CheckedExceptions.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/CheckedExceptions.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/ExceptionChaining.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/ExceptionChaining.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/ExceptionWithReturn.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/ExceptionWithReturn.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/FileHandlingException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/FileHandlingException.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/UnCheckedExceptions.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/UnCheckedExceptions.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/customexception/Student.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/customexception/Student.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/customexception/StudentDB.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/customexception/StudentDB.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/customexception/StudentMain.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/customexception/StudentMain.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/customexception/StudentNotFoundException.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/customexception/StudentNotFoundException.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/errors/OutOfMemoryError.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/errors/OutOfMemoryError.class -------------------------------------------------------------------------------- /ExceptionHandling/bin/com/cdac/errors/StackOverflowError.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/ExceptionHandling/bin/com/cdac/errors/StackOverflowError.class -------------------------------------------------------------------------------- /ExceptionHandling/src/com/cdac/CheckedExceptions.java: -------------------------------------------------------------------------------- 1 | package com.cdac; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.FileReader; 5 | 6 | public class CheckedExceptions { 7 | 8 | public static void main(String[] args) throws FileNotFoundException { 9 | //this exception checked at compile time 10 | //handle this with try catch to compile the program 11 | FileReader filereader = new FileReader("FilePath"); 12 | 13 | 14 | 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /ExceptionHandling/src/com/cdac/ExceptionChaining.java: -------------------------------------------------------------------------------- 1 | package com.cdac; 2 | 3 | /* 4 | * Exception Hierarchy Chaining 5 | * multiple catch block in Hierarchy order 6 | */ 7 | public class ExceptionChaining { 8 | 9 | public static void main(String[] args) { 10 | try{ 11 | //test case 1 - Arithmetic Exception 12 | int i = 10 / 0; 13 | 14 | //comment the above code and run this code 15 | //test case 2 - Runtime exception with return statement 16 | String s =null; 17 | s.length(); 18 | }catch(ArithmeticException ae){ 19 | System.out.println("Arithmetic Exception"); 20 | ae.printStackTrace(); 21 | }catch(RuntimeException re){ 22 | System.out.println("Runtime Exception"); 23 | re.printStackTrace(); 24 | //this return statement transfer the control to calling method 25 | //because this return, code statement after try block will not execute 26 | return; 27 | }catch(Exception e){ 28 | System.out.println("Exception"); 29 | e.printStackTrace(); 30 | } 31 | finally{ 32 | //this block will execute in both cases 33 | //with exception or without exception occurs 34 | System.out.println("Finally block"); 35 | } 36 | System.out.println("Outside of try block"); 37 | } 38 | } -------------------------------------------------------------------------------- /ExceptionHandling/src/com/cdac/ExceptionWithReturn.java: -------------------------------------------------------------------------------- 1 | package com.cdac; 2 | 3 | /* 4 | * This method will throw the exception 5 | * But the method returns the value also. 6 | */ 7 | 8 | public class ExceptionWithReturn { 9 | 10 | public static void main(String[] args) { 11 | boolean result = nullCheck(null); 12 | System.out.println("Result is :: "+result); 13 | } 14 | 15 | public static boolean nullCheck(String s){ 16 | try{ 17 | if(s == null) 18 | throw new ArithmeticException(); 19 | System.out.println("After exception"); 20 | }catch(ArithmeticException e){ 21 | e.printStackTrace(); 22 | return false; 23 | } 24 | finally{ 25 | System.out.println("Finally Block"); 26 | } 27 | return true; 28 | } 29 | 30 | } -------------------------------------------------------------------------------- /ExceptionHandling/src/com/cdac/FileHandlingException.java: -------------------------------------------------------------------------------- 1 | package com.cdac; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileReader; 7 | import java.io.IOException; 8 | 9 | public class FileHandlingException { 10 | 11 | public static void main(String[] args) { 12 | try { 13 | File file = new File("D:\\abc.txt"); 14 | BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); 15 | bufferedReader.readLine(); 16 | bufferedReader.close(); 17 | System.out.println("End of the program!"); 18 | } catch (FileNotFoundException e) { 19 | //when the file is not available in the path 20 | //this exception will be thrown 21 | e.printStackTrace(); 22 | } catch (IOException e) { 23 | //when there is an issue in input/output of the file 24 | //this exception will be thrown 25 | e.printStackTrace(); 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /ExceptionHandling/src/com/cdac/UnCheckedExceptions.java: -------------------------------------------------------------------------------- 1 | package com.cdac; 2 | 3 | public class UnCheckedExceptions { 4 | 5 | public static void main(String[] args) { 6 | numberFormatException(); 7 | divideByZero(); 8 | nullPointerException(); 9 | stringIndexOutOfBoundException(); 10 | classCastException(); 11 | } 12 | 13 | public static void numberFormatException(){ 14 | String str = "ten"; 15 | 16 | //this code will throw NumberFormatException 17 | //this is runtime exception 18 | //this exception checked at runtime only 19 | //use try catch to handle this exception 20 | int i = Integer.parseInt(str); 21 | } 22 | 23 | public static void divideByZero(){ 24 | //the below code will throw ArithmeticException 25 | //divide by 0 error 26 | //use try catch to handle this exception 27 | int a = 10/0; 28 | } 29 | 30 | public static void nullPointerException(){ 31 | String firstName = null; 32 | int length = firstName.length(); 33 | System.out.println("Length of the first name is : "+length); 34 | } 35 | 36 | public static void stringIndexOutOfBoundException(){ 37 | String name = "Core Java"; 38 | char charAtIndex100 = name.charAt(100); 39 | System.out.println("Char at index 100 is : "+charAtIndex100); 40 | } 41 | 42 | public static void classCastException(){ 43 | Object o = new Integer(10); 44 | String sObj = (String) o; 45 | System.out.println(sObj); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /ExceptionHandling/src/com/cdac/customexception/Student.java: -------------------------------------------------------------------------------- 1 | package com.cdac.customexception; 2 | 3 | public class Student implements Comparable{ 4 | 5 | private int rollNo; 6 | private String name; 7 | 8 | public Student(){ 9 | rollNo = -1; 10 | name = ""; 11 | } 12 | 13 | public Student(int rollNo, String name) { 14 | super(); 15 | this.rollNo = rollNo; 16 | this.name = name; 17 | } 18 | 19 | public int getRollNo() { 20 | return rollNo; 21 | } 22 | 23 | public void setRollNo(int rollNo) { 24 | this.rollNo = rollNo; 25 | } 26 | 27 | public String getName() { 28 | return name; 29 | } 30 | 31 | public void setName(String name) { 32 | this.name = name; 33 | } 34 | 35 | @Override 36 | public String toString() { 37 | return rollNo+" "+name; 38 | } 39 | 40 | @Override 41 | public int compareTo(Student o) { 42 | return this.rollNo - o.rollNo; 43 | 44 | //DECREASING ORDER BY ROLL NO 45 | // return o.rollNo - this.rollNo ; 46 | } 47 | } -------------------------------------------------------------------------------- /ExceptionHandling/src/com/cdac/customexception/StudentDB.java: -------------------------------------------------------------------------------- 1 | package com.cdac.customexception; 2 | 3 | public class StudentDB { 4 | Student[] students; 5 | 6 | public StudentDB() { 7 | students = new Student[3]; 8 | Student s1 = new Student(1,"Name1"); 9 | Student s2 = new Student(2,"Name2"); 10 | Student s3 = new Student(3,"Name3"); 11 | students[0] = s1; 12 | students[1] = s2; 13 | students[2] = s3; 14 | } 15 | 16 | public Student getStudent(int rollNo) throws StudentNotFoundException{ 17 | for(int i=0;i 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /FileIO/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /FileIO/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | FileIO 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /FileIO/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /FileIO/src/com/cdac/fileio/bytestream/ByteStream.java: -------------------------------------------------------------------------------- 1 | package com.cdac.fileio.bytestream; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.FileNotFoundException; 5 | import java.io.FileOutputStream; 6 | import java.io.IOException; 7 | 8 | public class ByteStream { 9 | 10 | public static void main(String[] args) throws IOException { 11 | String content = "Welcome to DAC Course!"; 12 | writeToFile(content); 13 | readFromFile(); 14 | } 15 | 16 | public static void writeToFile(String content){ 17 | String filePath = "D:\\byteStream.txt"; 18 | FileOutputStream fos = null; 19 | try { 20 | fos = new FileOutputStream(filePath); 21 | fos.write(content.getBytes()); 22 | } catch (FileNotFoundException e) { 23 | e.printStackTrace(); 24 | } catch (IOException e) { 25 | e.printStackTrace(); 26 | } 27 | finally{ 28 | try { 29 | fos.close(); 30 | } catch (IOException e) { 31 | e.printStackTrace(); 32 | } 33 | } 34 | } 35 | 36 | public static void readFromFile(){ 37 | String filePath = "D:\\byteStream.txt"; 38 | FileInputStream fis = null; 39 | try { 40 | fis = new FileInputStream(filePath); 41 | byte[] buffer = new byte[fis.available()]; 42 | fis.read(buffer); 43 | String fileContent = new String(buffer); 44 | System.out.println("File Content is : "+fileContent); 45 | } catch (FileNotFoundException e) { 46 | e.printStackTrace(); 47 | } catch (IOException e) { 48 | e.printStackTrace(); 49 | } finally { 50 | try { 51 | fis.close(); 52 | } catch (IOException e) { 53 | e.printStackTrace(); 54 | } 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /FileIO/src/com/cdac/fileio/bytestream/CharacterStream.java: -------------------------------------------------------------------------------- 1 | package com.cdac.fileio.bytestream; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.FileReader; 5 | import java.io.FileWriter; 6 | import java.io.IOException; 7 | import java.io.PrintWriter; 8 | import java.nio.CharBuffer; 9 | 10 | public class CharacterStream { 11 | 12 | public static void main(String[] args) throws IOException { 13 | String content = "Hi -1 Good morning!"; 14 | writeToFile(content); 15 | readFromFile(); 16 | } 17 | 18 | public static void writeToFile(String content){ 19 | String filePath = "D:\\char-based-stream.txt"; 20 | FileWriter fileWriter = null; 21 | try { 22 | fileWriter = new FileWriter(filePath); 23 | fileWriter.write(content); 24 | } catch (IOException e) { 25 | e.printStackTrace(); 26 | }finally{ 27 | try { 28 | fileWriter.close(); 29 | } catch (IOException e) { 30 | e.printStackTrace(); 31 | } 32 | } 33 | } 34 | 35 | public static void readFromFile(){ 36 | String filePath = "D:\\char-based-stream.txt"; 37 | FileReader fileReader = null; 38 | try { 39 | fileReader = new FileReader(filePath); 40 | int i; 41 | while ( (i = fileReader.read()) != -1){ 42 | char ch = (char) i; 43 | System.out.print(ch); 44 | } 45 | 46 | } catch (FileNotFoundException e) { 47 | e.printStackTrace(); 48 | } catch (IOException e) { 49 | e.printStackTrace(); 50 | }finally{ 51 | try { 52 | fileReader.close(); 53 | } catch (IOException e) { 54 | e.printStackTrace(); 55 | } 56 | } 57 | } 58 | } -------------------------------------------------------------------------------- /FileIO/src/com/cdac/fileio/bytestream/Employee.java: -------------------------------------------------------------------------------- 1 | package com.cdac.fileio.bytestream; 2 | 3 | import java.io.Serializable; 4 | 5 | public class Employee implements Serializable{ 6 | 7 | private int empId; 8 | private String name; 9 | transient private int salary; 10 | public int getEmpId() { 11 | return empId; 12 | } 13 | public void setEmpId(int empId) { 14 | this.empId = empId; 15 | } 16 | public String getName() { 17 | return name; 18 | } 19 | public void setName(String name) { 20 | this.name = name; 21 | } 22 | public int getSalary() { 23 | return salary; 24 | } 25 | public void setSalary(int salary) { 26 | this.salary = salary; 27 | } 28 | @Override 29 | public String toString() { 30 | return empId+" "+name+" "+salary; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /FileIO/src/com/cdac/fileio/bytestream/SerializationDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.fileio.bytestream; 2 | 3 | import java.io.FileInputStream; 4 | import java.io.FileOutputStream; 5 | import java.io.IOException; 6 | import java.io.ObjectInputStream; 7 | import java.io.ObjectOutputStream; 8 | 9 | public class SerializationDemo { 10 | 11 | public static void main(String[] args) throws IOException, ClassNotFoundException { 12 | writeObject(); 13 | readObject(); 14 | } 15 | 16 | public static void writeObject() throws IOException{ 17 | Employee e = new Employee(); 18 | e.setEmpId(1100); 19 | e.setName("Sathish"); 20 | e.setSalary(50000); 21 | 22 | FileOutputStream fos = new FileOutputStream("D:\\empobject.txt"); 23 | ObjectOutputStream oos = new ObjectOutputStream(fos); 24 | oos.writeObject(e); 25 | oos.close(); 26 | fos.close(); 27 | System.out.println("Object successfully saved in the disk."); 28 | } 29 | 30 | public static void readObject() throws IOException, ClassNotFoundException{ 31 | FileInputStream fis = new FileInputStream("D:\\empobject.txt"); 32 | ObjectInputStream ois = new ObjectInputStream(fis); 33 | Employee e = (Employee) ois.readObject(); 34 | ois.close(); 35 | fis.close(); 36 | System.out.println("Employee Details : "+e); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /FileIO/src/com/cdac/fileio/bytestream/TestErroStream.java: -------------------------------------------------------------------------------- 1 | package com.cdac.fileio.bytestream; 2 | 3 | import java.io.FileReader; 4 | import java.io.FileWriter; 5 | import java.io.IOException; 6 | import java.io.InputStreamReader; 7 | import java.io.Reader; 8 | 9 | public class TestErroStream { 10 | public static void main(String[] args) throws IOException { 11 | 12 | int b = System.in.read(); 13 | 14 | for(int i=0;i<5;i++){ 15 | System.out.println("Hello"); 16 | System.out.flush(); 17 | System.err.println("Error"); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /Generics/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Generics/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Generics/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Generics 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Generics/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Generics/src/com/cdac/generics/Database.java: -------------------------------------------------------------------------------- 1 | package com.cdac.generics; 2 | 3 | public class Database { 4 | T object; 5 | public void set(T t){ 6 | this.object = t; 7 | } 8 | 9 | public T get(){ 10 | return object; 11 | } 12 | } -------------------------------------------------------------------------------- /Generics/src/com/cdac/generics/GenericCollectionDisplay.java: -------------------------------------------------------------------------------- 1 | package com.cdac.generics; 2 | 3 | import java.util.Collection; 4 | import java.util.Iterator; 5 | import java.util.List; 6 | 7 | public class GenericCollectionDisplay { 8 | public static void display(Collection list){ 9 | Iterator iterator = list.iterator(); 10 | while( iterator.hasNext()){ 11 | Object obj = iterator.next(); 12 | System.out.println(obj); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /Generics/src/com/cdac/generics/GenericCollectionDisplayMain.java: -------------------------------------------------------------------------------- 1 | package com.cdac.generics; 2 | 3 | import java.util.ArrayList; 4 | import java.util.HashSet; 5 | import java.util.TreeSet; 6 | 7 | public class GenericCollectionDisplayMain { 8 | public static void main(String[] args) { 9 | ArrayList list = new ArrayList<>(); 10 | list.add("Java"); 11 | list.add("Perl"); 12 | list.add("PHP"); 13 | list.add("Dot Net"); 14 | System.out.println("Reading elements from List :: "); 15 | GenericCollectionDisplay.display(list); 16 | 17 | HashSet set = new HashSet<>(); 18 | set.add(10); 19 | set.add(20); 20 | set.add(30); 21 | System.out.println("Reading elements from Set :: "); 22 | GenericCollectionDisplay.display(set); 23 | 24 | TreeSet treeSet = new TreeSet<>(); 25 | treeSet.addAll(list); 26 | System.out.println("Reading elements from TreeSet :: "); 27 | GenericCollectionDisplay.display(treeSet); 28 | } 29 | } -------------------------------------------------------------------------------- /Generics/src/com/cdac/generics/NumberDatabase.java: -------------------------------------------------------------------------------- 1 | package com.cdac.generics; 2 | 3 | public class NumberDatabase { 4 | //this class supports objects from only Number 5 | //( type of objects should be extended Number class) 6 | T object; 7 | 8 | public void set(T t){ 9 | this.object = t; 10 | } 11 | 12 | public T get(){ 13 | return object; 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /Generics/src/com/cdac/generics/NumberDatabaseMain.java: -------------------------------------------------------------------------------- 1 | package com.cdac.generics; 2 | 3 | import java.util.HashMap; 4 | 5 | public class NumberDatabaseMain { 6 | public static void main(String[] args) { 7 | 8 | NumberDatabase integerDB = new NumberDatabase<>(); 9 | integerDB.set(100); 10 | System.out.println("Integer from database : "+integerDB.get()); 11 | 12 | NumberDatabase doubleInteger = new NumberDatabase<>(); 13 | doubleInteger.set(153.456); 14 | System.out.println("Double from database : "+doubleInteger.get()); 15 | 16 | NumberDatabase longDatabase = new NumberDatabase<>(); 17 | longDatabase.set(1200000000L); 18 | System.out.println("Long from database : "+longDatabase.get()); 19 | 20 | // NumberDatabase stringDatabase = new NumberDatabase<>(); 21 | //the above code will throw error 22 | //String class is not derived from Number class 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /Generics/src/com/cdac/generics/StringDB.java: -------------------------------------------------------------------------------- 1 | package com.cdac.generics; 2 | 3 | import java.util.Date; 4 | 5 | public class StringDB { 6 | 7 | public static void main(String[] args) { 8 | Database db = new Database<>(); 9 | db.set("Hello"); 10 | String s = db.get(); 11 | System.out.println(s); 12 | 13 | Database date = new Database<>(); 14 | date.set(new Date()); 15 | Date dd = date.get(); 16 | // String abc = (String)date.get(); // not ok 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /InterviewPrograms/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /InterviewPrograms/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /InterviewPrograms/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | InterviewPrograms 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /InterviewPrograms/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /InterviewPrograms/src/com/algorithm/search/LinearSearch.java: -------------------------------------------------------------------------------- 1 | package com.algorithm.search; 2 | 3 | /* 4 | * Linear Search 5 | * -------------- 6 | * In computer science, a linear search or 7 | * sequential search is a method for finding 8 | * an element within a list. 9 | * 10 | * It sequentially checks each element of the 11 | * list until a match is found or the 12 | * whole list has been searched. 13 | * 14 | * Time Complexity 15 | * ---------------- 16 | * Worst Case O(n) 17 | * Best Case O(1) 18 | */ 19 | 20 | public class LinearSearch { 21 | public static void main(String[] args) { 22 | int array[] ={3,60,35,2,45,320,5}; 23 | int X = 320; 24 | 25 | for(int i=0;i array[j+1]){ 39 | int temp = array[j]; 40 | array[j] = array[j+1]; 41 | array[j+1] = temp; 42 | } 43 | } 44 | } 45 | } 46 | } 47 | /* 48 | OUTPUT 49 | Before Sorting :: 50 | [3, 60, 35, 2, 45, 320, 5] 51 | After Sorting :: 52 | [2, 3, 5, 35, 45, 60, 320] 53 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/algorithm/sorting/InsertionSort.java: -------------------------------------------------------------------------------- 1 | package com.algorithm.sorting; 2 | 3 | import java.util.Arrays; 4 | /* 5 | * Insertion Sort 6 | * -------------- 7 | * Insertion sort is the sorting mechanism where 8 | * the sorted array is built having one item at a time. 9 | * The array elements are compared with each 10 | * other sequentially and then arranged simultaneously 11 | * in some particular order. 12 | * 13 | * It is much less efficient on large lists than 14 | * more advanced algorithms such as quicksort, heapsort, 15 | * or merge sort. 16 | * 17 | * Time Complexity 18 | * --------------- 19 | * Worst Case : O(n^2) 20 | * Best Case : O(n) 21 | */ 22 | 23 | public class InsertionSort { 24 | public static void main(String[] args) { 25 | int array[] ={3,60,35,2,45,320,5}; 26 | System.out.println("Before Sorting :: "); 27 | System.out.println(Arrays.toString(array)); 28 | insertionSort(array); 29 | System.out.println("After Sorting :: "); 30 | System.out.println(Arrays.toString(array)); 31 | } 32 | 33 | public static void insertionSort(int array[]){ 34 | int n = array.length; 35 | for(int i=1;i= 0 && array[j] > key){ 39 | array[j+1] = array[j]; 40 | j = j-1; 41 | } 42 | array[j+1] = key; 43 | } 44 | } 45 | } 46 | /* 47 | OUTPUT 48 | Before Sorting :: 49 | [3, 60, 35, 2, 45, 320, 5] 50 | After Sorting :: 51 | [2, 3, 5, 35, 45, 60, 320] 52 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/algorithm/sorting/SelectionSort.java: -------------------------------------------------------------------------------- 1 | package com.algorithm.sorting; 2 | 3 | import java.util.Arrays; 4 | 5 | /* 6 | * Selection Sort 7 | * -------------- 8 | * The selection sort algorithm sorts an array 9 | * by repeatedly finding the minimum element 10 | * (considering ascending order) from unsorted 11 | * part and putting it at the beginning. 12 | * The algorithm maintains two subarrays in a given array. 13 | * - The subarray which is already sorted. 14 | * - Remaining subarray which is unsorted. 15 | * 16 | * Time Complexity 17 | * --------------- 18 | * Worst Case : O(n^2) 19 | * Best Case : O(n*log(n)) 20 | * 21 | */ 22 | 23 | public class SelectionSort { 24 | public static void main(String[] args) { 25 | int array[] = {64,25,12,22,11}; 26 | System.out.println("Before Sorting :: "); 27 | System.out.println(Arrays.toString(array)); 28 | selectionSort(array); 29 | System.out.println("After Sorting :: "); 30 | System.out.println(Arrays.toString(array)); 31 | } 32 | 33 | public static void selectionSort(int array[]) { 34 | int n = array.length; 35 | for (int i = 0; i < n - 1; i++) { 36 | // Find the minimum element in unsorted array 37 | int minIndex = i; 38 | for (int j = i + 1; j < n; j++) 39 | if (array[j] < array[minIndex]) 40 | minIndex = j; 41 | //swap the minimum elt with the elt in index i. 42 | int temp = array[minIndex]; 43 | array[minIndex] = array[i]; 44 | array[i] = temp; 45 | } 46 | } 47 | } 48 | /* 49 | OUTPUT 50 | Before Sorting :: 51 | [64, 25, 12, 22, 11] 52 | After Sorting :: 53 | [11, 12, 22, 25, 64] 54 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/hackerrank/AllCombinationsOfBrackets.java: -------------------------------------------------------------------------------- 1 | package com.hackerrank; 2 | 3 | public class AllCombinationsOfBrackets { 4 | 5 | static int count = 0; 6 | public static void main(String[] args) { 7 | brackets(4, 0, ""); 8 | bracketsCount(4, 0, ""); 9 | System.out.println("Count is :: "+count); 10 | } 11 | 12 | public static void brackets(int openStack,int closeStack, String s ){ 13 | if(openStack == 0 && closeStack == 0) 14 | System.out.println(s); 15 | if(openStack > 0) 16 | brackets(openStack-1, closeStack+1, s+"("); 17 | if( closeStack > 0) 18 | brackets(openStack, closeStack-1, s+")"); 19 | } 20 | 21 | public static void bracketsCount(int openStack,int closeStack, String s ){ 22 | if(openStack == 0 && closeStack == 0) 23 | count++; 24 | if(openStack > 0) 25 | bracketsCount(openStack-1, closeStack+1, s+"("); 26 | if( closeStack > 0) 27 | bracketsCount(openStack, closeStack-1, s+")"); 28 | } 29 | } 30 | 31 | /* 32 | Input 33 | 3 34 | Output 35 | ((())) 36 | (()()) 37 | (())() 38 | ()(()) 39 | ()()() 40 | Count is :: 5 41 | 42 | Input 43 | 4 44 | Output 45 | (((()))) 46 | ((()())) 47 | ((())()) 48 | ((()))() 49 | (()(())) 50 | (()()()) 51 | (()())() 52 | (())(()) 53 | (())()() 54 | ()((())) 55 | ()(()()) 56 | ()(())() 57 | ()()(()) 58 | ()()()() 59 | Count is :: 14 60 | */ 61 | 62 | -------------------------------------------------------------------------------- /InterviewPrograms/src/com/hackerrank/BalancedBrackets.java: -------------------------------------------------------------------------------- 1 | package com.hackerrank; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Scanner; 5 | 6 | public class BalancedBrackets { 7 | public static void main(String[] args) { 8 | Scanner scanner = new Scanner(System.in); 9 | int n = Integer.parseInt(scanner.nextLine().trim()); 10 | String output[] = new String[n]; 11 | for(int i=0;i charStack = new ArrayList<>(); 22 | for(Character c : line.toCharArray()){ 23 | if(c == '(' || c == '[' || c == '{'){ 24 | charStack.add(c); 25 | }else{ 26 | if(charStack.isEmpty()){ 27 | output = "NO"; 28 | break; 29 | } 30 | char ch = charStack.remove(charStack.size()-1); 31 | if(c == ')' && ch == '('){ 32 | continue; 33 | }else if(c == '}' && ch == '{'){ 34 | continue; 35 | }else if(c == ']' && ch == '['){ 36 | continue; 37 | }else{ 38 | output = "NO"; 39 | break; 40 | } 41 | } 42 | } 43 | 44 | if(!charStack.isEmpty()) 45 | output = "NO"; 46 | if(output == null) 47 | output = "YES"; 48 | 49 | return output; 50 | } 51 | } 52 | 53 | /* 54 | 55 | Input 56 | 3 57 | {[()]} 58 | {[(])} 59 | {{[[(())]]}} 60 | {{}( 61 | 62 | Output 63 | YES 64 | NO 65 | YES 66 | NO 67 | 68 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/array/Average.java: -------------------------------------------------------------------------------- 1 | package com.java.array; 2 | 3 | /* 4 | * Calculate the Average of Given Array 5 | * ------------------------------------- 6 | * 7 | * This is one basic program using array. 8 | * Steps 9 | * 1. Declare a variable sum with 0 10 | * 2. Iterate the array and add with sum variable 11 | * 3. Divide the sum with array length 12 | * 13 | * array = {10, 10, 10, 10, 10}; 14 | * sum is 50 15 | * array length is 5 16 | * average is 10 17 | * 18 | * array = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; 19 | * sum is 550 20 | * array length is 10 21 | * average is 55 22 | */ 23 | public class Average { 24 | public static void main(String[] args) { 25 | //int array[] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; 26 | int array[] = {10, 10, 10, 10, 10}; 27 | int n = array.length; 28 | int sum = 0; 29 | for(int v : array) 30 | sum += v; 31 | float average = sum/n; 32 | 33 | System.out.println("The Given Array is : "); 34 | for(int v : array) 35 | System.out.print(v+" "); 36 | System.out.println("\nThe Average of Given Array is : "+average); 37 | } 38 | } 39 | /* 40 | OUTPUT 41 | The Given Array is : 42 | 10 20 30 40 50 60 70 80 90 100 43 | The Average of Given Array is : 55.0 44 | 45 | OUTPUT 46 | The Given Array is : 47 | 10 10 10 10 10 48 | The Average of Given Array is : 10.0 49 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/array/FindAllDuplicates.java: -------------------------------------------------------------------------------- 1 | package com.java.array; 2 | 3 | import java.util.HashSet; 4 | 5 | /* 6 | * Find All duplicate elements 7 | * --------------------------- 8 | * Write java program to find all the duplicates 9 | * elements from the given array. 10 | * 11 | * This solution will work only when array contains elements 12 | * value lesser than array size. 13 | * 14 | * Suppose the array size is 5, and array contains value 10 15 | * For this case below code will not work. 16 | * 17 | */ 18 | public class FindAllDuplicates { 19 | public static void main(String[] args) { 20 | HashSet set = new HashSet<>(); 21 | // int array[] = {4 , 2, 4, 5, 2, 3, 1, 2, 3}; 22 | // int array[] = {2, 1, 2, 2, 1, 3}; 23 | int arr[] = {3, 8, 1, 3, 6, 3, 8, 1, 6}; 24 | for(int i =0;i 0) 26 | arr[Math.abs(arr[i])] = -arr[Math.abs(arr[i])]; 27 | else{ 28 | set.add(Math.abs(arr[i])); 29 | } 30 | } 31 | System.out.println("All duplicates are :: "+set); 32 | } 33 | } 34 | 35 | /* 36 | INPUT 37 | {4,2,4,5,2,3,1,2,3} 38 | OUTPUT 39 | All duplicates are :: [2, 3, 4] 40 | 41 | INPUT 42 | {2, 1, 2, 2, 1, 3} 43 | OUTPUT 44 | All duplicates are :: [1, 2] 45 | 46 | INPUT 47 | {3, 8, 1, 3, 6, 3, 8, 1, 6}; 48 | OUTPUT 49 | All duplicates are :: [1, 3, 6, 8] 50 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/array/FindUniqueElt.java: -------------------------------------------------------------------------------- 1 | package com.java.array; 2 | 3 | /* 4 | * Find the only one unique Element in the Array 5 | * --------------------------------------------- 6 | * 7 | * In the given array only one element is 8 | * occurred one time, all the other elements are 9 | * repeated exactly two times. 10 | * Find the element which occurred only one time 11 | * 12 | * array is { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5}; 13 | * 14 | * 6 is repeated only one time 15 | * 1, 2, 3, 4, 5 all repeated two times 16 | * 17 | * Write the program to calculate 6 18 | * 19 | * Solution 20 | * -------- 21 | * use ^ (XOR) operator 22 | * apply ^ (XOR) operator to all the values 23 | * in the array. 24 | * when XOR apply between same value result is 0 25 | * 26 | * Finally we will the output as the value 27 | * which occurred only one time 28 | */ 29 | public class FindUniqueElt { 30 | public static void main(String[] args) { 31 | //int array[] = { 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5}; 32 | int array[] = {5, 10, 33, 42, 5, 42, 10}; 33 | int output = array[0]; 34 | for(int i=1;i= 0; i--) 26 | if(array[i] != 0) 27 | array[count--] = array[i]; 28 | 29 | while(count >= 0) 30 | array[count--] = 0; 31 | 32 | System.out.println("\nAfter moving all zeros to start of array :: "); 33 | for(int v : array) 34 | System.out.print(v+" "); 35 | } 36 | } 37 | /* 38 | OUTPUT 39 | The Given Array is :: 40 | 1 0 4 3 0 0 2 0 1 0 41 | After moving all zeros to start of array :: 42 | 0 0 0 0 0 1 4 3 2 1 43 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/array/ReverseArray.java: -------------------------------------------------------------------------------- 1 | package com.java.array; 2 | 3 | /* 4 | * Reverse the Array without using additional Array 5 | * ------------------------------------------------ 6 | * 7 | * given array[]= {1,2,3,4,5,6,7,8,9,10}; 8 | * reversed array[]= {10,9,8,7,6,5,4,3,2,1}; 9 | * 10 | * Steps to reverse 11 | * swap 0th index with (N-1) index (swap 1 & 10) 12 | * swap 1st index with (N-2) index (swap 2 & 9) 13 | * swap 2nd index with (N-3) index (swap 3 & 8) 14 | * swap 3rd index with (N-4) index (swap 4 & 7) 15 | * swap 4th index with (N-5) index (swap 5 & 6) 16 | * 17 | * In General 18 | * 19 | * swap i th index with (N - 1 - i) index 20 | * No of iterations will by (N/2) 21 | * 22 | */ 23 | 24 | public class ReverseArray { 25 | public static void main(String[] args) { 26 | int array[]= {1,2,3,4,5,6,7,8,9,10}; 27 | int N = array.length; 28 | int temp; 29 | 30 | System.out.println("The Given Array is : "); 31 | for(int x : array) { 32 | System.out.print(x+" "); 33 | } 34 | 35 | // N/2 iterations 36 | for(int i=0;i digitsList = new ArrayList<>(); 28 | while(num > 0){ 29 | int unitDigit = num % 10; 30 | digitsList.add(0, unitDigit); 31 | num = num/10; 32 | } 33 | 34 | System.out.println("The digits of the Giver Number : "); 35 | for(int digit : digitsList) 36 | System.out.print(digit+" "); 37 | scanner.close(); 38 | } 39 | } 40 | /* 41 | OUTPUT 42 | Enter any positive integer :: 2019 43 | The digits of the Giver Number : 44 | 2 0 1 9 45 | 46 | Enter any positive integer :: 3245879 47 | The digits of the Giver Number : 48 | 3 2 4 5 8 7 9 49 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/basic/Divisors.java: -------------------------------------------------------------------------------- 1 | package com.java.basic; 2 | 3 | import java.util.Scanner; 4 | import java.util.TreeSet; 5 | 6 | /* 7 | * Divisors of N 8 | * ------------- 9 | * 10 | * This program finds all the divisors of the 11 | * Given number N 12 | * 13 | * say Given Number is 45, Divisors are 14 | * [1, 3, 5, 9, 15, 45] 15 | * 16 | * say Given Number is 60, Divisors are 17 | * [1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60] 18 | * 19 | */ 20 | 21 | public class Divisors { 22 | public static void main(String[] args) { 23 | Scanner scanner = new Scanner(System.in); 24 | System.out.println("Enter the N value : "); 25 | int N = scanner.nextInt(); 26 | 27 | TreeSet divisors = new TreeSet<>(); 28 | for(int i=1;i symbol 4 | * ---------------------------------------------------------- 5 | * Mostly to find max or min we use < or > symbol 6 | * But here without using < or > symbol how to find smallest 7 | * 8 | * Use division operator to compare two compare 9 | * a = 10 b = 20 10 | * a/b == 0 -> this means a is smaller than b 11 | * a = 20 b = 5 12 | * b/a == 0 -> this means b is smaller than a 13 | * 14 | * This is the technique which we have to use to find the 15 | * smallest of 3 (a,b,c) 16 | * 17 | */ 18 | public class FindTheSmallest { 19 | public static void main(String[] args) { 20 | int a = 10; 21 | int b = 5; 22 | int c = 20; 23 | 24 | if(a/b == 0 && a/c == 0) 25 | System.out.println("a is the smallest"); 26 | else if(b/a == 0 && b/c == 0) 27 | System.out.println("b is the smallest"); 28 | else 29 | System.out.println("c is the smallest"); 30 | } 31 | } 32 | /* 33 | INPUT 34 | a = 10 35 | b = 20 36 | c = 5 37 | OUTPUT 38 | c is the smallest 39 | 40 | INPUT 41 | a = 10 42 | b = 5 43 | c = 20 44 | OUTPUT 45 | b is the smallest 46 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/basic/LeapYear.java: -------------------------------------------------------------------------------- 1 | package com.java.basic; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * LEAP YEAR 7 | * --------- 8 | * This program is to check the given number is leap year or not. 9 | * 10 | * Leap Year Conditions 11 | * 1. Year should be divisible by 4 12 | * 2. If it is divisible by 100 then should be divisible by 400 13 | * 3. If both conditions are not satisfied, then given input is not leap year. 14 | * 15 | * Example 16 | * 2000 - LEAP YEAR 17 | * 2100 - NOT A LEAP YEAR 18 | * 2020 - LEAP YEAR 19 | * 2019 - NOT A LEAP YEAR 20 | * 1900 - NOT A LEAP YEAR 21 | * 1996 - LEAP YEAR 22 | */ 23 | public class LeapYear { 24 | public static void main(String[] args) { 25 | Scanner scanner = new Scanner(System.in); 26 | System.out.println("Enter a year :: "); 27 | int year = scanner.nextInt(); 28 | 29 | if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 30 | System.out.println("YES LEAP YEAR!"); 31 | else 32 | System.out.println("No its not leap year."); 33 | 34 | scanner.close(); 35 | } 36 | } 37 | /* 38 | INPUT 39 | Enter a year :: 2020 40 | OUTPUT 41 | YES LEAP YEAR! 42 | 43 | INPUT 44 | Enter a year :: 2000 45 | OUTPUT 46 | YES LEAP YEAR! 47 | 48 | INPUT 49 | Enter a year :: 1996 50 | OUTPUT 51 | YES LEAP YEAR! 52 | 53 | INPUT 54 | Enter a year :: 2100 55 | OUTPUT 56 | No its not leap year. 57 | 58 | INPUT 59 | Enter a year :: 2019 60 | OUTPUT 61 | No its not leap year. 62 | 63 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/basic/PositiveOrNegative.java: -------------------------------------------------------------------------------- 1 | package com.java.basic; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Positive Or Negative 7 | * --------------------- 8 | * An integer is a whole number that can be either 9 | * greater than 0, called positive, or less than 0, 10 | * called negative. 11 | * 12 | * Zero is neither positive nor negative. 13 | * 14 | * 100 is Positive Number 15 | * -22 is Negative Number 16 | * 17 | */ 18 | public class PositiveOrNegative { 19 | public static void main(String[] args) { 20 | Scanner scanner = new Scanner(System.in); 21 | System.out.println("Enter any integer : "); 22 | 23 | int n = scanner.nextInt(); 24 | if(n > 0 ) 25 | System.out.println(n+" is a Positive Number."); 26 | else if( n < 0 ) 27 | System.out.println(n+" is a Negative Number."); 28 | else 29 | System.out.println("Zero is neither Positive nor Negative."); 30 | 31 | scanner.close(); 32 | } 33 | } 34 | /* 35 | OUTPUT 36 | Enter any integer : 155 37 | 155 is a Positive Number. 38 | 39 | OUTPUT 40 | Enter any integer : -77 41 | -77 is a Negative Number. 42 | 43 | OUTPUT 44 | Enter any integer : 0 45 | Zero is neither Positive nor Negative. 46 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/basic/Print10WithoutLoop.java: -------------------------------------------------------------------------------- 1 | package com.java.basic; 2 | 3 | /* 4 | * Print 1 to 10 without using loop 5 | * -------------------------------- 6 | * 7 | * Solution: 8 | * Use recursion to print without using loop 9 | * 10 | */ 11 | public class Print10WithoutLoop { 12 | public static void main(String[] args) { 13 | printWihtoutLoop(1); 14 | } 15 | 16 | public static void printWihtoutLoop(int n){ 17 | if(n <= 10){ 18 | System.out.println(n); 19 | printWihtoutLoop( n+1 ); 20 | } 21 | } 22 | } 23 | /* 24 | OUTPUT 25 | 1 26 | 2 27 | 3 28 | 4 29 | 5 30 | 6 31 | 7 32 | 8 33 | 9 34 | 10 35 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/basic/SwapApproach1.java: -------------------------------------------------------------------------------- 1 | package com.java.basic; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Swap Two Numbers 7 | * ----------------- 8 | * It is possible to swap two numbers without 9 | * using third variable. 10 | * 11 | * Approach 1:: 12 | * using +, - operators 13 | * a = 15, b = 20 14 | * 15 | * a = a + b // after a = 35, b = 20 16 | * b = a - b // after a = 35, b = 15 17 | * a = a - b // after a = 20, b = 15 18 | * 19 | */ 20 | public class SwapApproach1 { 21 | public static void main(String[] args) { 22 | Scanner scanner = new Scanner(System.in); 23 | System.out.println("Enter the value for a : "); 24 | int a = scanner.nextInt(); 25 | System.out.println("Enter the value for b : "); 26 | int b = scanner.nextInt(); 27 | 28 | System.out.println("Before swapping a & b : "); 29 | System.out.println("a = "+a+", b = "+b); 30 | 31 | //using +, - operators 32 | a = a + b; 33 | b = a - b; 34 | a = a - b; 35 | 36 | System.out.println("After swapping a & b : "); 37 | System.out.println("a = "+a+", b = "+b); 38 | 39 | scanner.close(); 40 | } 41 | } 42 | /* 43 | OUTPUT 44 | Enter the value for a : 15 45 | Enter the value for b : 20 46 | Before swapping a & b : 47 | a = 15, b = 20 48 | After swapping a & b : 49 | a = 20, b = 15 50 | 51 | OUTPUT 52 | Enter the value for a : 2 53 | Enter the value for b : 7 54 | Before swapping a & b : 55 | a = 15, b = 10 56 | After swapping a & b : 57 | a = 10, b = 15 58 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/basic/SwapApproach2.java: -------------------------------------------------------------------------------- 1 | package com.java.basic; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Swap Two Numbers 7 | * ----------------- 8 | * It is possible to swap two numbers without 9 | * using third variable. 10 | * 11 | * Approach 2:: 12 | * using *, / operators (Multiplication , Division) 13 | * a = 15, b = 10 14 | * 15 | * a = a * b // after a = 150, b = 10 16 | * b = a / b // after a = 150, b = 15 17 | * a = a / b // after a = 10, b = 15 18 | * 19 | */ 20 | public class SwapApproach2 { 21 | public static void main(String[] args) { 22 | Scanner scanner = new Scanner(System.in); 23 | System.out.println("Enter the value for a : "); 24 | int a = scanner.nextInt(); 25 | System.out.println("Enter the value for b : "); 26 | int b = scanner.nextInt(); 27 | 28 | System.out.println("Before swapping a & b : "); 29 | System.out.println("a = "+a+", b = "+b); 30 | 31 | //using *, / operators 32 | a = a * b; 33 | b = a / b; 34 | a = a / b; 35 | 36 | System.out.println("After swapping a & b : "); 37 | System.out.println("a = "+a+", b = "+b); 38 | 39 | scanner.close(); 40 | } 41 | } 42 | /* 43 | OUTPUT 44 | Enter the value for a : 15 45 | Enter the value for b : 20 46 | Before swapping a & b : 47 | a = 15, b = 20 48 | After swapping a & b : 49 | a = 20, b = 15 50 | 51 | OUTPUT 52 | Enter the value for a : 15 53 | Enter the value for b : 10 54 | Before swapping a & b : 55 | a = 15, b = 10 56 | After swapping a & b : 57 | a = 10, b = 15 58 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/basic/SwapApproach3.java: -------------------------------------------------------------------------------- 1 | package com.java.basic; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Swap Two Numbers 7 | * ---------------- 8 | * It is possible to swap two numbers without 9 | * using third variable. 10 | * 11 | * Approach 3:: 12 | * using ^ (XOR) operator 13 | * a = 3, b = 10 14 | * (0011) (1010) 15 | * 16 | * a = a ^ b // a = 0011 ^ 1010 = 1001 17 | * b = a ^ b // b = 1001 ^ 1010 = 0011 18 | * a = a ^ b // a = 1001 ^ 0011 = 1010 19 | * 20 | */ 21 | public class SwapApproach3 { 22 | public static void main(String[] args) { 23 | Scanner scanner = new Scanner(System.in); 24 | System.out.println("Enter the value for a : "); 25 | int a = scanner.nextInt(); 26 | System.out.println("Enter the value for b : "); 27 | int b = scanner.nextInt(); 28 | 29 | System.out.println("Before swapping a & b : "); 30 | System.out.println("a = "+a+", b = "+b); 31 | 32 | //using ^ (XOR) operators 33 | a = a ^ b; 34 | b = a ^ b; 35 | a = a ^ b; 36 | 37 | System.out.println("After swapping a & b : "); 38 | System.out.println("a = "+a+", b = "+b); 39 | scanner.close(); 40 | } 41 | } 42 | /* 43 | OUTPUT 44 | Enter the value for a : 15 45 | Enter the value for b : 20 46 | Before swapping a & b : 47 | a = 15, b = 20 48 | After swapping a & b : 49 | a = 20, b = 15 50 | 51 | OUTPUT 52 | Enter the value for a : 15 53 | Enter the value for b : 10 54 | Before swapping a & b : 55 | a = 15, b = 10 56 | After swapping a & b : 57 | a = 10, b = 15 58 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/coin/change/MinimumCoins.java: -------------------------------------------------------------------------------- 1 | package com.java.coin.change; 2 | 3 | public class MinimumCoins { 4 | public static void main(String[] args) { 5 | // int n = 40; 6 | // int coins[] = {5,10,20}; 7 | 8 | int n = 5; 9 | int coins[] = {1,2}; 10 | int noOfCoins = coins.length; 11 | 12 | int table[] = new int[n+1]; 13 | table[0] = 0; 14 | 15 | for(int i=1;i<=n;i++) 16 | table[i] = Integer.MAX_VALUE; 17 | 18 | for(int i=1;i<=n;i++){ 19 | for(int j=0;j 0 ){ 29 | int digit = num % 10; 30 | digitsSum += digit * digit * digit; 31 | num = num / 10; 32 | } 33 | 34 | if(digitsSum == tempNum) 35 | return true; 36 | return false; 37 | } 38 | } 39 | /* 40 | OUTPUT 41 | Armstrong Number from 1 to 1000 :: 42 | 1 153 370 371 407 43 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/AutomorphicNumberList.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | /* 4 | * Automorphic Number 5 | * 6 | * An automorphic number is a number 7 | * whose square ends with the number itself. 8 | * 9 | * examples are 10 | * 11 | * 5^2 = 25 = here 25 ends with 5 12 | * 6^2 = 36 = here 36 ends with 6 13 | * 76^2 = 5776 = here 5776 ends with 76 14 | * 15 | * Solution steps: 16 | * 1. find the square root of the number n 17 | * 2. get the unit digit of n 18 | * 3. get the unit digit of sqrt of n 19 | * 4. check both are same or not 20 | * 5. if both are same continue from step 2. 21 | * 6. if both are not same return false 22 | * means this number is not a automorphic 23 | * 24 | * Automorphic Numbers are 25 | * 1, 5, 6, 25, 76, 376, 625 26 | * 27 | */ 28 | public class AutomorphicNumberList { 29 | public static void main(String[] args) { 30 | System.out.println("Automorphic Number from 1 to 1000 :: "); 31 | 32 | for(int i=1;i<=1000;i++) 33 | if(isAutomorphicNumber(i)) 34 | System.out.print(i+" "); 35 | } 36 | 37 | private static boolean isAutomorphicNumber(int n){ 38 | int sqrt = n * n; 39 | while (n > 0){ 40 | int reminder1 = n % 10; 41 | int reminder2 = sqrt % 10; 42 | if(reminder1 != reminder2) 43 | return false; 44 | n = n/10; 45 | sqrt = sqrt/10; 46 | } 47 | return true; 48 | } 49 | } 50 | /* 51 | OUTPUT 52 | Automorphic Number from 1 to 1000 :: 53 | 1 5 6 25 76 376 625 54 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/GCD.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | /* 4 | * Find GCD of two numbers 5 | * GCD :: Greatest Common Divisor 6 | * The HCF or GCD of two numbers is the largest number 7 | * that can divide both numbers without reminder. 8 | * 9 | * Number a = 45 = 3 * 3 * 5 10 | * Number b = 75 = 3 * 5 * 5 11 | * GCD is = 3 * 5 = 15 12 | */ 13 | 14 | public class GCD { 15 | public static void main(String[] args) { 16 | int a = 10; 17 | int b = 55; 18 | 19 | // for this approach 20 | //a should be bigger 21 | //b should be small 22 | if(a < b){ 23 | int temp = a; 24 | a = b; 25 | b = temp; 26 | } 27 | 28 | while(b > 0){ 29 | int temp = b; 30 | //as b is small get reminder of a and b 31 | b = a%b; 32 | a = temp; 33 | } 34 | System.out.println("GCD is 10 & 55 : "+a); 35 | } 36 | } 37 | /* 38 | OUTPUT 39 | GCD is 10 & 55 : 5 40 | GCD is 143 & 26 : 13 41 | GCD is 7 & 29 : 1 42 | GCD is 45 & 75 : 15 43 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/KaprekarNumberList.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Kaprekar Number 7 | * 8 | * A Kaprekar number is a number whose square 9 | * when divided into two parts and such that 10 | * sum of parts is equal to the original number 11 | * and none of the parts has value 0. 12 | * 13 | * for example n = 45 14 | * square of 45 is = 2025 15 | * 2025 = 20 + 25 = 45 16 | * 17 | * n Square 2 parts sum 18 | * 9 81 8 + 1 19 | * 45 2025 20 + 25 20 | * 55 3025 30 + 25 21 | * 99 9801 98 + 01 22 | * 297 88209 88 + 209 23 | * 703 494209 494 + 209 24 | * 999 998001 998 + 001 25 | * 26 | */ 27 | public class KaprekarNumberList { 28 | public static void main(String[] args) { 29 | Scanner scanner = new Scanner(System.in); 30 | System.out.println("Kaprekar Number from 2 to 1000 :: "); 31 | for(int i = 2;i<=1000;i++){ 32 | if(isKaprekarNumber(i) == true) 33 | System.out.print(i+" "); 34 | } 35 | scanner.close(); 36 | } 37 | 38 | public static boolean isKaprekarNumber(int num){ 39 | long square = num * num; 40 | String str = Long.toString(square); 41 | for(int i=1;i 0 && l2 > 0 && l1+l2 == num) 47 | return true; 48 | } 49 | return false; 50 | } 51 | } 52 | /* 53 | OUTPUT 54 | 55 | Kaprekar Number from 2 to 1000 :: 56 | 9 45 55 99 297 703 999 57 | 58 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/LCM.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | /* 4 | * LCM - Least Common Multiple 5 | * 6 | * The LCM of two integers is the smallest 7 | * positive integer that is perfectly divisible 8 | * by both the numbers without a reminder. 9 | * (means reminder should be zero) 10 | * 11 | * say n1 = 12 and n2 = 15 12 | * 12 & 15 can divide 60, 120, 180 ... 13 | * but the smallest number is 60 14 | * LCM of 12 & 15 is 60 15 | * 16 | * steps 17 | * 1. get the minimum of n1 & n2 18 | * 2. assume the maximum is LCM 19 | * 3. increment the lcm upto both n1 & n2 20 | * perfectly divides the LCM. 21 | * 22 | * n1 = 10 and n2 = 6 23 | * 10 and 6 can divide 30, 60, 90 ... 24 | * but the smallest number is 30 25 | * LCM of 10 & 6 is 30 26 | * 27 | */ 28 | public class LCM { 29 | public static void main(String[] args) { 30 | int a = 5; 31 | int b = 7; 32 | 33 | int lcm = (a > b) ? a : b; 34 | 35 | while(true){ 36 | if(lcm % a == 0 && lcm % b == 0){ 37 | System.out.println("LCM of "+a+" & "+b+" is "+lcm); 38 | break; 39 | } 40 | lcm++; 41 | } 42 | } 43 | } 44 | /* 45 | OUTPUT 46 | 47 | LCM of 12 & 15 is 60 48 | LCM of 10 & 6 is 30 49 | LCM of 10 & 5 is 10 50 | LCM of 5 & 7 is 35 51 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/LargestDivisor.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Largest Divisor 7 | * 8 | * Write a program to find the Largest Divisor of a number 9 | * Largest divisor except the given number. 10 | * 11 | * Say the number is 100 12 | * Divisors are : 1, 2, 4, 5, 10, 20, 25, 50 13 | * Largest Divisor is 50 14 | * 15 | * Say the number is 25 16 | * Divisors are : 1, 5 17 | * Largest Divisor is 5 18 | * 19 | */ 20 | public class LargestDivisor { 21 | public static void main(String[] args) { 22 | Scanner scanner = new Scanner(System.in); 23 | System.out.println("Enter a number :: "); 24 | int n = scanner.nextInt(); 25 | int output = largestDivisor(n); 26 | System.out.println("Largest Divisor is : "+output); 27 | scanner.close(); 28 | } 29 | 30 | public static int largestDivisor(int n){ 31 | for(int i=2;i<=Math.sqrt(n);i++) 32 | if(n%i == 0) 33 | return n/i; 34 | return 1; 35 | } 36 | } 37 | 38 | /* 39 | Enter a number :: 100 40 | Largest Divisor is : 50 41 | 42 | Enter a number :: 23 43 | Largest Divisor is : 1 44 | 45 | Enter a number :: 25 46 | Largest Divisor is : 5 47 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/PalindromeCheck.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | /* 4 | * Palindrome Check 5 | * 6 | * Check the given number is Palinndrome Number or NOT 7 | * 8 | * A palindrome number is a number that remains the same when 9 | * its digits are reversed. 10 | * 11 | * Say N = 16461 12 | * reverse of N = 16461 13 | * N and reverse of N are same 14 | * So 16461 is Palindrome number 15 | * 16 | * Other examples are 17 | * 12321, 1001, 10101 18 | * 19 | * NOT a Palindrome 20 | * 100, 123, 2020 21 | * 22 | */ 23 | public class PalindromeCheck { 24 | public static void main(String[] args) { 25 | int N = 12345; 26 | String reverse = ""; 27 | 28 | int temp = N; 29 | while(temp > 0){ 30 | int d = temp % 10; 31 | temp = temp/10; 32 | reverse = reverse + d; 33 | } 34 | int reverseN = Integer.parseInt(reverse); 35 | if(N == reverseN) 36 | System.out.println(N+" is a Palindrome Number"); 37 | else 38 | System.out.println(N+" is NOT a Palindrome Number"); 39 | } 40 | } 41 | /* 42 | OUTPUT 43 | 16461 is a Palindrome Number 44 | 45 | OUTPUT 46 | 12321 is a Palindrome Number 47 | 48 | OUTPTU 49 | 12345 is NOT a Palindrome Number 50 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/PerfectNumber.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Perfect Number 7 | * --------------- 8 | * Perfect number is a positive integer that is 9 | * equal to the sum of its proper positive divisors, 10 | * that is, the sum of its positive divisors excluding 11 | * the number itself. 12 | * 13 | * Following are the examples of perfect number. 14 | * 6 = 1+2+3 15 | * 28= 1+2+4+7+14 16 | * 496= 1+2+4+8+16+31+62+124+248 17 | * 18 | */ 19 | public class PerfectNumber { 20 | 21 | public static void main(String[] args) { 22 | Scanner scanner = new Scanner(System.in); 23 | System.out.println("Enter any positive integer :: "); 24 | int num = Integer.parseInt(scanner.nextLine().trim()); 25 | 26 | if(isPerfectNumber(num)) 27 | System.out.println("Given number : "+num+" is a Perfect Number"); 28 | else 29 | System.out.println("Given number : "+num+" is NOT a Perfect Number"); 30 | scanner.close(); 31 | } 32 | 33 | private static boolean isPerfectNumber(int num) { 34 | int tempNum = num; 35 | int divisorSum = 1; 36 | for (int i = 2; i <= num / 2; i++) 37 | if (num % i == 0) 38 | divisorSum += i; 39 | 40 | if(tempNum == divisorSum) 41 | return true; 42 | return false; 43 | } 44 | } 45 | /* 46 | OUTPUT 47 | 48 | Enter any positive integer :: 6 49 | Given number : 6 is a Perfect Number 50 | 51 | Enter any positive integer :: 28 52 | Given number : 28 is a Perfect Number 53 | 54 | Enter any positive integer :: 100 55 | Given number : 100 is NOT a Perfect Number 56 | 57 | */ 58 | -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/PerfectNumberList.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | /* 4 | * Perfect Number from 1 to 1000 5 | * 6 | * Perfect number is a positive integer that is 7 | * equal to the sum of its proper positive divisors, 8 | * that is, the sum of its positive divisors excluding 9 | * the number itself. 10 | * 11 | * Following are the examples of perfect number. 12 | * 6 = 1+2+3 13 | * 28= 1+2+4+7+14 14 | * 496= 1+2+4+8+16+31+62+124+248 15 | * 16 | * All the Perfect Numbers 17 | * 1 6 28 496 8128 18 | */ 19 | public class PerfectNumberList { 20 | public static void main(String[] args) { 21 | System.out.println("Perfect Numbers from 1 to 1000 :: "); 22 | 23 | for(int i=1;i<=1000;i++) 24 | if(isPerfectNumber(i)) 25 | System.out.print(i+" "); 26 | } 27 | 28 | private static boolean isPerfectNumber(int num) { 29 | int tempNum = num; 30 | int divisorSum = 1; 31 | for (int i = 2; i <= num / 2; i++) 32 | if (num % i == 0) 33 | divisorSum += i; 34 | 35 | if(tempNum == divisorSum) 36 | return true; 37 | return false; 38 | } 39 | } 40 | /* 41 | OUTPUT 42 | 43 | Perfect Numbers from 1 to 1000 :: 44 | 1 6 28 496 45 | 46 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/PrimeFactors.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Prime Factors of a Given Number 7 | * 8 | * The prime factors of a number are all the 9 | * prime numbers that, when multiplied together, 10 | * equal the original number. 11 | * 12 | * You can find the prime factorization of a number 13 | * by using a factor tree and dividing the number 14 | * into smaller parts. 15 | * 16 | * say Given Number is 45, Prime factors are 17 | * 3, 3, 5 18 | * 19 | * say Given Number is 32, Prime factors are 20 | * 2, 2, 2, 2, 2 21 | * 22 | */ 23 | public class PrimeFactors { 24 | public static void main(String[] args) { 25 | Scanner scanner = new Scanner(System.in); 26 | System.out.println("Enter any positive integer :: "); 27 | int num = Integer.parseInt(scanner.nextLine().trim()); 28 | 29 | System.out.println("The Prime Factors are :"); 30 | printPrimeFactors(num); 31 | 32 | scanner.close(); 33 | } 34 | 35 | private static void printPrimeFactors(int num){ 36 | int limit = (int) Math.sqrt(num); 37 | 38 | for(int i=2;i<=limit;i++){ 39 | while(num % i == 0){ 40 | System.out.print(i+", "); 41 | num = num/i; 42 | } 43 | } 44 | if(num > 2) 45 | System.out.print(num); 46 | } 47 | } 48 | /* 49 | OUTPUT 50 | Enter any positive integer :: 72 51 | The Prime Factors are : 52 | 2, 2, 2, 3, 3, 53 | 54 | Enter any positive integer :: 22 55 | The Prime Factors are : 56 | 2, 11 57 | 58 | Enter any positive integer :: 90 59 | The Prime Factors are : 60 | 2, 3, 3, 5, 61 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/PrimeNumber.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | 7 | Prime number is a number that is greater than 1 and divided by 1 or itself only. 8 | In other words, prime numbers can't be divided by other numbers than itself or 1. 9 | For example 2, 3, 5, 7, 11, 13, 17.... are the prime numbers. 10 | 11 | */ 12 | public class PrimeNumber { 13 | 14 | public static void main(String[] args) { 15 | Scanner scanner = new Scanner(System.in); 16 | System.out.println("Enter any number::"); 17 | int number = Integer.parseInt(scanner.nextLine().trim()); 18 | boolean status = checkPrimeNumber(number); 19 | if(status == true) 20 | System.out.println("The entered number is Prime Number"); 21 | else 22 | System.out.println("The entered number is NOT a Prime Number"); 23 | scanner.close(); 24 | } 25 | 26 | private static boolean checkPrimeNumber(int number){ 27 | if(number <= 1) 28 | return false; 29 | int length = number / 2; 30 | 31 | for(int i=2;i 0){ 28 | int unitDigit = num % 10; 29 | reversedNum = (reversedNum * 10) + unitDigit; 30 | num = num / 10; 31 | } 32 | return reversedNum; 33 | } 34 | 35 | } 36 | /* 37 | OUTPUT 38 | Enter any positive number :: 123 39 | Reverse of the given number is : 321 40 | 41 | Enter any positive number :: 2019 42 | Reverse of the given number is : 9102 43 | 44 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/numbers/SumOfDigits.java: -------------------------------------------------------------------------------- 1 | package com.java.numbers; 2 | 3 | import java.util.Scanner; 4 | 5 | //This program will calculate the sum of digits of a given number 6 | /* 7 | * say N = 153 8 | * output is = 9 (1 + 5 + 3) 9 | * 10 | * say N = 1986 11 | * output is = 24 (1 + 9 + 8 + 6) 12 | */ 13 | public class SumOfDigits { 14 | 15 | public static void main(String[] args) { 16 | Scanner scanner = new Scanner(System.in); 17 | System.out.println("Please enter any number :: "); 18 | int N = Integer.parseInt(scanner.nextLine().trim()); 19 | int tempN = N; 20 | int sum = 0; 21 | while( N > 0){ 22 | int lastDigit = N %10; 23 | sum += lastDigit; 24 | N = N /10; 25 | } 26 | System.out.println("Sum of digits of "+tempN+" is :: "+sum); 27 | scanner.close(); 28 | } 29 | } 30 | 31 | /* 32 | OUTPUT 33 | 34 | Please enter any number :: 35 | 1986 36 | Sum of digits of :: 1986 is :: 24 37 | 38 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/parentheses/GenerateBalancedParentheses.java: -------------------------------------------------------------------------------- 1 | package com.java.parentheses; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Generate Balanced Parentheses 7 | * 8 | * Given n pairs of parentheses 9 | * Write a java program to generate all combinations of 10 | * well-formed or balanced parentheses 11 | * 12 | * input 1 13 | * () 14 | * 15 | * input 2 16 | * ()() 17 | * (()) 18 | * 19 | */ 20 | public class GenerateBalancedParentheses { 21 | public static void main(String[] args) { 22 | Scanner scanner = new Scanner(System.in); 23 | int N = scanner.nextInt(); 24 | generateParentheses(0, 0, "",N); 25 | scanner.close(); 26 | } 27 | 28 | public static void generateParentheses(int openCount,int closeCount,String parentheses,int N){ 29 | if(closeCount == N){ 30 | System.out.println(parentheses); 31 | return; 32 | } 33 | if(openCount < N) 34 | generateParentheses(openCount+1, closeCount, parentheses+"(",N); 35 | if(openCount > closeCount) 36 | generateParentheses(openCount, closeCount+1, parentheses+")",N); 37 | } 38 | } 39 | /* 40 | INPUT 41 | 3 42 | 43 | OUTPUT 44 | ((())) 45 | (()()) 46 | (())() 47 | ()(()) 48 | ()()() 49 | 50 | INPUT 51 | 4 52 | 53 | OUTPUT 54 | (((()))) 55 | ((()())) 56 | ((())()) 57 | ((()))() 58 | (()(())) 59 | (()()()) 60 | (()())() 61 | (())(()) 62 | (())()() 63 | ()((())) 64 | ()(()()) 65 | ()(())() 66 | ()()(()) 67 | ()()()() 68 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/FloydsTriangle.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | 3 | import java.util.Scanner; 4 | /* 5 | * Floyd's Triangle 6 | * 7 | * Floyd's triangle is a right-angled triangular array of natural numbers, 8 | * used in computer science education. It is named after Robert Floyd. 9 | * It is defined by filling the rows of the triangle with consecutive numbers, 10 | * starting with a 1 in the top left corner: 11 | 1 12 | 2 3 13 | 4 5 6 14 | 7 8 9 10 15 | 11 12 13 14 15 16 | 16 17 18 19 20 21 17 | 22 23 24 25 26 27 28 18 | 29 30 31 32 33 34 35 36 19 | 37 38 39 40 41 42 43 44 45 20 | 46 47 48 49 50 51 52 53 54 55 21 | */ 22 | public class FloydsTriangle { 23 | public static void main(String[] args) { 24 | Scanner scanner = new Scanner(System.in); 25 | System.out.println("Enter the number of rows of Floyd's Triangle to print :: "); 26 | int rows = Integer.parseInt(scanner.nextLine().trim()); 27 | 28 | //logic to print the Floyd's triangle 29 | int value = 1; 30 | for(int i=0;i=1;i--){ 19 | for(int j=0;j<2*(N-i);j++) 20 | System.out.print(" "); 21 | for(int j=0;j<(2*i)-1;j++) 22 | if( j == 0) 23 | System.out.print("*"); 24 | else 25 | System.out.print(" *"); 26 | if(i > 1 ) 27 | System.out.println(); 28 | } 29 | scanner.close(); 30 | } 31 | } 32 | /* 33 | OUTPUT 34 | Enter the number of rows to print the pattern :: 5 35 | 5 5 5 5 5 5 5 5 5 36 | 4 4 4 4 4 4 4 37 | 3 3 3 3 3 38 | 2 2 2 39 | 1 40 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern11.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | Write a Java Program to print the following Pattern 7 | 1 1 1 1 1 8 | 2 2 2 2 2 9 | 3 3 3 3 3 10 | 4 4 4 4 4 11 | 5 5 5 5 5 12 | */ 13 | public class Pattern11 { 14 | public static void main(String[] args) { 15 | Scanner scanner = new Scanner(System.in); 16 | System.out.println("Enter the number of rows to print the pattern :: "); 17 | int N = Integer.parseInt(scanner.nextLine().trim()); 18 | for(int i=0;i=0;j--) 27 | System.out.print(" "+CHARS.charAt(j)); 28 | d-=2; 29 | if(i < N-1) 30 | System.out.println(); 31 | } 32 | scanner.close(); 33 | } 34 | } 35 | /* 36 | Input 37 | 5 38 | Output 39 | A 40 | A B A 41 | A B C B A 42 | A B C D C B A 43 | A B C D E D C B A 44 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern14.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | 4 | /* 5 | Write the program to print the following pattern 6 | 1 7 | 1 2 1 8 | 1 2 3 2 1 9 | 1 2 3 4 3 2 1 10 | 1 2 3 4 5 4 3 2 1 11 | */ 12 | public class Pattern14 { 13 | public static void main(String args[] ) throws Exception { 14 | Scanner scanner = new Scanner(System.in); 15 | int N = Integer.parseInt(scanner.nextLine().trim()); 16 | int d = (N-1)*2; 17 | for(int i=0;i=0;j--) 26 | System.out.print(" "+(j+1)); 27 | d-=2; 28 | if(i < N-1) 29 | System.out.println(); 30 | } 31 | scanner.close(); 32 | } 33 | } 34 | /* 35 | Input 36 | 5 37 | Output 38 | 1 39 | 1 2 1 40 | 1 2 3 2 1 41 | 1 2 3 4 3 2 1 42 | 1 2 3 4 5 4 3 2 1 43 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern15.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | A 6 | B A B 7 | C B A B C 8 | D C B A B C D 9 | E D C B A B C D E 10 | */ 11 | public class Pattern15 { 12 | public static void main(String[] args) { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine().trim()); 15 | String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | int d = (N-1)*2; 17 | for(int i=0;i=0;j--) 21 | if(i == N-1 && j == i) 22 | System.out.print(CHARS.charAt(j)); 23 | else 24 | System.out.print(" "+CHARS.charAt(j)); 25 | for(int j=1;j<=i;j++) 26 | System.out.print(" "+CHARS.charAt(j)); 27 | if(i==0 && N != 1) 28 | System.out.print(" "); 29 | d-=2; 30 | if(i < N-1) 31 | System.out.println(); 32 | } 33 | scanner.close(); 34 | } 35 | } 36 | /* 37 | Input 38 | 5 39 | Output 40 | A 41 | B A B 42 | C B A B C 43 | D C B A B C D 44 | E D C B A B C D E 45 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern16.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | 0 6 | 1 0 1 7 | 2 1 0 1 2 8 | 3 2 1 0 1 2 3 9 | 4 3 2 1 0 1 2 3 4 10 | */ 11 | public class Pattern16 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine().trim()); 15 | int d = (N-1)*2; 16 | for(int i=0;i=0;j--) 20 | if(i == N-1 && j == i) 21 | System.out.print(j); 22 | else 23 | System.out.print(" "+j); 24 | for(int j=1;j<=i;j++) 25 | System.out.print(" "+j); 26 | if(i==0 && N != 1) 27 | System.out.print(" "); 28 | d-=2; 29 | if(i < N-1) 30 | System.out.println(); 31 | } 32 | scanner.close(); 33 | } 34 | } 35 | /* 36 | Input 37 | 5 38 | Output 39 | 0 40 | 1 0 1 41 | 2 1 0 1 2 42 | 3 2 1 0 1 2 3 43 | 4 3 2 1 0 1 2 3 4 44 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern17.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | A 6 | C B A 7 | E D C B A 8 | G F E D C B A 9 | I H G F E D C B A 10 | */ 11 | public class Pattern17 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | int iSpace = 0; 17 | for(int i=0;i=0;j--) 21 | if(j == 2*i) 22 | System.out.print(s.charAt(j)); 23 | else 24 | System.out.print(" "+s.charAt(j)); 25 | iSpace++; 26 | if(i != N-1) 27 | System.out.println(); 28 | } 29 | scanner.close(); 30 | } 31 | } 32 | /* 33 | Input 34 | 5 35 | Output 36 | A 37 | C B A 38 | E D C B A 39 | G F E D C B A 40 | I H G F E D C B A 41 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern18.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | A 6 | A B C 7 | A B C D E 8 | A B C D E F G 9 | A B C D E F G H I 10 | */ 11 | public class Pattern18 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | int iSpace = 0; 17 | for(int i=0;i=1;j--) 20 | if(j == 2*i+1) 21 | System.out.print(j); 22 | else 23 | System.out.print(" "+j); 24 | iSpace++; 25 | if(i != N-1) 26 | System.out.println(); 27 | } 28 | scanner.close(); 29 | } 30 | } 31 | /* 32 | Input 33 | 5 34 | Output 35 | 1 36 | 3 2 1 37 | 5 4 3 2 1 38 | 7 6 5 4 3 2 1 39 | 9 8 7 6 5 4 3 2 1 40 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern2.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Write a Java Program to print the following Pattern 7 | 8 | 1 9 | 2 2 10 | 3 3 3 11 | 4 4 4 4 12 | 5 5 5 5 5 13 | 6 6 6 6 6 6 14 | */ 15 | 16 | public class Pattern2 { 17 | public static void main(String[] args) { 18 | Scanner scanner = new Scanner(System.in); 19 | System.out.println("Enter the number of rows to print the pattern :: "); 20 | int N = Integer.parseInt(scanner.nextLine().trim()); 21 | 22 | //logic to print the pattern 23 | for(int i=1;i<=N;i++){ 24 | for(int j=1;j<=i;j++) 25 | System.out.print(i+" "); 26 | System.out.println(); 27 | } 28 | 29 | scanner.close(); 30 | } 31 | } 32 | /* 33 | OUTPUT 34 | Enter the number of rows to print the pattern :: 10 35 | 1 36 | 2 2 37 | 3 3 3 38 | 4 4 4 4 39 | 5 5 5 5 5 40 | 6 6 6 6 6 6 41 | 7 7 7 7 7 7 7 42 | 8 8 8 8 8 8 8 8 43 | 9 9 9 9 9 9 9 9 9 44 | 10 10 10 10 10 10 10 10 10 10 45 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern20.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | 1 6 | 1 2 3 7 | 1 2 3 4 5 8 | 1 2 3 4 5 6 7 9 | 1 2 3 4 5 6 7 8 9 10 | */ 11 | public class Pattern20 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | int iSpace = 0; 16 | for(int i=0;i=i; j--) 24 | System.out.print(j+" "); 25 | System.out.println(); 26 | } 27 | 28 | scanner.close(); 29 | } 30 | } 31 | 32 | /* 33 | OUTPUT 34 | 35 | Enter the number of rows to print the pattern :: 10 36 | 10 9 8 7 6 5 4 3 2 1 37 | 10 9 8 7 6 5 4 3 2 38 | 10 9 8 7 6 5 4 3 39 | 10 9 8 7 6 5 4 40 | 10 9 8 7 6 5 41 | 10 9 8 7 6 42 | 10 9 8 7 43 | 10 9 8 44 | 10 9 45 | 10 46 | 47 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern30.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | * * * * * 6 | * * * * 7 | * * * 8 | * * 9 | * 10 | */ 11 | public class Pattern30 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | for(int i=0;i=i;j--) 18 | if(j == N-1) 19 | System.out.print(s.charAt(j)); 20 | else 21 | System.out.print(" "+s.charAt(j)); 22 | if(i != N-1) 23 | System.out.println(); 24 | } 25 | scanner.close(); 26 | } 27 | } 28 | /* 29 | Input 30 | 5 31 | Output 32 | E D C B A 33 | E D C B 34 | E D C 35 | E D 36 | E 37 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern37.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | E E E E E 6 | D D D D 7 | C C C 8 | B B 9 | A 10 | */ 11 | public class Pattern37 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | for(int i=0;i=i;j--) 18 | if(j == N-1) 19 | System.out.print(s.charAt(N-i-1)); 20 | else 21 | System.out.print(" "+s.charAt(N-i-1)); 22 | if(i != N-1) 23 | System.out.println(); 24 | } 25 | scanner.close(); 26 | } 27 | } 28 | /* 29 | Input 30 | 5 31 | Output 32 | E E E E E 33 | D D D D 34 | C C C 35 | B B 36 | A 37 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern38.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | 5 4 3 2 1 6 | 5 4 3 2 7 | 5 4 3 8 | 5 4 9 | 5 10 | */ 11 | public class Pattern38 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | for(int i=0;ii;j--) 17 | if(j == N) 18 | System.out.print(j); 19 | else 20 | System.out.print(" "+j); 21 | if(i != N-1) 22 | System.out.println(); 23 | } 24 | scanner.close(); 25 | } 26 | } 27 | /* 28 | Input 29 | 5 30 | Output 31 | 5 4 3 2 1 32 | 5 4 3 2 33 | 5 4 3 34 | 5 4 35 | 5 36 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern39.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | 5 5 5 5 5 6 | 4 4 4 4 7 | 3 3 3 8 | 2 2 9 | 1 10 | */ 11 | public class Pattern39 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | for(int i=0;i=1;i--){ 22 | for(int j=1;j<=i;j++) 23 | System.out.print(j+" "); 24 | System.out.println(); 25 | } 26 | 27 | scanner.close(); 28 | } 29 | } 30 | /* 31 | OUTPUT 32 | 33 | Enter the number of rows to print the pattern :: 10 34 | 1 2 3 4 5 6 7 8 9 10 35 | 1 2 3 4 5 6 7 8 9 36 | 1 2 3 4 5 6 7 8 37 | 1 2 3 4 5 6 7 38 | 1 2 3 4 5 6 39 | 1 2 3 4 5 40 | 1 2 3 4 41 | 1 2 3 42 | 1 2 43 | 1 44 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern40.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | A B C D E 6 | A B C D 7 | A B C 8 | A B 9 | A 10 | */ 11 | public class Pattern40 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | for(int i=0;i=0;j--) 18 | if(j == 4) 19 | System.out.print(s.charAt(j)); 20 | else 21 | System.out.print(" "+s.charAt(j)); 22 | if(i != N-1) 23 | System.out.println(); 24 | } 25 | scanner.close(); 26 | } 27 | } 28 | /* 29 | Input 30 | 5 31 | Output 32 | E D C B A 33 | E D C B A 34 | E D C B A 35 | E D C B A 36 | E D C B A 37 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern47.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | E E E E E 6 | D D D D D 7 | C C C C C 8 | B B B B B 9 | A A A A A 10 | */ 11 | public class Pattern47 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | for(int i=N-1;i>=0;i--){ 17 | for(int j=0;j<5;j++) 18 | if(j == 0) 19 | System.out.print(s.charAt(i)); 20 | else 21 | System.out.print(" "+s.charAt(i)); 22 | if(i != 0) 23 | System.out.println(); 24 | } 25 | scanner.close(); 26 | } 27 | } 28 | /* 29 | Input 30 | 5 31 | Output 32 | E E E E E 33 | D D D D D 34 | C C C C C 35 | B B B B B 36 | A A A A A 37 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern48.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | 5 4 3 2 1 6 | 5 4 3 2 1 7 | 5 4 3 2 1 8 | 5 4 3 2 1 9 | 5 4 3 2 1 10 | */ 11 | public class Pattern48 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | for(int i=0;i0;j--) 17 | if(j == 5) 18 | System.out.print(j); 19 | else 20 | System.out.print(" "+j); 21 | if(i != N-1) 22 | System.out.println(); 23 | } 24 | scanner.close(); 25 | } 26 | } 27 | /* 28 | Input 29 | 5 30 | Output 31 | 5 4 3 2 1 32 | 5 4 3 2 1 33 | 5 4 3 2 1 34 | 5 4 3 2 1 35 | 5 4 3 2 1 36 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern49.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | 5 5 5 5 5 6 | 4 4 4 4 4 7 | 3 3 3 3 3 8 | 2 2 2 2 2 9 | 1 1 1 1 1 10 | */ 11 | public class Pattern49 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | for(int i=N;i>0;i--){ 16 | for(int j=0;j<5;j++) 17 | if(j == 0) 18 | System.out.print(i); 19 | else 20 | System.out.print(" "+i); 21 | if(i != 1) 22 | System.out.println(); 23 | } 24 | scanner.close(); 25 | } 26 | } 27 | /* 28 | Input 29 | 5 30 | Output 31 | 5 5 5 5 5 32 | 4 4 4 4 4 33 | 3 3 3 3 3 34 | 2 2 2 2 2 35 | 1 1 1 1 1 36 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern5.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Write a Java Program to print the following Pattern 7 | 1 8 | 1 2 1 9 | 1 2 3 2 1 10 | 1 2 3 4 3 2 1 11 | 1 2 3 4 5 4 3 2 1 12 | 1 2 3 4 5 6 5 4 3 2 1 13 | */ 14 | public class Pattern5 { 15 | public static void main(String[] args) { 16 | Scanner scanner = new Scanner(System.in); 17 | System.out.println("Enter the number of rows to print the pattern :: "); 18 | int N = Integer.parseInt(scanner.nextLine().trim()); 19 | 20 | for(int i=1;i<=N;i++){ 21 | for(int j=1;j<=i;j++) 22 | System.out.print(j+" "); 23 | for(int j=i-1;j>=1;j--) 24 | System.out.print(j+" "); 25 | System.out.println(); 26 | } 27 | 28 | scanner.close(); 29 | } 30 | } 31 | /* 32 | OUTPUT 33 | 34 | Enter the number of rows to print the pattern :: 10 35 | 1 36 | 1 2 1 37 | 1 2 3 2 1 38 | 1 2 3 4 3 2 1 39 | 1 2 3 4 5 4 3 2 1 40 | 1 2 3 4 5 6 5 4 3 2 1 41 | 1 2 3 4 5 6 7 6 5 4 3 2 1 42 | 1 2 3 4 5 6 7 8 7 6 5 4 3 2 1 43 | 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 44 | 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1 45 | 46 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern50.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | import java.util.Scanner; 3 | /* 4 | Write the program to print the following pattern 5 | A B C D E 6 | A B C D E 7 | A B C D E 8 | A B C D E 9 | A B C D E 10 | */ 11 | public class Pattern50 { 12 | public static void main(String args[] ) throws Exception { 13 | Scanner scanner = new Scanner(System.in); 14 | int N = Integer.parseInt(scanner.nextLine()); 15 | String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 16 | for(int i=0;i=1;i--){ 26 | for(int j=1;j<=i;j++) 27 | System.out.print(j+" "); 28 | System.out.println(); 29 | } 30 | for(int i=2; i<=N; i++){ 31 | for(int j=1;j<=i;j++) 32 | System.out.print(j+" "); 33 | System.out.println(); 34 | } 35 | 36 | scanner.close(); 37 | } 38 | } 39 | /* 40 | OUTPUT 41 | 42 | Enter the number of rows to print the pattern :: 10 43 | 1 2 3 4 5 6 7 8 9 10 44 | 1 2 3 4 5 6 7 8 9 45 | 1 2 3 4 5 6 7 8 46 | 1 2 3 4 5 6 7 47 | 1 2 3 4 5 6 48 | 1 2 3 4 5 49 | 1 2 3 4 50 | 1 2 3 51 | 1 2 52 | 1 53 | 1 2 54 | 1 2 3 55 | 1 2 3 4 56 | 1 2 3 4 5 57 | 1 2 3 4 5 6 58 | 1 2 3 4 5 6 7 59 | 1 2 3 4 5 6 7 8 60 | 1 2 3 4 5 6 7 8 9 61 | 1 2 3 4 5 6 7 8 9 10 62 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern7.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Write a Java Program to print the following Pattern 7 | 1 2 3 4 5 6 8 | 2 3 4 5 6 9 | 3 4 5 6 10 | 4 5 6 11 | 5 6 12 | 6 13 | 5 6 14 | 4 5 6 15 | 3 4 5 6 16 | 2 3 4 5 6 17 | 1 2 3 4 5 6 18 | */ 19 | public class Pattern7 { 20 | public static void main(String[] args) { 21 | Scanner scanner = new Scanner(System.in); 22 | System.out.println("Enter the number of rows to print the pattern :: "); 23 | int N = Integer.parseInt(scanner.nextLine().trim()); 24 | 25 | for(int i=1;i<=N;i++){ 26 | for(int k=1;k=1; i--){ 33 | for(int k=1;k=1;j--) 22 | System.out.print(j+" "); 23 | System.out.println(); 24 | } 25 | 26 | scanner.close(); 27 | } 28 | } 29 | /* 30 | OUTPUT 31 | 32 | Enter the number of rows to print the pattern :: 10 33 | 1 34 | 2 1 35 | 3 2 1 36 | 4 3 2 1 37 | 5 4 3 2 1 38 | 6 5 4 3 2 1 39 | 7 6 5 4 3 2 1 40 | 8 7 6 5 4 3 2 1 41 | 9 8 7 6 5 4 3 2 1 42 | 10 9 8 7 6 5 4 3 2 1 43 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/patterns/Pattern9.java: -------------------------------------------------------------------------------- 1 | package com.java.patterns; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Write a Java Program to print the following Pattern 7 | 1 8 | 1 0 9 | 1 0 1 10 | 1 0 1 0 11 | 1 0 1 0 1 12 | 1 0 1 0 1 0 13 | */ 14 | public class Pattern9 { 15 | public static void main(String[] args) { 16 | Scanner scanner = new Scanner(System.in); 17 | System.out.println("Enter the number of rows to print the pattern :: "); 18 | int N = Integer.parseInt(scanner.nextLine().trim()); 19 | 20 | for(int i=1;i<=N;i++){ 21 | for(int j=1;j<=i;j++) 22 | System.out.print((j%2)+" "); 23 | System.out.println(); 24 | } 25 | 26 | scanner.close(); 27 | } 28 | } 29 | /* 30 | OUTPUT 31 | 32 | Enter the number of rows to print the pattern :: 10 33 | 1 34 | 1 0 35 | 1 0 1 36 | 1 0 1 0 37 | 1 0 1 0 1 38 | 1 0 1 0 1 0 39 | 1 0 1 0 1 0 1 40 | 1 0 1 0 1 0 1 0 41 | 1 0 1 0 1 0 1 0 1 42 | 1 0 1 0 1 0 1 0 1 0 43 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/AlternateTenSeries.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | /* 4 | * Write the program to print the following series 5 | * 6 | * 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10 7 | * 8 | * Idea is very simple 9 | * Have only one for loop 10 | * 11 | * variable starts with 1 12 | * one variable increment by 1 in every iteration 13 | * 14 | * variable starts with 10 15 | * another variable decrement by 1 in every iteration 16 | */ 17 | public class AlternateTenSeries { 18 | public static void main(String[] args) { 19 | int a = 10; 20 | int b = 1; 21 | System.out.print("The Series is : "); 22 | for(int i=0;i<10;i++,a--,b++) 23 | System.out.print(a+" "+b+" "); 24 | 25 | System.out.print("\nAlternate approach : "); 26 | a = 10; 27 | for(int i=0;i<10;i++) 28 | System.out.print((a-i)+" "+(i+1)+" "); 29 | } 30 | } 31 | /* 32 | OUTPUT 33 | 34 | The Series is : 35 | 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10 36 | 37 | Alternate approach : 38 | 10 1 9 2 8 3 7 4 6 5 5 6 4 7 3 8 2 9 1 10 39 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/EvenNumberSeries.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * EVEN Number Series 7 | * ------------------ 8 | * 0 2 4 6 8 10 12 14 .... 9 | * 10 | * In the for loop, either use i++, check i is even and print 11 | * Else in the for loop use i+=2 then print i value 12 | * i starts with value 0 13 | */ 14 | public class EvenNumberSeries { 15 | public static void main(String[] args) { 16 | Scanner scanner = new Scanner(System.in); 17 | System.out.println("Enter the N value : "); 18 | int N = scanner.nextInt(); 19 | 20 | System.out.println("Even numbers up to "+N); 21 | for(int i=0;i<=N;i+=2) 22 | System.out.print(i+" "); 23 | 24 | scanner.close(); 25 | } 26 | } 27 | 28 | /* 29 | OUTPUT 30 | Enter the N value : 25 31 | Even numbers up to 25 32 | 0 2 4 6 8 10 12 14 16 18 20 22 24 33 | 34 | Enter the N value : 14 35 | Even numbers up to 14 36 | 0 2 4 6 8 10 12 14 37 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/FibonacciSeries.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | /* 4 | * Fibonacci Series 5 | * ---------------- 6 | * In mathematics, the Fibonacci numbers, 7 | * commonly denoted Fn, form a sequence, 8 | * called the Fibonacci sequence, such that 9 | * each number is the sum of the 10 | * two preceding ones, starting from 0 and 1. 11 | * 12 | * In other words, the previous two numbers 13 | * are added to get the next number, starting from 0 & 1. 14 | * 15 | * The Fibonacci Sequence is the series 16 | * of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ... 17 | * 18 | */ 19 | 20 | public class FibonacciSeries { 21 | public static void main(String[] args) { 22 | int N = 10; 23 | int num1 = 0; 24 | int num2 = 1; 25 | 26 | System.out.println("Fibonacci series N="+N); 27 | System.out.print(num1+" "+num2); 28 | int i = 2; 29 | while(i <= N){ 30 | i++; 31 | int temp = num1+num2; 32 | num1 = num2; 33 | num2 = temp; 34 | System.out.print(" "+num2); 35 | } 36 | } 37 | } 38 | /* 39 | OUTPUT 40 | Fibonacci series N=15 41 | 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 42 | 43 | Fibonacci series N=10 44 | 0 1 1 2 3 5 8 13 21 34 55 45 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/GeometricProgression.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/InterviewPrograms/src/com/java/series/GeometricProgression.java -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/OddNumberSeries.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * ODD Number Series 7 | * ------------------ 8 | * 1 3 5 7 9 11 13 15 17 19 ..... 9 | * 10 | * In the for loop, either use i++, check i is odd and print 11 | * Else in the for loop use i+=2 then print i value 12 | * i starts with value 1 13 | */ 14 | 15 | public class OddNumberSeries { 16 | public static void main(String[] args) { 17 | Scanner scanner = new Scanner(System.in); 18 | System.out.println("Enter the N value :"); 19 | int N = scanner.nextInt(); 20 | 21 | System.out.println("ODD numbers up to "+N); 22 | for(int i=1;i<=N;i+=2) 23 | System.out.print(i+" "); 24 | 25 | scanner.close(); 26 | } 27 | } 28 | /* 29 | OUTPUT 30 | 31 | Enter the N value : 20 32 | ODD numbers up to 20 33 | 1 3 5 7 9 11 13 15 17 19 34 | 35 | Enter the N value : 13 36 | ODD numbers up to 13 37 | 1 3 5 7 9 11 13 38 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/PatternSeries.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | import java.util.Scanner; 4 | /* 5 | * Write the program to print the following series 6 | * 7 | * 3 33 333 3333 33333 333333 ..... 8 | * 9 | * Best approach 10 | * Use String variable and starts with 3, 11 | * and append 3 for every iteration 12 | * 13 | */ 14 | 15 | public class PatternSeries { 16 | public static void main(String[] args) { 17 | Scanner scanner = new Scanner(System.in); 18 | System.out.println("Enter the N value for the series : "); 19 | int N = scanner.nextInt(); 20 | 21 | int baseValue = 3; 22 | int result = 0; 23 | for(int i=0;i 2^0 = 1 12 | * i = 1 => 2^1 = 2 13 | * i = 2 => 2^2 = 4 14 | * i = 3 => 2^3 = 8 15 | * i = 4 => 2^4 = 16 16 | * i = 5 => 2^5 = 32 17 | * i = 6 => 2^6 = 64 18 | * i = 7 => 2^7 = 128 19 | * i = 8 => 2^8 = 256 20 | * i = 9 => 2^9 = 512 21 | * i = 10 => 2^10 = 1024 22 | * 23 | */ 24 | public class PowersOf2 { 25 | public static void main(String[] args) { 26 | int n = 5; 27 | System.out.println("The Series is : "); 28 | for(int i=0; i<=n; i++){ 29 | int value = (int)Math.pow(2, i); 30 | System.out.print(value+", "); 31 | } 32 | } 33 | } 34 | /* 35 | OUTPUT 36 | n = 10 37 | The Series is : 38 | 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 39 | 40 | OUTPUT 41 | n = 5 42 | The Series is : 43 | 1, 2, 4, 8, 16, 32 44 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/PrimeNumberSeries.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | /* 4 | * Prime Number Series 5 | * -------------------- 6 | * 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 .... 7 | * 8 | * Prime Number 9 | * ------------ 10 | * A prime number is a natural number greater than 1 11 | * that is not a product of two smaller natural numbers. 12 | * 13 | * A natural number thats divisible only by itself and 1. 14 | * 15 | */ 16 | 17 | public class PrimeNumberSeries { 18 | public static void main(String[] args) { 19 | int n = 100; 20 | System.out.println("Prime numbers up to : "+n); 21 | for(int i=2; i<=n; i++) 22 | if(isPrime(i)) 23 | System.out.print(i+", "); 24 | } 25 | public static boolean isPrime(int num){ 26 | for(int i=2;i<=Math.sqrt(num);i++) 27 | if(num % i == 0) 28 | return false; 29 | return true; 30 | } 31 | } 32 | /* 33 | OUTPUT 34 | n = 20 35 | Prime numbers up to : 20 36 | 2, 3, 5, 7, 11, 13, 17, 19 37 | 38 | OUTPUT 39 | n = 100 40 | Prime numbers up to : 100 41 | 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 42 | 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 43 | 44 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/QuadraticSequence.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | /* 4 | * Quadratic Sequence 5 | * ------------------ 6 | * A quadratic sequence is a sequence of numbers 7 | * in which the second difference between 8 | * any two consecutive terms is constant. 9 | * 10 | * Say the series 11 | * 1 2 4 7 11 16 22 29 ..... 12 | * 13 | * It is simple to calculate the next term 14 | * ith term = (i-1)th term + (i-1) 15 | * where i starts from 1 16 | * 17 | * term 1 : 1 18 | * term 2 = 1 + 1 = 2 19 | * term 3 = 2 + 2 = 4 20 | * term 4 = 4 + 3 = 7 21 | * term 5 = 7 + 4 = 11 22 | * term 6 = 11 + 5 = 16 23 | * term 7 = 16 + 6 = 22 24 | * 25 | * The Series is 26 | * 1, 2, 4, 7, 11, 16, 22 27 | * 28 | * for values i = 1 to n 29 | * 1 = ( (1 * (1-1) ) / 2) + 1 = 1 30 | * 2 = ( (2 * (2-1) ) / 2) + 1 = 2 31 | * 3 = ( (3 * (3-1) ) / 2) + 1 = 4 32 | * 4 = ( (4 * (4-1) ) / 2) + 1 = 7 33 | * 5 = ( (5 * (5-1) ) / 2) + 1 = 11 34 | * 6 = ( (6 * (6-1) ) / 2) + 1 = 16 35 | * 36 | */ 37 | public class QuadraticSequence { 38 | public static void main(String[] args) { 39 | int n = 15; 40 | System.out.println("The Series is : "); 41 | int prevTerm = 1; 42 | System.out.print(prevTerm+", "); 43 | for(int i=2; i<=n; i++){ 44 | prevTerm = prevTerm + i-1; 45 | System.out.print(prevTerm+", "); 46 | } 47 | } 48 | } 49 | /* 50 | OUTPUT 51 | n = 10 52 | The Series is : 53 | 1, 2, 4, 7, 11, 16, 22, 29, 37, 46 54 | 55 | OUTPUT 56 | n = 15 57 | The Series is : 58 | 1, 2, 4, 7, 11, 16, 22, 29, 37, 46, 56, 67, 79, 92, 106 59 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/Series1.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | /* 4 | * Print the following Series 5 | * 2 9 28 65 126 217 344 6 | * ------------------------- 7 | * for i-th element 8 | * (i^3)+1 9 | * 10 | * i = 1 => 1^3+1 = 1 + 1 = 2 11 | * i = 2 => 2^3+1 = 8 + 1 = 9 12 | * i = 3 => 3^3+1 = 27 + 1 = 28 13 | * i = 4 => 4^3+1 = 64 + 1 = 65 14 | * i = 5 => 5^3+1 = 125 + 1 = 126 15 | * i = 6 => 6^3+1 = 216 + 1 = 217 16 | * i = 7 => 7^3+1 = 343 + 1 = 344 17 | * 18 | */ 19 | 20 | public class Series1 { 21 | public static void main(String[] args) { 22 | int n = 10; 23 | System.out.println("The Series is : "); 24 | for(int i=1;i<=n;i++){ 25 | int value = (i*i*i) + 1; 26 | System.out.print(value+", "); 27 | } 28 | } 29 | } 30 | /* 31 | OUTPUT 32 | n = 10 33 | The Series is : 34 | 2, 9, 28, 65, 126, 217, 344, 513, 730, 1001 35 | 36 | OUTPUT 37 | n = 5 38 | The Series is : 39 | 2, 9, 28, 65, 126 40 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/series/Series5.java: -------------------------------------------------------------------------------- 1 | package com.java.series; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Write Java program to generate the following Series 7 | * 8 | * 1 2 4 7 11 16 22 29 9 | * 10 | * Solution is 11 | * i-th value = ( (i * (i-1) ) / 2) + 1 12 | * 13 | * for values i = 1 to n 14 | * 1 = ( (1 * (1-1) ) / 2) + 1 = 1 15 | * 2 = ( (2 * (2-1) ) / 2) + 1 = 2 16 | * 3 = ( (3 * (3-1) ) / 2) + 1 = 4 17 | * 4 = ( (4 * (4-1) ) / 2) + 1 = 7 18 | * 5 = ( (5 * (5-1) ) / 2) + 1 = 11 19 | * 6 = ( (6 * (6-1) ) / 2) + 1 = 16 20 | */ 21 | public class Series5 { 22 | public static void main(String[] args) { 23 | Scanner scanner = new Scanner(System.in); 24 | System.out.println("Enter the value (N) for the series : "); 25 | int N = scanner.nextInt(); 26 | 27 | if(N <= 0){ 28 | System.out.println("Cannot generate the Series!"); 29 | System.exit(1); 30 | } 31 | 32 | for(int i=1;i<=N;i++){ 33 | int result = ((i*(i-1))/2)+1; 34 | System.out.print(result+" "); 35 | } 36 | 37 | scanner.close(); 38 | } 39 | } 40 | /* 41 | TEST CASE 1 42 | Enter the value (N) for the series : 10 43 | 1 2 4 7 11 16 22 29 37 46 44 | 45 | TEST CASE 2 46 | Enter the value (N) for the series : 15 47 | 1 2 4 7 11 16 22 29 37 46 56 67 79 92 106 48 | */ -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/singleton/Account.java: -------------------------------------------------------------------------------- 1 | package com.java.singleton; 2 | 3 | public class Account { 4 | 5 | private int balance; 6 | private static Account instance = null; 7 | 8 | private Account(){ 9 | balance = 0; 10 | } 11 | 12 | public static Account getInstance(){ 13 | if( instance == null) 14 | instance = new Account(); 15 | return instance; 16 | } 17 | 18 | public void setBalance(int balance) { 19 | this.balance = balance; 20 | } 21 | 22 | public void addBalance(int balance){ 23 | this.balance += balance; 24 | } 25 | 26 | public int getBalance() { 27 | return balance; 28 | } 29 | } -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/singleton/SingletonMain.java: -------------------------------------------------------------------------------- 1 | package com.java.singleton; 2 | 3 | public class SingletonMain { 4 | 5 | public static void main(String[] args) { 6 | Account donateAcnt1 = Account.getInstance(); 7 | donateAcnt1.setBalance(10000); 8 | System.out.println("Balance in Donation account is : "+donateAcnt1.getBalance()); 9 | getDonation(); 10 | } 11 | 12 | public static void getDonation(){ 13 | Account account = Account.getInstance(); 14 | account.addBalance(25000); 15 | System.out.println("New balance in Donation account is : "+account.getBalance()); 16 | } 17 | } -------------------------------------------------------------------------------- /InterviewPrograms/src/com/java/strings/CombinationOfString.java: -------------------------------------------------------------------------------- 1 | package com.java.strings; 2 | 3 | import java.util.Scanner; 4 | 5 | /* 6 | * Combination 7 | * ------------ 8 | * 9 | * A combination is a selection of all or part of 10 | * a set of objects, without regard to the order in 11 | * which objects are selected. 12 | * 13 | * For example, suppose we have a set of 14 | * three letters: A, B, and C. We might ask how many ways 15 | * we can select 2 letters from that set. 16 | * Each possible selection would be an example of a combination. 17 | * The complete list of possible selections 18 | * would be: AB, AC, and BC. 19 | * 20 | * When the order doesn't matter, it is a Combination. 21 | * When the order does matter it is a Permutation. 22 | * 23 | * Order does matter 24 | 1 2 3 25 | 1 3 2 26 | 2 1 3 27 | 2 3 1 28 | 3 1 2 29 | 3 2 1 30 | 31 | */ 32 | 33 | public class CombinationOfString { 34 | public static void main(String[] args) { 35 | Scanner scanner = new Scanner(System.in); 36 | System.out.println("Enter any String : "); 37 | String s = scanner.nextLine().trim(); 38 | System.out.println("All Combinations are :: "); 39 | combination("", s,0); 40 | scanner.close(); 41 | } 42 | 43 | public static void combination(String c,String s,int curIndex){ 44 | for(int i=curIndex;i 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /JavaEnums/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | JavaEnums 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /JavaEnums/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /JavaEnums/bin/com/cdac/java/eum/Directions.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/JavaEnums/bin/com/cdac/java/eum/Directions.class -------------------------------------------------------------------------------- /JavaEnums/bin/com/cdac/java/eum/Level.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/JavaEnums/bin/com/cdac/java/eum/Level.class -------------------------------------------------------------------------------- /JavaEnums/bin/com/cdac/java/eum/MainProgram.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/JavaEnums/bin/com/cdac/java/eum/MainProgram.class -------------------------------------------------------------------------------- /JavaEnums/bin/com/cdac/java/eum/TrafficSignal.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/JavaEnums/bin/com/cdac/java/eum/TrafficSignal.class -------------------------------------------------------------------------------- /JavaEnums/src/com/cdac/java/eum/Directions.java: -------------------------------------------------------------------------------- 1 | package com.cdac.java.eum; 2 | 3 | public enum Directions{ 4 | 5 | LEFT(1), 6 | RIGHT(2), 7 | UP(3), 8 | DOWN(4); 9 | 10 | private final int dirCode; 11 | private Directions(int dirCode){ 12 | this.dirCode = dirCode; 13 | System.out.println("Constructor called "+this.toString()); 14 | } 15 | 16 | public int getDirCode(){ 17 | return dirCode; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /JavaEnums/src/com/cdac/java/eum/Level.java: -------------------------------------------------------------------------------- 1 | package com.cdac.java.eum; 2 | 3 | public enum Level { 4 | HIGH,MEDIUM,LOW; 5 | } -------------------------------------------------------------------------------- /JavaEnums/src/com/cdac/java/eum/MainProgram.java: -------------------------------------------------------------------------------- 1 | package com.cdac.java.eum; 2 | 3 | import java.util.EnumMap; 4 | import java.util.EnumSet; 5 | 6 | public class MainProgram { 7 | 8 | public static void main(String[] args) { 9 | 10 | 11 | /*//Enum Set 12 | EnumSet enumSet = EnumSet.of(Directions.DOWN,Directions.UP); 13 | 14 | //Enum Map 15 | EnumMap map = new EnumMap<>(Directions.class); 16 | map.put(Directions.LEFT, "Left"); 17 | map.put(Directions.RIGHT, "right");*/ 18 | 19 | //returns all the enum constants 20 | for(Directions d : Directions.values()) 21 | System.out.println(d); 22 | 23 | //enum follows the order ( insertion order) 24 | //ordinal method returns the index in the enum constants 25 | Directions d1 = Directions.UP; 26 | int index = d1.ordinal(); 27 | System.out.println(index); 28 | 29 | Directions left = Directions.valueOf("LEFT"); 30 | System.out.println(left); 31 | 32 | System.out.println("Traffic Signals : "); 33 | for(TrafficSignal t : TrafficSignal.values()) 34 | System.out.println(t.name()+" "+t.getAction()+" "+t.getCode()); 35 | } 36 | } -------------------------------------------------------------------------------- /JavaEnums/src/com/cdac/java/eum/TrafficSignal.java: -------------------------------------------------------------------------------- 1 | package com.cdac.java.eum; 2 | 3 | public enum TrafficSignal { 4 | 5 | RED("STOP",1),GREEN("GO",2),ORANGE("SLOW DOWN",3); 6 | 7 | private String action; 8 | private int code; 9 | private TrafficSignal(String action,int code){ 10 | this.action = action; 11 | this.code = code; 12 | } 13 | 14 | public String getAction(){ 15 | return action; 16 | } 17 | 18 | public int getCode(){ 19 | return code; 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /Networking/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Networking/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Networking/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Networking 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Networking/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Networking/src/com/cdac/networking/NetworkInfo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.networking; 2 | 3 | import java.net.InetAddress; 4 | import java.net.MalformedURLException; 5 | import java.net.URL; 6 | import java.net.UnknownHostException; 7 | 8 | public class NetworkInfo { 9 | 10 | public static void main(String[] args) throws UnknownHostException, MalformedURLException { 11 | InetAddress inetAddress = InetAddress.getByName("google.com"); 12 | System.out.println("Host Name : "+inetAddress.getHostName()); 13 | System.out.println("Host Address : "+inetAddress.getHostAddress()); 14 | 15 | inetAddress = InetAddress.getByName("facebook.com"); 16 | System.out.println("Host Name : "+inetAddress.getHostName()); 17 | System.out.println("Host Address : "+inetAddress.getHostAddress()); 18 | 19 | URL url = new URL("http://localhost:8080/testApp"); 20 | System.out.println("Protocol : "+url.getProtocol()); 21 | System.out.println("Host : "+url.getHost()); 22 | System.out.println("Port No : "+url.getPort()); 23 | System.out.println("Path : "+url.getPath()); 24 | System.out.println("Default Port : "+url.getDefaultPort()); 25 | System.out.println("End of the Program"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Networking/src/com/cdac/networking/TCPClient.java: -------------------------------------------------------------------------------- 1 | package com.cdac.networking; 2 | 3 | import java.io.DataInputStream; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | import java.net.Socket; 7 | import java.net.UnknownHostException; 8 | import java.util.Scanner; 9 | 10 | public class TCPClient { 11 | 12 | public static void main(String[] args) throws UnknownHostException, IOException { 13 | Scanner scanner = new Scanner(System.in); 14 | String ipAddress = "localhost"; 15 | int portNumbe = 5500; 16 | Socket socket = new Socket(ipAddress, portNumbe); 17 | 18 | DataInputStream dis = new DataInputStream(socket.getInputStream()); 19 | DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 20 | 21 | String line = ""; 22 | while ( !line.equals( "Stop")) 23 | { 24 | System.out.println("Send msg to Server :: "); 25 | line = scanner.nextLine(); 26 | dos.writeUTF(line); 27 | String strFromServer = dis.readUTF(); 28 | System.out.println("Server says :: "+strFromServer); 29 | } 30 | 31 | dis.close(); 32 | dos.close(); 33 | socket.close(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /Networking/src/com/cdac/networking/TCPServer.java: -------------------------------------------------------------------------------- 1 | package com.cdac.networking; 2 | 3 | import java.io.DataInputStream; 4 | import java.io.DataOutputStream; 5 | import java.io.IOException; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | import java.util.Scanner; 9 | 10 | public class TCPServer { 11 | 12 | public static void main(String[] args) throws IOException { 13 | Scanner scanner = new Scanner(System.in); 14 | int portNumber = 5500; 15 | 16 | ServerSocket server = new ServerSocket(portNumber); 17 | System.out.println("Server is waiting for the client."); 18 | Socket socket = server.accept(); 19 | System.out.println("Client is connected."); 20 | 21 | DataInputStream dis = new DataInputStream(socket.getInputStream()); 22 | DataOutputStream dos = new DataOutputStream(socket.getOutputStream()); 23 | 24 | String line = ""; 25 | while ( !line.equals( "Stop")) 26 | { 27 | String strFromClient = dis.readUTF(); 28 | System.out.println("Client Says :: "+strFromClient); 29 | System.out.println("Enter Server Reply :: "); 30 | line = scanner.nextLine(); 31 | dos.writeUTF(line); 32 | } 33 | 34 | dis.close(); 35 | dos.close(); 36 | server.close(); 37 | socket.close(); 38 | } 39 | } -------------------------------------------------------------------------------- /Reflections/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Reflections/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Reflections 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Reflections/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.7 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.7 12 | -------------------------------------------------------------------------------- /Reflections/bin/com/cdac/Account.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/Reflections/bin/com/cdac/Account.class -------------------------------------------------------------------------------- /Reflections/bin/com/cdac/ReflectionsDemo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sathishmepco/Java-Interview-Programs/bcd06e599916691df03d4574fcd39fd79ee4e297/Reflections/bin/com/cdac/ReflectionsDemo.class -------------------------------------------------------------------------------- /Reflections/src/com/cdac/Account.java: -------------------------------------------------------------------------------- 1 | package com.cdac; 2 | 3 | public class Account { 4 | 5 | private int balance; 6 | public Account() { 7 | } 8 | 9 | public Account(int balance) { 10 | this.balance = balance; 11 | } 12 | 13 | public int getBalance() { 14 | return balance; 15 | } 16 | 17 | public void setBalance(int balance) { 18 | this.balance = balance; 19 | } 20 | } -------------------------------------------------------------------------------- /Reflections/src/com/cdac/ReflectionsDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac; 2 | 3 | import java.lang.reflect.Field; 4 | import java.lang.reflect.InvocationTargetException; 5 | import java.lang.reflect.Method; 6 | 7 | public class ReflectionsDemo { 8 | 9 | public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, SecurityException, 10 | IllegalArgumentException, IllegalAccessException, InvocationTargetException { 11 | 12 | Class c = String.class; 13 | Method[] methods = c.getMethods(); 14 | 15 | System.out.println("Methods in String class :: "); 16 | for (Method m : methods) 17 | System.out.println(m.getName()); 18 | 19 | Account account = new Account(5000); 20 | Class accountClass = Class.forName("com.cdac.reflection.Account"); 21 | methods = accountClass.getMethods(); 22 | System.out.println("Methods in Account class"); 23 | for (Method m : methods) { 24 | System.out.println(m.getName()); 25 | if (m.getName().contains("set")) { 26 | m.invoke(account, 30000); 27 | } 28 | } 29 | System.out.println("Account Balance :: " + account.getBalance()); 30 | 31 | // when field is public 32 | /* 33 | * Field f = accountClass.getField("balance"); f.setInt(account, 10000); 34 | * System.out.println(account.getBalance()); 35 | */ 36 | 37 | // accessing the private fields 38 | Field privateField = accountClass.getDeclaredField("balance"); 39 | privateField.setAccessible(true); 40 | privateField.set(account, 20000); 41 | System.out.println("New Balance is :: " + account.getBalance()); 42 | 43 | Field[] fields = accountClass.getDeclaredFields(); 44 | System.out.println("Fields in Account class"); 45 | for (Field field : fields) 46 | System.out.println(field.getName()); 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /Threads/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | -------------------------------------------------------------------------------- /Threads/.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | -------------------------------------------------------------------------------- /Threads/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | Threads 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /Threads/.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 3 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 4 | org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve 5 | org.eclipse.jdt.core.compiler.compliance=1.8 6 | org.eclipse.jdt.core.compiler.debug.lineNumber=generate 7 | org.eclipse.jdt.core.compiler.debug.localVariable=generate 8 | org.eclipse.jdt.core.compiler.debug.sourceFile=generate 9 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 10 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 11 | org.eclipse.jdt.core.compiler.source=1.8 12 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/thread/atmcenter/ATMCenter.java: -------------------------------------------------------------------------------- 1 | package com.cdac.thread.atmcenter; 2 | 3 | public class ATMCenter { 4 | public void withdraw(int amount){ 5 | String name = Thread.currentThread().getName(); 6 | System.out.println("Entered into ATM center : "+name); 7 | System.out.println("Taking the money : "+amount); 8 | try { 9 | Thread.sleep(1000); 10 | System.out.println("Transaction completed : "+amount); 11 | } catch (InterruptedException e) { 12 | e.printStackTrace(); 13 | } 14 | System.out.println("Exit from the ATM center : "+name); 15 | } 16 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/thread/atmcenter/MainProgram.java: -------------------------------------------------------------------------------- 1 | package com.cdac.thread.atmcenter; 2 | 3 | public class MainProgram { 4 | 5 | public static void main(String[] args) { 6 | ATMCenter atmCenter = new ATMCenter(); 7 | 8 | MoneyWithdrawRunnable runnable1 = new MoneyWithdrawRunnable(atmCenter, 500); 9 | MoneyWithdrawRunnable runnable2 = new MoneyWithdrawRunnable(atmCenter, 1000); 10 | MoneyWithdrawRunnable runnable3 = new MoneyWithdrawRunnable(atmCenter, 50000); 11 | 12 | Thread t1 = new Thread(runnable1); 13 | t1.setName("Sathish"); 14 | Thread t2 = new Thread(runnable2); 15 | t2.setName("Ranjit"); 16 | Thread t3 = new Thread(runnable3); 17 | t3.setName("Raakesh"); 18 | 19 | t1.start(); 20 | t2.start(); 21 | t3.start(); 22 | 23 | } 24 | } 25 | /* 26 | OUTPUT 27 | 28 | Entered into ATM center : Sathish 29 | Taking the money : 500 30 | Transaction completed : 500 31 | Exit from the ATM center : Sathish 32 | 33 | Entered into ATM center : Raakesh 34 | Taking the money : 50000 35 | Transaction completed : 50000 36 | Exit from the ATM center : Raakesh 37 | 38 | Entered into ATM center : Ranjit 39 | Taking the money : 1000 40 | Transaction completed : 1000 41 | Exit from the ATM center : Ranjit 42 | 43 | */ -------------------------------------------------------------------------------- /Threads/src/com/cdac/thread/atmcenter/MoneyWithdrawRunnable.java: -------------------------------------------------------------------------------- 1 | package com.cdac.thread.atmcenter; 2 | 3 | public class MoneyWithdrawRunnable implements Runnable{ 4 | 5 | ATMCenter atmCenter; 6 | int amount; 7 | 8 | public MoneyWithdrawRunnable(ATMCenter atmCenter,int amount) { 9 | this.atmCenter = atmCenter; 10 | this.amount= amount; 11 | } 12 | 13 | @Override 14 | public void run() { 15 | synchronized (atmCenter) { 16 | atmCenter.withdraw(amount); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/GoodEveningThread.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads; 2 | 3 | public class GoodEveningThread implements Runnable{ 4 | @Override 5 | public void run() { 6 | Thread curThread = Thread.currentThread(); 7 | String name = curThread.getName(); 8 | for(int i=0;i<10;i++){ 9 | System.out.println(name+" :: Good Evening !"); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/GoodMorningThread.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads; 2 | 3 | public class GoodMorningThread extends Thread{ 4 | @Override 5 | public void run() { 6 | super.run(); 7 | Thread curThread = Thread.currentThread(); 8 | String name = curThread.getName(); 9 | for(int i=0;i<10;i++){ 10 | System.out.println(name+" :: Good Morning !"); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/JoinDemo.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads; 2 | 3 | public class JoinDemo { 4 | 5 | public static void main(String[] args) { 6 | GoodMorningThread thread1 = new GoodMorningThread(); 7 | thread1.setName("Morning"); 8 | 9 | GoodEveningThread runnable = new GoodEveningThread(); 10 | Thread thread2 = new Thread(runnable); 11 | thread2.setName("Evening"); 12 | 13 | thread1.start(); 14 | try { 15 | thread1.join(); 16 | //now thread1 will complete then only thread2 will start its execution 17 | } catch (InterruptedException e) { 18 | e.printStackTrace(); 19 | } 20 | thread2.start(); 21 | 22 | System.out.println("End of main Thread"); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/ThreadMain.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads; 2 | 3 | public class ThreadMain { 4 | public static void main(String[] args) { 5 | 6 | GoodMorningThread thread1 = new GoodMorningThread(); 7 | thread1.setName("Morning"); 8 | 9 | GoodEveningThread runnable = new GoodEveningThread(); 10 | Thread thread2 = new Thread(runnable); 11 | thread2.setName("Evening"); 12 | 13 | thread1.start(); 14 | thread2.start(); 15 | 16 | System.out.println("End of main Thread"); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/deadlock/DeadLock1.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.deadlock; 2 | 3 | public class DeadLock1 { 4 | 5 | public static void main(String[] args) throws InterruptedException { 6 | //here current thread is Main Thread 7 | Thread.currentThread().join(); 8 | } 9 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/deadlock/DeadLock2.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.deadlock; 2 | 3 | public class DeadLock2 { 4 | public static void main(String[] args) throws InterruptedException { 5 | DeadLockRunnable runnable = new DeadLockRunnable(); 6 | Thread t1 = new Thread(runnable); 7 | t1.start(); 8 | t1.join(); 9 | System.out.println("End of Main Thread"); 10 | } 11 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/deadlock/DeadLockRunnable.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.deadlock; 2 | 3 | public class DeadLockRunnable implements Runnable 4 | { 5 | @Override 6 | public void run() { 7 | System.out.println("Dead Lock Thread Start "); 8 | try { 9 | Thread.currentThread().join(); 10 | } catch (InterruptedException e) { 11 | e.printStackTrace(); 12 | } 13 | System.out.println("Dead Lock Thread End"); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/synchronization/ConsumerRunnable.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.synchronization; 2 | 3 | public class ConsumerRunnable implements Runnable{ 4 | 5 | Factory factory; 6 | public ConsumerRunnable(Factory factory) { 7 | this.factory = factory; 8 | } 9 | 10 | @Override 11 | public void run() { 12 | factory.consume(); 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/synchronization/Factory.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.synchronization; 2 | 3 | public class Factory { 4 | 5 | int current = 0; 6 | Object lock = new Object(); 7 | 8 | public void produce(){ 9 | synchronized (lock) { 10 | System.out.println("Producer is called"); 11 | while(current < 3){ 12 | current++; 13 | System.out.println("Producing the Item : "+current); 14 | try { 15 | Thread.sleep(1000); 16 | } catch (InterruptedException e) { 17 | e.printStackTrace(); 18 | } 19 | } 20 | System.out.println("Production is done!"); 21 | lock.notifyAll(); 22 | } 23 | } 24 | 25 | public void consume(){ 26 | synchronized (lock) { 27 | System.out.println("Consumer is called !"); 28 | try { 29 | if(current <= 0 ){ 30 | System.out.println("No item is available to consume, going to wait state"); 31 | lock.wait(); 32 | } 33 | } catch (InterruptedException e) { 34 | e.printStackTrace(); 35 | } 36 | while(current > 0){ 37 | System.out.println("Consuming the item : "+current); 38 | try { 39 | Thread.sleep(1000); 40 | } catch (InterruptedException e) { 41 | e.printStackTrace(); 42 | } 43 | current--; 44 | } 45 | System.out.println("Consumed all the items"); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/synchronization/ProducerConsumeMain.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.synchronization; 2 | 3 | public class ProducerConsumeMain { 4 | public static void main(String[] args) { 5 | Factory factory = new Factory(); 6 | 7 | ProducerRunnable producer = new ProducerRunnable(factory); 8 | ConsumerRunnable consumer = new ConsumerRunnable(factory); 9 | 10 | Thread producerThread = new Thread(producer); 11 | Thread consumerThread = new Thread(consumer); 12 | 13 | consumerThread.start(); 14 | producerThread.start(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/synchronization/ProducerRunnable.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.synchronization; 2 | 3 | public class ProducerRunnable implements Runnable{ 4 | 5 | Factory factory; 6 | public ProducerRunnable(Factory factory) { 7 | this.factory = factory; 8 | } 9 | 10 | @Override 11 | public void run() { 12 | factory.produce(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/synchronization2/MessageSender.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.synchronization2; 2 | 3 | public class MessageSender { 4 | public void send(String message){ 5 | System.out.println("Sending : "+message); 6 | try { 7 | Thread.sleep(1000); 8 | } catch (InterruptedException e) { 9 | e.printStackTrace(); 10 | } 11 | System.out.println("Message Sent : "+message); 12 | } 13 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/synchronization2/MessageSenderMain.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.synchronization2; 2 | 3 | public class MessageSenderMain { 4 | 5 | public static void main(String[] args) { 6 | MessageSender sender = new MessageSender(); 7 | 8 | Thread t1 = new Thread(new SenderRunnable(sender, "Hi")); 9 | Thread t2 = new Thread(new SenderRunnable(sender, "Hello")); 10 | Thread t3 = new Thread(new SenderRunnable(sender, "Good Morning")); 11 | 12 | t1.start(); 13 | t2.start(); 14 | t3.start(); 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /Threads/src/com/cdac/threads/synchronization2/SenderRunnable.java: -------------------------------------------------------------------------------- 1 | package com.cdac.threads.synchronization2; 2 | 3 | public class SenderRunnable implements Runnable{ 4 | 5 | MessageSender sender; 6 | String message; 7 | 8 | public SenderRunnable(MessageSender sender, String message) { 9 | this.sender = sender; 10 | this.message = message; 11 | } 12 | 13 | @Override 14 | public void run() { 15 | synchronized (sender) { 16 | sender.send(message); 17 | } 18 | } 19 | } --------------------------------------------------------------------------------