├── ASCII1.java ├── ASCII2.java ├── ASCII3.java ├── Armstrong.java ├── CountNodes.java ├── CountNodesCL.java ├── CreateList.java ├── DeleteEnd.java ├── DeleteEndCL.java ├── DeleteStart.java ├── DeleteStartCL.java ├── GCD.java ├── ISBN.java ├── InsertEnd.java ├── InsertMid.java ├── InsertStart.java ├── LinkedListExamples.java ├── MinMax.java ├── PalindromeLL.java ├── Pascals.java ├── README.md ├── RemoveDuplicates.java ├── ReverseList.java ├── ReverseListCL.java ├── SearchLinkedList.java ├── SinglyLinkedList.java ├── arr1.java ├── arr10.java ├── arr11.java ├── arr12.java ├── arr13.java ├── arr14.java ├── arr16.java ├── arr17.java ├── arr18.java ├── arr19.java ├── arr2.java ├── arr20.java ├── arr21.java ├── arr22.java ├── arr23.java ├── arr3.java ├── arr4.java ├── arr5.java ├── arr6.java ├── arr7.java ├── arr8.java ├── arr9.java ├── atm.java ├── autobiographical_num.java ├── automorphic.java ├── bin2dec.java ├── binsearch.java ├── blank_diamond.java ├── blank_pyramid.java ├── bool2str.java ├── bouncy_num.java ├── bubblesort.java ├── buzz_num.java ├── chaddi.java ├── char2str.java ├── char_kotlin.java ├── char_pyramid.java ├── char_tripat_1.java ├── char_tripat_2.java ├── chr2int.java ├── date2str.java ├── date2timestp.java ├── dbl2int.java ├── dbl2str.java ├── dec2bin.java ├── dec2hex.java ├── dec2oct.java ├── deleteMid.java ├── diamond.java ├── down_right_tri.java ├── duck_num.java ├── emirp_num.java ├── evil_num.java ├── fact.java ├── fascinating_num.java ├── fibo.java ├── flt2str.java ├── hex2dec.java ├── insertionsort.java ├── int2char.java ├── int2dbl.java ├── int2long.java ├── int2str.java ├── keith_num.java ├── kotlin_pat.java ├── krishnamurthy_num.java ├── left_pascals.java ├── left_triangle_pat.java ├── linsearch.java ├── long2int.java ├── long2str.java ├── mat1.java ├── mat10.java ├── mat11.java ├── mat12.java ├── mat13.java ├── mat2.java ├── mat3.java ├── mat4.java ├── mat5.java ├── mat6.java ├── mat7.java ├── mat8.java ├── mat9.java ├── mystery_num.java ├── neon.java ├── nth_prime.java ├── num2word.java ├── num_diamond.java ├── num_hourglass.java ├── numpat_1.java ├── numpat_2.java ├── numpat_3.java ├── numpat_box.java ├── obj2str.java ├── oct2dec.java ├── palin.java ├── perfect_sq.java ├── peterson_num.java ├── prime.java ├── pyramid.java ├── random.java ├── rev_blank_pyramid.java ├── rev_kotlin.java ├── reverse.java ├── right_pascals.java ├── right_triangle_pat.java ├── sand_hourglass.java ├── selectionsort.java ├── smith_num.java ├── sphenic_num.java ├── spy_num.java ├── str1.java ├── str10.java ├── str11.java ├── str12.java ├── str13.java ├── str14.java ├── str15.java ├── str16.java ├── str17.java ├── str18.java ├── str19.java ├── str2.java ├── str20.java ├── str2bool.java ├── str2char.java ├── str2date.java ├── str2dbl.java ├── str2flt.java ├── str2int.java ├── str2long.java ├── str2obj.java ├── str3.java ├── str4.java ├── str5.java ├── str6.java ├── str7.java ├── str8.java ├── str9.java ├── strontio_num.java ├── sunny_num.java ├── tech_num.java ├── timestp2date.java ├── tri_numpat_1.java ├── tri_numpat_10.java ├── tri_numpat_11.java ├── tri_numpat_2.java ├── tri_numpat_3.java ├── tri_numpat_4.java ├── tri_numpat_5.java ├── tri_numpat_6.java ├── tri_numpat_7.java ├── tri_numpat_8.java ├── tri_numpat_9.java └── xylem_phloem_num.java /ASCII1.java: -------------------------------------------------------------------------------- 1 | class PrintAsciiValueExample2 2 | { 3 | public static void main(String[] String) 4 | { 5 | int ch1 = 'a'; 6 | int ch2 = 'b'; 7 | System.out.println("The ASCII value of a is: "+ch1); 8 | System.out.println("The ASCII value of b is: "+ch2); 9 | } 10 | } -------------------------------------------------------------------------------- /ASCII2.java: -------------------------------------------------------------------------------- 1 | class PrintAsciiValueExample3 2 | { 3 | public static void main(String[] args) 4 | { 5 | //characters whose ASCII value to be found 6 | char ch1 = 'a'; 7 | char ch2 = 'b'; 8 | //casting or converting a charter into int type 9 | int ascii1 = (int) ch1; 10 | int ascii2 = (int) ch2; 11 | System.out.println("The ASCII value of " + ch1 + " is: " + ascii1); 12 | System.out.println("The ASCII value of " + ch1 + " is: " + ascii2); 13 | } 14 | } -------------------------------------------------------------------------------- /ASCII3.java: -------------------------------------------------------------------------------- 1 | class AsciiValueOfAllChracters 2 | { 3 | public static void main(String[] args) 4 | { 5 | for(int i = 0; i <= 255; i++) 6 | { 7 | System.out.println(" The ASCII value of " + (char)i + " = " + i); 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /Armstrong.java: -------------------------------------------------------------------------------- 1 | class ArmstrongExample{ 2 | public static void main(String[] args) { 3 | int c=0,a,temp; 4 | int n=153;//It is the number to check armstrong 5 | temp=n; 6 | while(n>0) 7 | { 8 | a=n%10; 9 | n=n/10; 10 | c=c+(a*a*a); 11 | } 12 | if(temp==c) 13 | System.out.println("armstrong number"); 14 | else 15 | System.out.println("Not armstrong number"); 16 | } 17 | } -------------------------------------------------------------------------------- /CountNodes.java: -------------------------------------------------------------------------------- 1 | public class CountNodes { 2 | 3 | //Represent a node of singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | //Represent the head and tail of the singly linked list 15 | public Node head = null; 16 | public Node tail = null; 17 | 18 | //addNode() will add a new node to the list 19 | public void addNode(int data) { 20 | //Create a new node 21 | Node newNode = new Node(data); 22 | 23 | //Checks if the list is empty 24 | if(head == null) { 25 | //If list is empty, both head and tail will point to new node 26 | head = newNode; 27 | tail = newNode; 28 | } 29 | else { 30 | //newNode will be added after tail such that tail's next will point to newNode 31 | tail.next = newNode; 32 | //newNode will become new tail of the list 33 | tail = newNode; 34 | } 35 | } 36 | 37 | //countNodes() will count the nodes present in the list 38 | public int countNodes() { 39 | int count = 0; 40 | //Node current will point to head 41 | Node current = head; 42 | 43 | while(current != null) { 44 | //Increment the count by 1 for each node 45 | count++; 46 | current = current.next; 47 | } 48 | return count; 49 | } 50 | 51 | //display() will display all the nodes present in the list 52 | public void display() { 53 | //Node current will point to head 54 | Node current = head; 55 | if(head == null) { 56 | System.out.println("List is empty"); 57 | return; 58 | } 59 | System.out.println("Nodes of singly linked list: "); 60 | while(current != null) { 61 | //Prints each node by incrementing pointer 62 | System.out.print(current.data + " "); 63 | current = current.next; 64 | } 65 | System.out.println(); 66 | } 67 | 68 | public static void main(String[] args) { 69 | 70 | CountNodes sList = new CountNodes(); 71 | 72 | //Add nodes to the list 73 | sList.addNode(1); 74 | sList.addNode(2); 75 | sList.addNode(3); 76 | sList.addNode(4); 77 | 78 | //Displays the nodes present in the list 79 | sList.display(); 80 | 81 | //Counts the nodes present in the given list 82 | System.out.println("Count of nodes present in the list: " + sList.countNodes()); 83 | } 84 | } -------------------------------------------------------------------------------- /CountNodesCL.java: -------------------------------------------------------------------------------- 1 | public class CountNodesCL { 2 | //Represents the node of list. 3 | public class Node{ 4 | int data; 5 | Node next; 6 | public Node(int data) { 7 | this.data = data; 8 | } 9 | } 10 | 11 | public int count; 12 | //Declaring head and tail pointer as null. 13 | public Node head = null; 14 | public Node tail = null; 15 | 16 | //This function will add the new node at the end of the list. 17 | public void add(int data){ 18 | //Create new node 19 | Node newNode = new Node(data); 20 | //Checks if the list is empty. 21 | if(head == null) { 22 | //If list is empty, both head and tail would point to new node. 23 | head = newNode; 24 | tail = newNode; 25 | newNode.next = head; 26 | } 27 | else { 28 | //tail will point to new node. 29 | tail.next = newNode; 30 | //New node will become new tail. 31 | tail = newNode; 32 | //Since, it is circular linked list tail will point to head. 33 | tail.next = head; 34 | } 35 | } 36 | 37 | //This function will count the nodes of circular linked list 38 | public void countNodes() { 39 | Node current = head; 40 | do{ 41 | //Increment the count variable by 1 for each node 42 | count++; 43 | current = current.next; 44 | }while(current != head); 45 | System.out.println("Count of nodes present in circular linked list: "+count); 46 | } 47 | 48 | public static void main(String[] args) { 49 | CountNodesCL cl = new CountNodesCL(); 50 | cl.add(1); 51 | cl.add(2); 52 | cl.add(4); 53 | cl.add(1); 54 | cl.add(2); 55 | cl.add(3); 56 | //Counts the number of nodes present in the list 57 | cl.countNodes(); 58 | } 59 | } -------------------------------------------------------------------------------- /CreateList.java: -------------------------------------------------------------------------------- 1 | public class CreateList { 2 | //Represents the node of list. 3 | public class Node{ 4 | int data; 5 | Node next; 6 | public Node(int data) { 7 | this.data = data; 8 | } 9 | } 10 | 11 | //Declaring head and tail pointer as null. 12 | public Node head = null; 13 | public Node tail = null; 14 | 15 | //This function will add the new node at the end of the list. 16 | public void add(int data){ 17 | //Create new node 18 | Node newNode = new Node(data); 19 | //Checks if the list is empty. 20 | if(head == null) { 21 | //If list is empty, both head and tail would point to new node. 22 | head = newNode; 23 | tail = newNode; 24 | newNode.next = head; 25 | } 26 | else { 27 | //tail will point to new node. 28 | tail.next = newNode; 29 | //New node will become new tail. 30 | tail = newNode; 31 | //Since, it is circular linked list tail will point to head. 32 | tail.next = head; 33 | } 34 | } 35 | 36 | //Displays all the nodes in the list 37 | public void display() { 38 | Node current = head; 39 | if(head == null) { 40 | System.out.println("List is empty"); 41 | } 42 | else { 43 | System.out.println("Nodes of the circular linked list: "); 44 | do{ 45 | //Prints each node by incrementing pointer. 46 | System.out.print(" "+ current.data); 47 | current = current.next; 48 | }while(current != head); 49 | System.out.println(); 50 | } 51 | } 52 | 53 | public static void main(String[] args) { 54 | CreateList cl = new CreateList(); 55 | //Adds data to the list 56 | cl.add(1); 57 | cl.add(2); 58 | cl.add(3); 59 | cl.add(4); 60 | //Displays all the nodes present in the list 61 | cl.display(); 62 | } 63 | } -------------------------------------------------------------------------------- /DeleteEnd.java: -------------------------------------------------------------------------------- 1 | public class DeleteEnd { 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | //Represent the head and tail of the singly linked list 15 | public Node head = null; 16 | public Node tail = null; 17 | 18 | //addNode() will add a new node to the list 19 | public void addNode(int data) { 20 | //Create a new node 21 | Node newNode = new Node(data); 22 | 23 | //Checks if the list is empty 24 | if(head == null) { 25 | //If list is empty, both head and tail will point to new node 26 | head = newNode; 27 | tail = newNode; 28 | } 29 | else { 30 | //newNode will be added after tail such that tail's next will point to newNode 31 | tail.next = newNode; 32 | //newNode will become new tail of the list 33 | tail = newNode; 34 | } 35 | } 36 | 37 | //deleteFromEnd() will delete a node from end of the list 38 | public void deleteFromEnd() { 39 | 40 | //Checks if the list is empty 41 | if(head == null) { 42 | System.out.println("List is empty"); 43 | return; 44 | } 45 | else { 46 | //Checks whether the list contains only one element 47 | if(head != tail ) { 48 | Node current = head; 49 | //Loop through the list till the second last element such that current.next is pointing to tail 50 | while(current.next != tail) { 51 | current = current.next; 52 | } 53 | //Second last element will become new tail of the list 54 | tail = current; 55 | tail.next = null; 56 | } 57 | //If the list contains only one element 58 | //Then it will remove it and both head and tail will point to null 59 | else { 60 | head = tail = null; 61 | } 62 | } 63 | } 64 | 65 | //display() will display all the nodes present in the list 66 | public void display() { 67 | //Node current will point to head 68 | Node current = head; 69 | if(head == null) { 70 | System.out.println("List is empty"); 71 | return; 72 | } 73 | while(current != null) { 74 | //Prints each node by incrementing pointer 75 | System.out.print(current.data + " "); 76 | current = current.next; 77 | } 78 | System.out.println(); 79 | } 80 | 81 | public static void main(String[] args) { 82 | 83 | DeleteEnd sList = new DeleteEnd(); 84 | 85 | //Adds data to the list 86 | sList.addNode(1); 87 | sList.addNode(2); 88 | sList.addNode(3); 89 | sList.addNode(4); 90 | 91 | //Printing original list 92 | System.out.println("Original List: "); 93 | sList.display(); 94 | 95 | while(sList.head != null) { 96 | sList.deleteFromEnd(); 97 | //Printing updated list 98 | System.out.println("Updated List: "); 99 | sList.display(); 100 | } 101 | } 102 | } -------------------------------------------------------------------------------- /DeleteEndCL.java: -------------------------------------------------------------------------------- 1 | public class DeleteEndCL { 2 | //Represents the node of list. 3 | public class Node{ 4 | int data; 5 | Node next; 6 | public Node(int data) { 7 | this.data = data; 8 | } 9 | } 10 | 11 | //Declaring head and tail pointer as null. 12 | public Node head = null; 13 | public Node tail = null; 14 | 15 | //This function will add the new node at the end of the list. 16 | public void add(int data){ 17 | //Create new node 18 | Node newNode = new Node(data); 19 | //Checks if the list is empty. 20 | if(head == null) { 21 | //If list is empty, both head and tail would point to new node. 22 | head = newNode; 23 | tail = newNode; 24 | newNode.next = head; 25 | } 26 | else { 27 | //tail will point to new node. 28 | tail.next = newNode; 29 | //New node will become new tail. 30 | tail = newNode; 31 | //Since, it is circular linked list tail will point to head. 32 | tail.next = head; 33 | } 34 | } 35 | 36 | //Deletes node from end of the list 37 | public void deleteEnd() { 38 | //Checks whether list is empty 39 | if(head == null) { 40 | return; 41 | } 42 | else { 43 | //Checks whether contain only one element 44 | if(head != tail ) { 45 | Node current = head; 46 | //Loop will iterate till the second last element as current.next is pointing to tail 47 | while(current.next != tail) { 48 | current = current.next; 49 | } 50 | //Second last element will be new tail 51 | tail = current; 52 | //Tail will point to head as it is a circular linked list 53 | tail.next = head; 54 | } 55 | //If the list contains only one element 56 | //Then it will remove it and both head and tail will point to null 57 | else { 58 | head = tail = null; 59 | } 60 | } 61 | } 62 | 63 | //Displays all the nodes in the list 64 | public void display() { 65 | Node current = head; 66 | if(head == null) { 67 | System.out.println("List is empty"); 68 | } 69 | else { 70 | do{ 71 | //Prints each node by incrementing pointer. 72 | System.out.print(" "+ current.data); 73 | current = current.next; 74 | }while(current != head); 75 | System.out.println(); 76 | } 77 | } 78 | 79 | public static void main(String[] args) { 80 | DeleteEndCL cl = new DeleteEndCL(); 81 | //Adds data to the list 82 | cl.add(1); 83 | cl.add(2); 84 | cl.add(3); 85 | cl.add(4); 86 | //Printing original list 87 | System.out.println("Original List: "); 88 | cl.display(); 89 | while(cl.head != null) { 90 | cl.deleteEnd(); 91 | //Printing updated list 92 | System.out.println("Updated List: "); 93 | cl.display(); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /DeleteStart.java: -------------------------------------------------------------------------------- 1 | public class DeleteStart { 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | //Represent the head and tail of the singly linked list 15 | public Node head = null; 16 | public Node tail = null; 17 | 18 | //addNode() will add a new node to the list 19 | public void addNode(int data) { 20 | //Create a new node 21 | Node newNode = new Node(data); 22 | 23 | //Checks if the list is empty 24 | if(head == null) { 25 | //If list is empty, both head and tail will point to new node 26 | head = newNode; 27 | tail = newNode; 28 | } 29 | else { 30 | //newNode will be added after tail such that tail's next will point to newNode 31 | tail.next = newNode; 32 | //newNode will become new tail of the list 33 | tail = newNode; 34 | } 35 | } 36 | 37 | //deleteFromStart() will delete a node from the beginning of the list 38 | public void deleteFromStart() { 39 | 40 | //Checks if the list is empty 41 | if(head == null) { 42 | System.out.println("List is empty"); 43 | return; 44 | } 45 | else { 46 | //Checks whether the list contains only one node 47 | //If not, the head will point to next node in the list and tail will point to the new head. 48 | if(head != tail) { 49 | head = head.next; 50 | } 51 | //If the list contains only one node 52 | //then, it will remove it and both head and tail will point to null 53 | else { 54 | head = tail = null; 55 | } 56 | } 57 | } 58 | 59 | //display() will display all the nodes present in the list 60 | public void display() { 61 | //Node current will point to head 62 | Node current = head; 63 | if(head == null) { 64 | System.out.println("List is empty"); 65 | return; 66 | } 67 | while(current != null) { 68 | //Prints each node by incrementing pointer 69 | System.out.print(current.data + " "); 70 | current = current.next; 71 | } 72 | System.out.println(); 73 | } 74 | 75 | public static void main(String[] args) { 76 | 77 | DeleteStart sList = new DeleteStart(); 78 | 79 | //Adds data to the list 80 | sList.addNode(1); 81 | sList.addNode(2); 82 | sList.addNode(3); 83 | sList.addNode(4); 84 | 85 | //Printing original list 86 | System.out.println("Original List: "); 87 | sList.display(); 88 | 89 | while(sList.head != null) { 90 | sList.deleteFromStart(); 91 | //Printing updated list 92 | System.out.println("Updated List: "); 93 | sList.display(); 94 | } 95 | } 96 | } -------------------------------------------------------------------------------- /DeleteStartCL.java: -------------------------------------------------------------------------------- 1 | public class DeleteStartCL { 2 | //Represents the node of list. 3 | public class Node{ 4 | int data; 5 | Node next; 6 | public Node(int data) { 7 | this.data = data; 8 | } 9 | } 10 | 11 | //Declaring head and tail pointer as null. 12 | public Node head = null; 13 | public Node tail = null; 14 | 15 | //This function will add the new node at the end of the list. 16 | public void add(int data){ 17 | //Create new node 18 | Node newNode = new Node(data); 19 | //Checks if the list is empty. 20 | if(head == null) { 21 | //If list is empty, both head and tail would point to new node. 22 | head = newNode; 23 | tail = newNode; 24 | newNode.next = head; 25 | } 26 | else { 27 | //tail will point to new node. 28 | tail.next = newNode; 29 | //New node will become new tail. 30 | tail = newNode; 31 | //Since, it is circular linked list tail will point to head. 32 | tail.next = head; 33 | } 34 | } 35 | 36 | //Deletes node from the beginning of the list 37 | public void deleteStart() { 38 | //Checks whether list is empty 39 | if(head == null) { 40 | return; 41 | } 42 | else { 43 | //Checks whether contain only one element 44 | //If not, head will point to next element in the list and tail will point to new head. 45 | if(head != tail ) { 46 | head = head.next; 47 | tail.next = head; 48 | } 49 | //If the list contains only one element 50 | //then it will remove it and both head and tail will point to null 51 | else { 52 | head = tail = null; 53 | } 54 | } 55 | } 56 | 57 | //Displays all the nodes in the list 58 | public void display() { 59 | Node current = head; 60 | if(head == null) { 61 | System.out.println("List is empty"); 62 | } 63 | else { 64 | do{ 65 | //Prints each node by incrementing pointer. 66 | System.out.print(" "+ current.data); 67 | current = current.next; 68 | }while(current != head); 69 | System.out.println(); 70 | } 71 | } 72 | 73 | public static void main(String[] args) { 74 | DeleteStartCL cl = new DeleteStartCL(); 75 | //Adds data to the list 76 | cl.add(1); 77 | cl.add(2); 78 | cl.add(3); 79 | cl.add(4); 80 | //Printing original list 81 | System.out.println("Original List: "); 82 | cl.display(); 83 | while(cl.head != null) { 84 | cl.deleteStart(); 85 | //Printing updated list 86 | System.out.println("Updated List: "); 87 | cl.display(); 88 | } 89 | } 90 | } -------------------------------------------------------------------------------- /GCD.java: -------------------------------------------------------------------------------- 1 | class FindGCDExample1 2 | { 3 | public static void main(String[] args) 4 | { 5 | //x and y are the numbers to find the GCF 6 | int x = 12, y = 8, gcd = 1; 7 | //running loop form 1 to the smallest of both numbers 8 | for(int i = 1; i <= x && i <= y; i++) 9 | { 10 | //returns true if both conditions are satisfied 11 | if(x%i==0 && y%i==0) 12 | //storing the variable i in the variable gcd 13 | gcd = i; 14 | } 15 | //prints the gcd 16 | System.out.printf("GCD of %d and %d is: %d", x, y, gcd); 17 | } 18 | } -------------------------------------------------------------------------------- /ISBN.java: -------------------------------------------------------------------------------- 1 | 2 | import java.io.*; 3 | //create ISBNNumberExample class to check whether the given number is a valid ISBN or not 4 | class ISBNNumberExample { 5 | 6 | static boolean checkISBNNumber(long number) 7 | { 8 | int sum = 0; 9 | int i, t, intNumber, dNumber; 10 | String strNumber; 11 | 12 | strNumber = ""+number; 13 | 14 | if (strNumber.length() != 10) { 15 | return false; 16 | } 17 | 18 | for (i = 0; i < strNumber.length(); i++) { 19 | intNumber = Integer.parseInt(strNumber.substring(i, i+1)); 20 | dNumber = i + 1; 21 | t = dNumber * intNumber; 22 | sum = sum + t; 23 | } 24 | 25 | // check whether the sum is divisible by 11 or not 26 | 27 | if ((sum % 11) == 0) { 28 | return true; 29 | } 30 | 31 | return false; 32 | 33 | } 34 | 35 | // main() method start 36 | public static void main(String args[]) 37 | { 38 | long n1, n2; 39 | 40 | try { 41 | 42 | //create BufferedReader class object to get input from user 43 | InputStreamReader in = new InputStreamReader(System.in); 44 | BufferedReader br = new BufferedReader(in); 45 | 46 | //show custom message 47 | System.out.println("Enter first 10 digit ISBN number"); 48 | 49 | //store user entered value into variable n1 50 | n1 = Long.parseLong(br.readLine()); 51 | 52 | //show custom message 53 | System.out.println("Enter second 10 digit ISBN number"); 54 | 55 | //store user entered value into variable n2 56 | n2 = Long.parseLong(br.readLine()); 57 | 58 | if (checkISBNNumber(n1)) 59 | System.out.println(n1 + " is a valid ISBN number"); 60 | else 61 | System.out.println(n1 + " is not a valid ISBN number"); 62 | if (checkISBNNumber(n2)) 63 | System.out.println(n2 + " is a a valid ISBN number"); 64 | else 65 | System.out.println(n2 + " is not a valid ISBN number"); 66 | 67 | }catch(java.lang.Exception e) { 68 | System.out.println("Error while reading the data."); 69 | } 70 | } 71 | } -------------------------------------------------------------------------------- /InsertEnd.java: -------------------------------------------------------------------------------- 1 | public class InsertEnd { 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | //Represent the head and tail of the singly linked list 15 | public Node head = null; 16 | public Node tail = null; 17 | 18 | //addAtEnd() will add a new node to the end of the list 19 | public void addAtEnd(int data) { 20 | //Create a new node 21 | Node newNode = new Node(data); 22 | 23 | //Checks if the list is empty 24 | if(head == null) { 25 | //If list is empty, both head and tail will point to new node 26 | head = newNode; 27 | tail = newNode; 28 | } 29 | else { 30 | //newNode will be added after tail such that tail's next will point to newNode 31 | tail.next = newNode; 32 | //newNode will become new tail of the list 33 | tail = newNode; 34 | } 35 | } 36 | 37 | //display() will display all the nodes present in the list 38 | public void display() { 39 | //Node current will point to head 40 | Node current = head; 41 | if(head == null) { 42 | System.out.println("List is empty"); 43 | return; 44 | } 45 | System.out.println("Adding nodes to the end of the list: "); 46 | while(current != null) { 47 | //Prints each node by incrementing pointer 48 | System.out.print(current.data + " "); 49 | current = current.next; 50 | } 51 | System.out.println(); 52 | } 53 | 54 | public static void main(String[] args) { 55 | 56 | InsertEnd sList = new InsertEnd(); 57 | 58 | //Adding 1 to the list 59 | sList.addAtEnd(1); 60 | sList.display(); 61 | 62 | //Adding 2 to the list 63 | sList.addAtEnd(2); 64 | sList.display(); 65 | 66 | //Adding 3 to the list 67 | sList.addAtEnd(3); 68 | sList.display(); 69 | 70 | //Adding 4 to the list 71 | sList.addAtEnd(4); 72 | sList.display(); 73 | } 74 | } -------------------------------------------------------------------------------- /InsertMid.java: -------------------------------------------------------------------------------- 1 | public class InsertMid { 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | public int size; 15 | //Represent the head and tail of the singly linked list 16 | public Node head = null; 17 | public Node tail = null; 18 | 19 | //addNode() will add a new node to the list 20 | public void addNode(int data) { 21 | //Create a new node 22 | Node newNode = new Node(data); 23 | 24 | //Checks if the list is empty 25 | if(head == null) { 26 | //If list is empty, both head and tail will point to new node 27 | head = newNode; 28 | tail = newNode; 29 | } 30 | else { 31 | //newNode will be added after tail such that tail's next will point to newNode 32 | tail.next = newNode; 33 | //newNode will become new tail of the list 34 | tail = newNode; 35 | } 36 | //Size will count the number of nodes present in the list 37 | size++; 38 | } 39 | 40 | //This function will add the new node at the middle of the list. 41 | public void addInMid(int data){ 42 | //Create a new node 43 | Node newNode = new Node(data); 44 | 45 | //Checks if the list is empty 46 | if(head == null) { 47 | //If list is empty, both head and tail would point to new node 48 | head = newNode; 49 | tail = newNode; 50 | } 51 | else { 52 | Node temp, current; 53 | //Store the mid position of the list 54 | int count = (size % 2 == 0) ? (size/2) : ((size+1)/2); 55 | //Node temp will point to head 56 | temp = head; 57 | current = null; 58 | 59 | //Traverse through the list till the middle of the list is reached 60 | for(int i = 0; i < count; i++) { 61 | //Node current will point to temp 62 | current = temp; 63 | //Node temp will point to node next to it. 64 | temp = temp.next; 65 | } 66 | 67 | //current will point to new node 68 | current.next = newNode; 69 | //new node will point to temp 70 | newNode.next = temp; 71 | } 72 | size++; 73 | } 74 | 75 | //display() will display all the nodes present in the list 76 | public void display() { 77 | //Node current will point to head 78 | Node current = head; 79 | if(head == null) { 80 | System.out.println("List is empty"); 81 | return; 82 | } 83 | 84 | while(current != null) { 85 | //Prints each node by incrementing pointer 86 | System.out.print(current.data + " "); 87 | current = current.next; 88 | } 89 | System.out.println(); 90 | } 91 | 92 | public static void main(String[] args) { 93 | 94 | InsertMid sList = new InsertMid(); 95 | 96 | //Adds data to the list 97 | sList.addNode(1); 98 | sList.addNode(2); 99 | 100 | System.out.println("Original list: "); 101 | sList.display(); 102 | 103 | //Inserting node '3' in the middle 104 | sList.addInMid(3); 105 | System.out.println( "Updated List: "); 106 | sList.display(); 107 | 108 | //Inserting node '4' in the middle 109 | sList.addInMid(4); 110 | System.out.println("Updated List: "); 111 | sList.display(); 112 | } 113 | } -------------------------------------------------------------------------------- /InsertStart.java: -------------------------------------------------------------------------------- 1 | public class InsertStart { 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | //Represent the head and tail of the singly linked list 15 | public Node head = null; 16 | public Node tail = null; 17 | 18 | //addAtStart() will add a new node to the beginning of the list 19 | public void addAtStart(int data) { 20 | //Create a new node 21 | Node newNode = new Node(data); 22 | 23 | //Checks if the list is empty 24 | if(head == null) { 25 | //If list is empty, both head and tail will point to new node 26 | head = newNode; 27 | tail = newNode; 28 | } 29 | else { 30 | //Node temp will point to head 31 | Node temp = head; 32 | //newNode will become new head of the list 33 | head = newNode; 34 | //Node temp(previous head) will be added after new head 35 | head.next = temp; 36 | } 37 | } 38 | 39 | //display() will display all the nodes present in the list 40 | public void display() { 41 | //Node current will point to head 42 | Node current = head; 43 | if(head == null) { 44 | System.out.println("List is empty"); 45 | return; 46 | } 47 | System.out.println("Adding nodes to the start of the list: "); 48 | while(current != null) { 49 | //Prints each node by incrementing pointer 50 | System.out.print(current.data + " "); 51 | current = current.next; 52 | } 53 | System.out.println(); 54 | } 55 | 56 | public static void main(String[] args) { 57 | 58 | InsertStart sList = new InsertStart(); 59 | 60 | //Adding 1 to the list 61 | sList.addAtStart(1); 62 | sList.display(); 63 | 64 | //Adding 2 to the list 65 | sList.addAtStart(2); 66 | sList.display(); 67 | 68 | //Adding 3 to the list 69 | sList.addAtStart(3); 70 | sList.display(); 71 | 72 | //Adding 4 to the list 73 | sList.addAtStart(4); 74 | sList.display(); 75 | } 76 | } -------------------------------------------------------------------------------- /LinkedListExamples.java: -------------------------------------------------------------------------------- 1 | public class LinkedListExamples 2 | { 3 | Node head; // head of list 4 | static class Node { 5 | int data; 6 | Node next; 7 | Node(int d) { data = d; next=null; } 8 | } 9 | /* This function prints contents of the linked list starting from head */ 10 | public void display() 11 | { 12 | Node n = head; 13 | while (n != null) 14 | { 15 | System.out.print(n.data+" \n"); 16 | n = n.next; 17 | } 18 | } 19 | /* method to create a simple linked list with 3 nodes*/ 20 | public static void main(String[] args) 21 | { 22 | /* Start with the empty list. */ 23 | LinkedListExamples list = new LinkedListExamples(); 24 | 25 | list.head = new Node(100); 26 | Node second = new Node(200); 27 | Node third = new Node(300); 28 | 29 | list.head.next = second; // Link first node with the second node 30 | second.next = third; // Link first node with the second node 31 | list.display(); 32 | } 33 | } -------------------------------------------------------------------------------- /MinMax.java: -------------------------------------------------------------------------------- 1 | public class MinMax { 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | //Represent the head and tail of the singly linked list 15 | public Node head = null; 16 | public Node tail = null; 17 | 18 | //addNode() will add a new node to the list 19 | public void addNode(int data) { 20 | //Create a new node 21 | Node newNode = new Node(data); 22 | 23 | //Checks if the list is empty 24 | if(head == null) { 25 | //If list is empty, both head and tail will point to new node 26 | head = newNode; 27 | tail = newNode; 28 | } 29 | else { 30 | //newNode will be added after tail such that tail's next will point to newNode 31 | tail.next = newNode; 32 | //newNode will become new tail of the list 33 | tail = newNode; 34 | } 35 | } 36 | 37 | //minNode() will find out the minimum value node in the list 38 | public void minNode() { 39 | Node current = head; 40 | int min; 41 | 42 | if(head == null) { 43 | System.out.println("List is empty"); 44 | } 45 | else { 46 | //Initializing min with head node data 47 | min = head.data; 48 | 49 | while(current != null){ 50 | //If current node's data is smaller than min 51 | //Then, replace value of min with current node's data 52 | if(min > current.data) { 53 | min = current.data; 54 | } 55 | current= current.next; 56 | } 57 | System.out.println("Minimum value node in the list: "+ min); 58 | } 59 | } 60 | 61 | //maxNode() will find out the maximum value node in the list 62 | public void maxNode() { 63 | Node current = head; 64 | int max; 65 | 66 | if(head == null) { 67 | System.out.println("List is empty"); 68 | } 69 | else { 70 | //Initializing max with head node data 71 | max = head.data; 72 | 73 | while(current != null){ 74 | //If current node's data is greater than max 75 | //Then, replace value of max with current node's data 76 | if(max < current.data) { 77 | max = current.data; 78 | } 79 | current = current.next; 80 | } 81 | System.out.println("Maximum value node in the list: "+ max); 82 | } 83 | } 84 | 85 | public static void main(String[] args) { 86 | 87 | MinMax sList = new MinMax(); 88 | 89 | //Adds data to the list 90 | sList.addNode(5); 91 | sList.addNode(8); 92 | sList.addNode(1); 93 | sList.addNode(6); 94 | 95 | //Display the minimum value node in the list 96 | sList.minNode(); 97 | 98 | //Display the maximum value node in the list 99 | sList.maxNode(); 100 | } 101 | } -------------------------------------------------------------------------------- /PalindromeLL.java: -------------------------------------------------------------------------------- 1 | public class PalindromeLL { 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | 8 | public Node(int data) { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | public int size; 15 | //Represent the head and tail of the singly linked list 16 | public Node head = null; 17 | public Node tail = null; 18 | 19 | //addNode() will add a new node to the list 20 | public void addNode(int data) { 21 | //Create a new node 22 | Node newNode = new Node(data); 23 | 24 | //Checks if the list is empty 25 | if(head == null) { 26 | //If list is empty, both head and tail will point to new node 27 | head = newNode; 28 | tail = newNode; 29 | } 30 | else { 31 | //newNode will be added after tail such that tail's next will point to newNode 32 | tail.next = newNode; 33 | //newNode will become new tail of the list 34 | tail = newNode; 35 | } 36 | //Size will count the number of nodes present in the list 37 | size++; 38 | } 39 | 40 | //reverseList() will reverse the singly linked list and return the head of the list 41 | public Node reverseList(Node temp){ 42 | Node current = temp; 43 | Node prevNode = null, nextNode = null; 44 | 45 | //Swap the previous and next nodes of each node to reverse the direction of the list 46 | while(current != null){ 47 | nextNode = current.next; 48 | current.next = prevNode; 49 | prevNode = current; 50 | current = nextNode; 51 | } 52 | return prevNode; 53 | } 54 | 55 | //isPalindrome() will determine whether given list is palindrome or not. 56 | public void isPalindromeLL(){ 57 | Node current = head; 58 | boolean flag = true; 59 | 60 | //Store the mid position of the list 61 | int mid = (size%2 == 0)? (size/2) : ((size+1)/2); 62 | 63 | //Finds the middle node in given singly linked list 64 | for(int i=1; i 0; j--){ 21 | //Shift element of array by one 22 | arr[j] = arr[j-1]; 23 | } 24 | //Last element of array will be added to the start of array. 25 | arr[0] = last; 26 | } 27 | 28 | System.out.println(); 29 | 30 | //Displays resulting array after rotation 31 | System.out.println("Array after right rotation: "); 32 | for(int i = 0; i< arr.length; i++){ 33 | System.out.print(arr[i] + " "); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /arr14.java: -------------------------------------------------------------------------------- 1 | public class arr14 { 2 | public static void main(String[] args) { 3 | //Initialize array 4 | int [] arr = new int [] {5, 2, 8, 7, 1}; 5 | int temp = 0; 6 | 7 | //Displaying elements of original array 8 | System.out.println("Elements of original array: "); 9 | for (int i = 0; i < arr.length; i++) { 10 | System.out.print(arr[i] + " "); 11 | } 12 | 13 | //Sort the array in descending order 14 | for (int i = 0; i < arr.length; i++) { 15 | for (int j = i+1; j < arr.length; j++) { 16 | if(arr[i] < arr[j]) { 17 | temp = arr[i]; 18 | arr[i] = arr[j]; 19 | arr[j] = temp; 20 | } 21 | } 22 | } 23 | 24 | System.out.println(); 25 | 26 | //Displaying elements of array after sorting 27 | System.out.println("Elements of array sorted in descending order: "); 28 | for (int i = 0; i < arr.length; i++) { 29 | System.out.print(arr[i] + " "); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /arr16.java: -------------------------------------------------------------------------------- 1 | public class arr16 { 2 | public static int getSecondLargest(int[] a, int total){ 3 | int temp; 4 | for (int i = 0; i < total; i++) 5 | { 6 | for (int j = i + 1; j < total; j++) 7 | { 8 | if (a[i] > a[j]) 9 | { 10 | temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | } 15 | } 16 | return a[total-2]; 17 | } 18 | public static void main(String args[]){ 19 | int a[]={1,2,5,6,3,2}; 20 | int b[]={44,66,99,77,33,22,55}; 21 | System.out.println("Second Largest: "+getSecondLargest(a,6)); 22 | System.out.println("Second Largest: "+getSecondLargest(b,7)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /arr17.java: -------------------------------------------------------------------------------- 1 | public class arr17 { 2 | public static int getSecondLargest(int[] a, int total){ 3 | int temp; 4 | for (int i = 0; i < total; i++) 5 | { 6 | for (int j = i + 1; j < total; j++) 7 | { 8 | if (a[i] > a[j]) 9 | { 10 | temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | } 15 | } 16 | return a[total-2]; 17 | } 18 | public static void main(String args[]){ 19 | int a[]={1,2,5,6,3,2}; 20 | int b[]={44,66,99,77,33,22,55}; 21 | System.out.println("Second Largest: "+getSecondLargest(a,6)); 22 | System.out.println("Second Largest: "+getSecondLargest(b,7)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /arr18.java: -------------------------------------------------------------------------------- 1 | public class arr18 { 2 | public static int getLargest(int[] a, int total){ 3 | int temp; 4 | for (int i = 0; i < total; i++) 5 | { 6 | for (int j = i + 1; j < total; j++) 7 | { 8 | if (a[i] > a[j]) 9 | { 10 | temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | } 15 | } 16 | return a[total-1]; 17 | } 18 | public static void main(String args[]){ 19 | int a[]={1,2,5,6,3,2}; 20 | int b[]={44,66,99,77,33,22,55}; 21 | System.out.println("Largest: "+getLargest(a,6)); 22 | System.out.println("Largest: "+getLargest(b,7)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /arr19.java: -------------------------------------------------------------------------------- 1 | public class arr19 { 2 | public static int getSecondSmallest(int[] a, int total){ 3 | int temp; 4 | for (int i = 0; i < total; i++) 5 | { 6 | for (int j = i + 1; j < total; j++) 7 | { 8 | if (a[i] > a[j]) 9 | { 10 | temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | } 15 | } 16 | return a[1];//2nd element because index starts from 0 17 | } 18 | public static void main(String args[]){ 19 | int a[]={1,2,5,6,3,2}; 20 | int b[]={44,66,99,77,33,22,55}; 21 | System.out.println("Second smallest: "+getSecondSmallest(a,6)); 22 | System.out.println("Second smallest: "+getSecondSmallest(b,7)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /arr2.java: -------------------------------------------------------------------------------- 1 | class Frequency { 2 | public static void main(String[] args) { 3 | //Initialize array 4 | int [] arr = new int [] {1, 2, 8, 3, 2, 2, 2, 5, 1}; 5 | //Array fr will store frequencies of element 6 | int [] fr = new int [arr.length]; 7 | int visited = -1; 8 | for(int i = 0; i < arr.length; i++){ 9 | int count = 1; 10 | for(int j = i+1; j < arr.length; j++){ 11 | if(arr[i] == arr[j]){ 12 | count++; 13 | //To avoid counting same element again 14 | fr[j] = visited; 15 | } 16 | } 17 | if(fr[i] != visited) 18 | fr[i] = count; 19 | } 20 | 21 | //Displays the frequency of each element present in array 22 | System.out.println("---------------------------------------"); 23 | System.out.println(" Element | Frequency"); 24 | System.out.println("---------------------------------------"); 25 | for(int i = 0; i < fr.length; i++){ 26 | if(fr[i] != visited) 27 | System.out.println(" " + arr[i] + " | " + fr[i]); 28 | } 29 | System.out.println("----------------------------------------"); 30 | }} -------------------------------------------------------------------------------- /arr20.java: -------------------------------------------------------------------------------- 1 | public class arr20 { 2 | public static int getSmallest(int[] a, int total){ 3 | int temp; 4 | for (int i = 0; i < total; i++) 5 | { 6 | for (int j = i + 1; j < total; j++) 7 | { 8 | if (a[i] > a[j]) 9 | { 10 | temp = a[i]; 11 | a[i] = a[j]; 12 | a[j] = temp; 13 | } 14 | } 15 | } 16 | return a[0]; 17 | } 18 | public static void main(String args[]){ 19 | int a[]={1,2,5,6,3,2}; 20 | int b[]={44,66,99,77,33,22,55}; 21 | System.out.println("Smallest: "+getSmallest(a,6)); 22 | System.out.println("Smallest: "+getSmallest(b,7)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /arr21.java: -------------------------------------------------------------------------------- 1 | public class arr21 { 2 | public static int removeDuplicateElements(int arr[], int n){ 3 | if (n==0 || n==1){ 4 | return n; 5 | } 6 | int[] temp = new int[n]; 7 | int j = 0; 8 | for (int i=0; i arr[j]) 14 | { 15 | tmp = arr[i]; 16 | arr[i] = arr[j]; 17 | arr[j] = tmp; 18 | } 19 | } 20 | //prints the sorted element of the array 21 | System.out.println(arr[i]); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /arr3.java: -------------------------------------------------------------------------------- 1 | class RotateLeft { 2 | public static void main(String[] args) { 3 | //Initialize array 4 | int [] arr = new int [] {1, 2, 3, 4, 5}; 5 | //n determine the number of times an array should be rotated 6 | int n = 3; 7 | //Displays original array 8 | System.out.println("Original array: "); 9 | for (int i = 0; i < arr.length; i++) { 10 | System.out.print(arr[i] + " "); 11 | } 12 | //Rotate the given array by n times toward left 13 | for(int i = 0; i < n; i++){ 14 | int j, first; 15 | //Stores the first element of the array 16 | first = arr[0]; 17 | for(j = 0; j < arr.length-1; j++){ 18 | //Shift element of array by one 19 | arr[j] = arr[j+1]; 20 | } 21 | //First element of array will be added to the end 22 | arr[j] = first; 23 | } 24 | System.out.println(); 25 | //Displays resulting array after rotation 26 | System.out.println("Array after left rotation: "); 27 | for(int i = 0; i< arr.length; i++){ 28 | System.out.print(arr[i] + " "); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /arr4.java: -------------------------------------------------------------------------------- 1 | class DuplicateElement { 2 | public static void main(String[] args) { 3 | //Initialize array 4 | int [] arr = new int [] {1, 2, 3, 4, 2, 7, 8, 8, 3}; 5 | System.out.println("Duplicate elements in given array: "); 6 | //Searches for duplicate element 7 | for(int i = 0; i < arr.length; i++) { 8 | for(int j = i + 1; j < arr.length; j++) { 9 | if(arr[i] == arr[j]) 10 | System.out.println(arr[j]); 11 | } 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /arr5.java: -------------------------------------------------------------------------------- 1 | public class arr5 { 2 | public static void main(String[] args) { 3 | //Initialize array 4 | int [] arr = new int [] {1, 2, 3, 4, 5}; 5 | System.out.println("Elements of given array: "); 6 | //Loop through the array by incrementing value of i 7 | for (int i = 0; i < arr.length; i++) { 8 | System.out.print(arr[i] + " "); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /arr6.java: -------------------------------------------------------------------------------- 1 | public class arr6 { 2 | public static void main(String[] args) { 3 | //Initialize array 4 | int [] arr = new int [] {1, 2, 3, 4, 5}; 5 | System.out.println("Original array: "); 6 | for (int i = 0; i < arr.length; i++) { 7 | System.out.print(arr[i] + " "); 8 | } 9 | System.out.println(); 10 | System.out.println("Array in reverse order: "); 11 | //Loop through the array in reverse order 12 | for (int i = arr.length-1; i >= 0; i--) { 13 | System.out.print(arr[i] + " "); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /arr7.java: -------------------------------------------------------------------------------- 1 | public class arr7 { 2 | public static void main(String[] args) { 3 | 4 | //Initialize array 5 | int [] arr = new int [] {1, 2, 3, 4, 5}; 6 | 7 | System.out.println("Elements of given array present on even position: "); 8 | //Loop through the array by incrementing value of i by 2 9 | //Here, i will start from 1 as first even positioned element is present at position 1. 10 | for (int i = 1; i < arr.length; i = i+2) { 11 | System.out.println(arr[i]); 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /arr8.java: -------------------------------------------------------------------------------- 1 | public class arr8 { 2 | public static void main(String[] args) { 3 | //Initialize array 4 | int [] arr = new int [] {1, 2, 3, 4, 5}; 5 | System.out.println("Elements of given array present on odd position: "); 6 | //Loop through the array by incrementing value of i by 2 7 | for (int i = 0; i < arr.length; i = i+2) { 8 | System.out.println(arr[i]); 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /arr9.java: -------------------------------------------------------------------------------- 1 | public class arr9 { 2 | public static void main(String[] args) { 3 | 4 | //Initialize array 5 | int [] arr = new int [] {25, 11, 7, 75, 56}; 6 | //Initialize max with first element of array. 7 | int max = arr[0]; 8 | //Loop through the array 9 | for (int i = 0; i < arr.length; i++) { 10 | //Compare elements of array with max 11 | if(arr[i] > max) 12 | max = arr[i]; 13 | } 14 | System.out.println("Largest element present in given array: " + max); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /atm.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //create ATMExample class to implement the ATM functionality 4 | class ATMExample 5 | { 6 | //main method starts 7 | public static void main(String args[] ) 8 | { 9 | //declare and initialize balance, withdraw, and deposit 10 | int balance = 100000, withdraw, deposit; 11 | 12 | //create scanner class object to get choice of user 13 | Scanner sc = new Scanner(System.in); 14 | 15 | while(true) 16 | { 17 | System.out.println("Automated Teller Machine"); 18 | System.out.println("Choose 1 for Withdraw"); 19 | System.out.println("Choose 2 for Deposit"); 20 | System.out.println("Choose 3 for Check Balance"); 21 | System.out.println("Choose 4 for EXIT"); 22 | System.out.print("Choose the operation you want to perform:"); 23 | 24 | //get choice from user 25 | int choice = sc.nextInt(); 26 | switch(choice) 27 | { 28 | case 1: 29 | System.out.print("Enter money to be withdrawn:"); 30 | 31 | //get the withdrawl money from user 32 | withdraw = sc.nextInt(); 33 | 34 | //check whether the balance is greater than or equal to the withdrawal amount 35 | if(balance >= withdraw) 36 | { 37 | //remove the withdrawl amount from the total balance 38 | balance = balance - withdraw; 39 | System.out.println("Please collect your money"); 40 | } 41 | else 42 | { 43 | //show custom error message 44 | System.out.println("Insufficient Balance"); 45 | } 46 | System.out.println(""); 47 | break; 48 | 49 | case 2: 50 | 51 | System.out.print("Enter money to be deposited:"); 52 | 53 | //get deposite amount from te user 54 | deposit = sc.nextInt(); 55 | 56 | //add the deposit amount to the total balanace 57 | balance = balance + deposit; 58 | System.out.println("Your Money has been successfully depsited"); 59 | System.out.println(""); 60 | break; 61 | 62 | case 3: 63 | //displaying the total balance of the user 64 | System.out.println("Balance : "+balance); 65 | System.out.println(""); 66 | break; 67 | 68 | case 4: 69 | //exit from the menu 70 | System.exit(0); 71 | } 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /autobiographical_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class AutobiographicalNumberExample 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.print("Enter the number you want to check: "); 8 | //reading an integer from the user to check 9 | int num = sc.nextInt(); 10 | //determines the absolute value of the given number 11 | num = Math.abs(num); 12 | //assigning the value of num into variable n 13 | int n = num; 14 | //the valueOf() method returns the string representation of int argument 15 | String str = String.valueOf(num); 16 | //creates an array of digits 17 | int digitarray[] = new int[str.length()]; 18 | for(int i = digitarray.length - 1; i >= 0; i--) 19 | { 20 | //determines the last digit of the given number 21 | digitarray[i] = n % 10; 22 | //removes the last digit 23 | n = n/10; 24 | } 25 | boolean flag = true; 26 | //an inner loop compares the iterator of the outer loop with each digit of the inner loop //if they are equal then increment the occurrence count of the digit 27 | for(int i = 0; i < digitarray.length; i++) 28 | { 29 | int count = 0; 30 | for(int j = 0; j < digitarray.length; j++) 31 | { 32 | if(i == digitarray[j]) 33 | //increments the count by 1 if the above condition returns true 34 | count++; 35 | } 36 | if(count != digitarray[i]) 37 | { 38 | flag = false; 39 | //breaks the execution if the condition becomes true 40 | break; 41 | } 42 | } 43 | if(flag) 44 | //prints if the status returns true 45 | System.out.println(num + " is an autobiographical number."); 46 | else 47 | //prints if status returns false 48 | System.out.println(num + " is not an autobiographical number."); 49 | } 50 | } -------------------------------------------------------------------------------- /automorphic.java: -------------------------------------------------------------------------------- 1 | class AutomorphicNumberExample1 2 | { 3 | //user-defined static method that checks whether the number is automorphic or not 4 | static boolean isAutomorphic(int num) 5 | { 6 | //determines the square of the specified number 7 | int square = num * num; 8 | //comparing the digits until the number becomes 0 9 | while (num > 0) 10 | { 11 | //find the remainder (last digit) of the variable num and square and comparing them 12 | if (num % 10 != square % 10) 13 | //returns false if digits are not equal 14 | return false; 15 | //reduce num and square by dividing them by 10 16 | num = num/10; 17 | square = square/10; 18 | } 19 | return true; 20 | } 21 | //Driver code 22 | public static void main(String args[]) 23 | { 24 | //number to be check 25 | //calling the method and prints the result accordingly 26 | System.out.println(isAutomorphic(76) ? "Automorphic" : "Not Automorphic"); 27 | System.out.println(isAutomorphic(13) ? "Automorphic" : "Not Automorphic"); 28 | } 29 | } -------------------------------------------------------------------------------- /bin2dec.java: -------------------------------------------------------------------------------- 1 | class BinaryToDecimalExample2{ 2 | public static void main(String args[]){ 3 | System.out.println(Integer.parseInt("1010",2)); 4 | System.out.println(Integer.parseInt("10101",2)); 5 | System.out.println(Integer.parseInt("11111",2)); 6 | }} -------------------------------------------------------------------------------- /binsearch.java: -------------------------------------------------------------------------------- 1 | class BinarySearchExample{ 2 | public static void binarySearch(int arr[], int first, int last, int key){ 3 | int mid = (first + last)/2; 4 | while( first <= last ){ 5 | if ( arr[mid] < key ){ 6 | first = mid + 1; 7 | }else if ( arr[mid] == key ){ 8 | System.out.println("Element is found at index: " + mid); 9 | break; 10 | }else{ 11 | last = mid - 1; 12 | } 13 | mid = (first + last)/2; 14 | } 15 | if ( first > last ){ 16 | System.out.println("Element is not found!"); 17 | } 18 | } 19 | public static void main(String args[]){ 20 | int arr[] = {10,20,30,40,50}; 21 | int key = 30; 22 | int last=arr.length-1; 23 | binarySearch(arr,0,last,key); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /blank_diamond.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class DiamondPatternBlank 3 | { 4 | public static void main(String[] args) 5 | { 6 | Scanner sc = new Scanner(System.in); 7 | System.out.println("Enter the number of rows you want to print: "); 8 | int i,j,k; 9 | int rows = sc.nextInt(); 10 | for (i=1; i<= rows ; i++) 11 | { 12 | for (j = rows; j > i ; j--) 13 | { 14 | System.out.print(" "); 15 | } 16 | System.out.print("*"); 17 | for (k = 1; k < 2*(i -1) ;k++) 18 | { 19 | System.out.print(" "); 20 | } 21 | if( i==1) 22 | { 23 | System.out.println(""); 24 | } 25 | else 26 | { 27 | System.out.println("*"); 28 | } 29 | } 30 | for (i=rows-1; i>= 1 ; i--) 31 | { 32 | for ( j = rows; j > i ; j--) 33 | { 34 | System.out.print(" "); 35 | } 36 | System.out.print("*"); 37 | for ( k = 1; k < 2*(i -1) ;k++) 38 | { 39 | System.out.print(" "); 40 | } 41 | if(i==1) 42 | System.out.println(""); 43 | else 44 | System.out.println("*"); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /blank_pyramid.java: -------------------------------------------------------------------------------- 1 | 2 | class TrianglePattern 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, k, rows=9; 7 | for (i=1; i<= rows ; i++) 8 | { 9 | for (j = i; j < rows ; j++) 10 | { 11 | System.out.print(" "); 12 | } 13 | for (k = 1; k <= (2*i -1) ;k++) 14 | { 15 | if(k==1 || i == rows || k==(2*i-1)) 16 | { 17 | System.out.print("*"); 18 | } 19 | else 20 | { 21 | System.out.print(" "); 22 | } 23 | } 24 | System.out.println(""); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /bool2str.java: -------------------------------------------------------------------------------- 1 | class BooleanToStringExample1{ 2 | public static void main(String args[]){ 3 | boolean b1=true; 4 | boolean b2=false; 5 | String s1=String.valueOf(b1); 6 | String s2=String.valueOf(b2); 7 | System.out.println(s1); 8 | System.out.println(s2); 9 | } 10 | } -------------------------------------------------------------------------------- /bouncy_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class BouncyNumberExample1 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner scan = new Scanner(System.in); 7 | System.out.print("Enter any number you want to check: "); 8 | //reading an integer from the user 9 | int inputNumber = scan.nextInt(); 10 | //if any of the following condition returns true, the number id not bouncy 11 | if (isIncreasing(inputNumber) || isDecreasing(inputNumber) || inputNumber < 101) 12 | //prints if the number is not bouncy 13 | System.out.println(inputNumber+" not a bouncy number."); 14 | else 15 | //prints if the number is bouncy 16 | System.out.println(inputNumber+" is a bouncy number."); 17 | } 18 | //function that checks if the number is an increasing number or not 19 | public static boolean isIncreasing(int inputNumber) 20 | { 21 | //converts the number into string 22 | String str = Integer.toString(inputNumber); 23 | char digit; 24 | //flag set to true 25 | boolean flag = true; 26 | //iterates over the string up to length-1 27 | for(int i=0;i < str.length()-1;i++) 28 | { 29 | digit = str.charAt(i); 30 | //if any digit is greater than check next digit, it will not check further 31 | if(digit > str.charAt(i+1)) 32 | { 33 | //flag set to false if the condition returns true 34 | flag = false; 35 | break; 36 | } 37 | } 38 | return flag; 39 | } 40 | //function that checks if the number is a decreasing number or not 41 | public static boolean isDecreasing(int inputNumber) 42 | { 43 | //converts the number into string 44 | String str = Integer.toString(inputNumber); 45 | char digit; 46 | //flag set to true 47 | boolean flag = true; 48 | //iterates over the string up to length-1 49 | for(int i=0;i < str.length()-1;i++) 50 | { 51 | digit = str.charAt(i); 52 | //if any digit is less than the next digit, it will not check further 53 | if(digit < str.charAt(i+1)) 54 | { 55 | //flag set to false if the condition returns true 56 | flag = false; 57 | break; 58 | } 59 | } 60 | return flag; 61 | } 62 | } -------------------------------------------------------------------------------- /bubblesort.java: -------------------------------------------------------------------------------- 1 | public class BubbleSortExample { 2 | static void bubbleSort(int[] arr) { 3 | int n = arr.length; 4 | int temp = 0; 5 | for(int i=0; i < n; i++){ 6 | for(int j=1; j < (n-i); j++){ 7 | if(arr[j-1] > arr[j]){ 8 | //swap elements 9 | temp = arr[j-1]; 10 | arr[j-1] = arr[j]; 11 | arr[j] = temp; 12 | } 13 | 14 | } 15 | } 16 | 17 | } 18 | public static void main(String[] args) { 19 | int arr[] ={3,60,35,2,45,320,5}; 20 | 21 | System.out.println("Array Before Bubble Sort"); 22 | for(int i=0; i < arr.length; i++){ 23 | System.out.print(arr[i] + " "); 24 | } 25 | System.out.println(); 26 | 27 | bubbleSort(arr);//sorting array elements using bubble sort 28 | 29 | System.out.println("Array After Bubble Sort"); 30 | for(int i=0; i < arr.length; i++){ 31 | System.out.print(arr[i] + " "); 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /buzz_num.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //create FindAllBuzzNumber class to get all the Buzz number in a given range 4 | class FindAllBuzzNumbers 5 | { 6 | //main() method start 7 | public static void main(String args[]) 8 | { 9 | int range; 10 | 11 | //create scanner class object 12 | Scanner sc=new Scanner(System.in); 13 | 14 | //show custom message 15 | System.out.println("Enter the value of range"); 16 | 17 | //store user entered value into variable range 18 | range = sc.nextInt(); 19 | 20 | for(int i = 1; i <= range; i++) 21 | checkNumber(i); 22 | } 23 | 24 | // create checkNumber() method to check Buzz number 25 | static void checkNumber(int number) 26 | { 27 | // check whether the number ends with 7, is divisible by 7 or not 28 | if(number % 10 == 7 || number % 7 == 0) 29 | System.out.println(number + " is an Buzz number"); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /chaddi.java: -------------------------------------------------------------------------------- 1 | class ReversePyramidPattern 2 | { 3 | public static void main(String[] args) 4 | { 5 | int rows=8; 6 | for (int i= 0; i<= rows-1; i++) 7 | { 8 | for (int j=0; j<=i; j++) 9 | { 10 | System.out.print(" "); 11 | } 12 | for (int k=0; k<=rows-1-i; k++) 13 | { 14 | System.out.print("*" + " "); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /char2str.java: -------------------------------------------------------------------------------- 1 | class CharToStringExample2{ 2 | public static void main(String args[]){ 3 | char c='M'; 4 | String s=Character.toString(c); 5 | System.out.println("String is: "+s); 6 | }} -------------------------------------------------------------------------------- /char_kotlin.java: -------------------------------------------------------------------------------- 1 | class KshapePattern 2 | { 3 | public static void main(String[] args) 4 | { 5 | for (int i = 8; i >= 0; i--) 6 | { 7 | int alphabet = 65; 8 | for (int j = 0; j <= i; j++) 9 | { 10 | System.out.print((char) (alphabet + j) + " "); 11 | } 12 | System.out.println(); 13 | } 14 | for (int i = 0; i<= 8; i++) 15 | { 16 | int alphabet = 65; 17 | for (int j = 0; j <= i; j++) 18 | { 19 | System.out.print((char) (alphabet + j) + " "); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /char_pyramid.java: -------------------------------------------------------------------------------- 1 | class TriangleCharacterPattern 2 | { 3 | public static void main(String[] args) 4 | { 5 | for (int i = 0; i <= 8; i++) 6 | { 7 | int alphabet = 65; 8 | for (int j = 8; j > i; j--) 9 | { 10 | System.out.print(" "); 11 | } 12 | for (int k = 0; k <= i; k++) 13 | { 14 | System.out.print((char) (alphabet + k) + " "); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /char_tripat_1.java: -------------------------------------------------------------------------------- 1 | class RightAlphabaticPattern 2 | { 3 | public static void main(String[] args) 4 | { 5 | int alphabet = 65; //ASCII value of capital A is 65 6 | //inner loop for rows 7 | for (int i = 0; i <= 8; i++) 8 | { 9 | //outer loop for columns 10 | for (int j = 0; j <= i; j++) 11 | { 12 | //adds the value of j in the ASCII value of A and prints the corresponding alphabet 13 | System.out.print((char) (alphabet + j) + " "); 14 | } 15 | System.out.println(); 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /char_tripat_2.java: -------------------------------------------------------------------------------- 1 | class RepeatingPattern 2 | { 3 | public static void main(String[] args) 4 | { 5 | int letter = 65; //ASCII value of capital A is 65 6 | //inner loop for rwos 7 | for (int i = 0; i<= 9; i++) 8 | { 9 | //outer loop for columns 10 | for (int j = 0; j <= i; j++) 11 | { 12 | //prints the character 13 | System.out.print((char) letter + " "); 14 | } 15 | letter++; 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /chr2int.java: -------------------------------------------------------------------------------- 1 | class CharToIntExample1{ 2 | public static void main(String args[]){ 3 | char c='a'; 4 | char c2='1'; 5 | int a=c; 6 | int b=c2; 7 | System.out.println(a); 8 | System.out.println(b); 9 | }} -------------------------------------------------------------------------------- /date2str.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | import java.util.Calendar; 5 | class DateToStringExample1 { 6 | public static void main(String args[]){ 7 | Date date = Calendar.getInstance().getTime(); 8 | DateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); 9 | String strDate = dateFormat.format(date); 10 | System.out.println("Converted String: " + strDate); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /date2timestp.java: -------------------------------------------------------------------------------- 1 | import java.sql.Timestamp; 2 | import java.util.Date; 3 | class DateToTimestampExample1 { 4 | public static void main(String args[]){ 5 | Date date = new Date(); 6 | Timestamp ts=new Timestamp(date.getTime()); 7 | System.out.println(ts); 8 | } 9 | } -------------------------------------------------------------------------------- /dbl2int.java: -------------------------------------------------------------------------------- 1 | class DoubleToIntExample1{ 2 | public static void main(String args[]){ 3 | double d=10.5; 4 | int i=(int)d; 5 | System.out.println(i); 6 | }} -------------------------------------------------------------------------------- /dbl2str.java: -------------------------------------------------------------------------------- 1 | class DoubleToStringExample1{ 2 | public static void main(String args[]){ 3 | double d=12.3; 4 | String s=String.valueOf(d); 5 | System.out.println(s); 6 | }} -------------------------------------------------------------------------------- /dec2bin.java: -------------------------------------------------------------------------------- 1 | class DecimalToBinaryExample2{ 2 | public static void toBinary(int decimal){ 3 | int binary[] = new int[40]; 4 | int index = 0; 5 | while(decimal > 0){ 6 | binary[index++] = decimal%2; 7 | decimal = decimal/2; 8 | } 9 | for(int i = index-1;i >= 0;i--){ 10 | System.out.print(binary[i]); 11 | } 12 | System.out.println();//new line 13 | } 14 | public static void main(String args[]){ 15 | System.out.println("Decimal of 10 is: "); 16 | toBinary(10); 17 | System.out.println("Decimal of 21 is: "); 18 | toBinary(21); 19 | System.out.println("Decimal of 31 is: "); 20 | toBinary(31); 21 | }} -------------------------------------------------------------------------------- /dec2hex.java: -------------------------------------------------------------------------------- 1 | class DecimalToHexExample1{ 2 | public static void main(String args[]){ 3 | System.out.println(Integer.toHexString(10)); 4 | System.out.println(Integer.toHexString(15)); 5 | System.out.println(Integer.toHexString(289)); 6 | }} -------------------------------------------------------------------------------- /dec2oct.java: -------------------------------------------------------------------------------- 1 | class DecimalToOctalExample1{ 2 | public static void main(String args[]){ 3 | //Using the predefined Integer.toOctalString() method 4 | //to convert decimal value into octal 5 | System.out.println(Integer.toOctalString(8)); 6 | System.out.println(Integer.toOctalString(19)); 7 | System.out.println(Integer.toOctalString(81)); 8 | }} -------------------------------------------------------------------------------- /deleteMid.java: -------------------------------------------------------------------------------- 1 | public class deleteMid{ 2 | 3 | //Represent a node of the singly linked list 4 | class Node{ 5 | int data; 6 | Node next; 7 | public Node(int data) 8 | { 9 | this.data = data; 10 | this.next = null; 11 | } 12 | } 13 | 14 | //Represent the head and tail of the singly linked list 15 | public Node head = null; 16 | public Node tail = null; 17 | 18 | public int size; 19 | //addNode() will add a new node to the list 20 | public void addNode(int data) { 21 | //Create a new node 22 | Node newNode = new Node(data); 23 | //Checks if the list is empty 24 | if(head == null) { 25 | //If list is empty, both head and tail will point to new node 26 | head = newNode; 27 | tail = newNode; 28 | } 29 | else { 30 | //newNode will be added after tail such that tail's next will point to newNode 31 | tail.next = newNode; 32 | //newNode will become new tail of the list 33 | tail = newNode; 34 | } 35 | size++; 36 | } 37 | //deleteFromMid() will delete a node from the middle of the list 38 | void deleteFromMid() { 39 | Node temp, current; 40 | //Checks if the list is empty 41 | if(head == null) { 42 | System.out.println("List is empty"); 43 | return; 44 | } 45 | else { 46 | //Store the mid position of the list 47 | int count = (size % 2 == 0) ? (size/2) : ((size+1)/2); 48 | //Checks whether the head is equal to the tail or not, if yes then the list has only one node. 49 | if( head != tail ) { 50 | //Initially, temp will point to head 51 | temp = head; 52 | current = null; 53 | //Current will point to node previous to temp 54 | //If temp is pointing to node 2 then current will point to node 1. 55 | for(int i = 0; i < count-1; i++){ 56 | current = temp; 57 | temp = temp.next; 58 | } 59 | if(current != null) { 60 | //temp is the middle that needs to be removed. 61 | //So, current node will point to node next to temp by skipping temp. 62 | current.next = temp.next; 63 | //Delete temp 64 | temp = null; 65 | } 66 | //If current points to NULL then, head and tail will point to node next to temp. 67 | else { 68 | head = tail = temp.next; 69 | //Delete temp 70 | temp = null; 71 | } 72 | } 73 | //If the list contains only one element 74 | //then it will remove it and both head and tail will point to NULL 75 | else { 76 | head = tail = null; 77 | } 78 | } 79 | size--; 80 | } 81 | //display() will display all the nodes present in the list 82 | public void display() { 83 | //Node current will point to head 84 | Node current = head; 85 | if(head == null) { 86 | System.out.println("List is empty"); 87 | return; 88 | } 89 | while(current != null) { 90 | //Prints each node by incrementing pointer 91 | System.out.print(current.data + " "); 92 | current = current.next; 93 | } 94 | System.out.println(); 95 | } 96 | 97 | public static void main(String[] args) { 98 | 99 | deleteMid sList = new deleteMid(); 100 | 101 | //Adds data to the list 102 | sList.addNode(1); 103 | sList.addNode(2); 104 | sList.addNode(3); 105 | sList.addNode(4); 106 | 107 | //Printing original list 108 | System.out.println("Original List: "); 109 | sList.display(); 110 | 111 | while(sList.head != null) { 112 | sList.deleteFromMid(); 113 | //Printing updated list 114 | System.out.println("Updated List: "); 115 | sList.display(); 116 | } 117 | } 118 | } -------------------------------------------------------------------------------- /diamond.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class DiamondPattern 3 | { 4 | public static void main(String args[]) 5 | { 6 | int row, i, j, space = 1; 7 | System.out.print("Enter the number of rows you want to print: "); 8 | Scanner sc = new Scanner(System.in); 9 | row = sc.nextInt(); 10 | space = row - 1; 11 | for (j = 1; j<= row; j++) 12 | { 13 | for (i = 1; i<= space; i++) 14 | { 15 | System.out.print(" "); 16 | } 17 | space--; 18 | for (i = 1; i <= 2 * j - 1; i++) 19 | { 20 | System.out.print("*"); 21 | } 22 | System.out.println(""); 23 | } 24 | space = 1; 25 | for (j = 1; j<= row - 1; j++) 26 | { 27 | for (i = 1; i<= space; i++) 28 | { 29 | System.out.print(" "); 30 | } 31 | space++; 32 | for (i = 1; i<= 2 * (row - j) - 1; i++) 33 | { 34 | System.out.print("*"); 35 | } 36 | System.out.println(""); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /down_right_tri.java: -------------------------------------------------------------------------------- 1 | class RightDownMirrorPattern 2 | { 3 | public static void main(String args[]) 4 | { 5 | int row=7; 6 | for (int i= row; i>= 1; i--) 7 | { 8 | for (int j=row; j>i;j--) 9 | { 10 | System.out.print(" "); 11 | } 12 | for (int k=1;k<=i;k++) 13 | { 14 | System.out.print("*"); 15 | } 16 | System.out.println(""); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /duck_num.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //create DuckNumberExample class to check whether the given number is a Duck number or not 4 | class DuckNumberExample { 5 | 6 | // create checkNumber() method that returns true when it founds number Buzz 7 | public static boolean checkNumber(int number) { 8 | 9 | // use loop to repeat steps 10 | while(number != 0) { 11 | 12 | // check whether the last digit of the number is zero or not 13 | if(number%10 == 0) 14 | return true; //return true if the number is Duck 15 | 16 | // divide the number by 10 to remove the last digit 17 | number = number / 10; 18 | } 19 | 20 | return false; //return false if the number is not Duck 21 | } 22 | // main() method start 23 | public static void main(String args[]) 24 | { 25 | int n1, n2; 26 | 27 | //create scanner class object to get input from user 28 | Scanner sc=new Scanner(System.in); 29 | 30 | //show custom message 31 | System.out.println("Enter first number"); 32 | 33 | //store user entered value into variable n1 34 | n1 = sc.nextInt(); 35 | 36 | //show custom message 37 | System.out.println("Enter second number"); 38 | 39 | //store user entered value into variable n2 40 | n2 = sc.nextInt(); 41 | 42 | if (checkNumber(n1)) 43 | System.out.println(n1 + " is a Duck number"); 44 | else 45 | System.out.println(n1 + " is not a Duck number"); 46 | if (checkNumber(n2)) 47 | System.out.println(n2 + " is a Duck number"); 48 | else 49 | System.out.println(n2 + " is not a Duck number"); 50 | } 51 | } -------------------------------------------------------------------------------- /emirp_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class EmirpNumberExample1 3 | { 4 | //function that checks the given number is prime or not 5 | public static boolean isPrime(int n) 6 | { 7 | //base case 8 | if (n <= 1) 9 | return false; 10 | //loop executes from 2 to n-1 11 | for (int i = 2; i < n; i++) 12 | if (n % i == 0) 13 | //returns false if the condition returns true 14 | return false; 15 | //returns true if the condition returns false 16 | return true; 17 | } 18 | //function that checks if the given number is emirp or not 19 | public static boolean isEmirp(int n) 20 | { 21 | //checks if the given number is prime or not 22 | if (isPrime(n) == false) 23 | return false; 24 | //variable that stores the reverse of the number 25 | int reverse = 0; 26 | //the while loop executes until the specified condition becomes false 27 | while (n != 0) 28 | { 29 | //finds the last digit of the number (n) 30 | int digit = n % 10; 31 | //finds the reverse of the given number 32 | reverse = reverse * 10 + digit; 33 | //removes the last digit 34 | n = n/10; 35 | } 36 | //calling the user-defined function that checks the reverse number is prime or not 37 | return isPrime(reverse); 38 | } 39 | //driver code 40 | public static void main(String args[]) 41 | { 42 | Scanner sc=new Scanner(System.in); 43 | System.out.print("Enter a number to check: "); 44 | //reading an integer from the user 45 | int n=sc.nextInt(); 46 | if (isEmirp(n) == true) 47 | System.out.println("Yes, the given number is an emirp number."); 48 | else 49 | System.out.println("No, the given number is not an emirp number."); 50 | } 51 | } -------------------------------------------------------------------------------- /evil_num.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //create EvilNumberExample class to check whether the given number is an Evil number or not 4 | class EvilNumberExample { 5 | 6 | // create checkNumber() method that returns true when it founds number Evil 7 | public static boolean checkNumber(int n) { 8 | 9 | // find the equivalence binary number using user defined convertToBinary() method 10 | long binaryNumber = convertToBinary(n); 11 | 12 | // find total number of 1's in binary number 13 | int count = 0; 14 | 15 | // iterate each digit of binary number 16 | while(binaryNumber != 0) { 17 | 18 | // if the last digit of binary number is 1, increase the count value 19 | if(binaryNumber % 10 == 1) 20 | count++; 21 | 22 | // remove the last digit from the number 23 | binaryNumber = binaryNumber / 10; 24 | } 25 | 26 | // check whether the value of count is even or odd 27 | if(count % 2 == 0) 28 | return true; //return true when the value of count is even 29 | 30 | //return false if the value of the count is false 31 | return false; 32 | } 33 | 34 | //create convertToBinary() method to convert the decimal value into binary 35 | private static long convertToBinary(int number) { 36 | long binaryNumber = 0; 37 | int rem = 0; 38 | int j = 1; 39 | while(number != 0) { 40 | rem = number % 2; 41 | binaryNumber += rem * j; 42 | number = number / 2; 43 | j = j * 10; 44 | } 45 | 46 | return binaryNumber; //return the binary equivalent number of the decimal number 47 | } 48 | 49 | //main() method start 50 | public static void main(String[] args) { 51 | 52 | // declare variable in which the user entered value will be store 53 | int num = 0; 54 | 55 | // create scanner class object 56 | Scanner sc = new Scanner(System.in); 57 | 58 | //display custom message 59 | System.out.print("Enter a number : "); 60 | 61 | //get input from user 62 | num = sc.nextInt(); 63 | 64 | // check whether the number is evil number or not 65 | if(checkNumber(num)) 66 | System.out.println(num + " is an evil number"); 67 | else 68 | System.out.println(num + " is not an evil number"); 69 | 70 | } 71 | } -------------------------------------------------------------------------------- /fact.java: -------------------------------------------------------------------------------- 1 | class FactorialExample{ 2 | public static void main(String args[]){ 3 | int i,fact=1; 4 | int number=5;//It is the number to calculate factorial 5 | for(i=1;i<=number;i++){ 6 | fact=fact*i; 7 | } 8 | System.out.println("Factorial of "+number+" is: "+fact); 9 | } 10 | } -------------------------------------------------------------------------------- /fascinating_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class FascinatingNumberExample1 3 | { 4 | public static void main(String args[]) 5 | { 6 | int num, n2, n3; 7 | Scanner sc=new Scanner(System.in); 8 | System.out.print("Enter any Number: "); 9 | num = sc.nextInt(); 10 | n2 = num * 2; 11 | n3 = num * 3; 12 | //concatenating num, n2, and n3 13 | String concatstr = num + "" + n2 + n3; 14 | boolean found = true; 15 | //checks all digits from 1 to 9 are present or not 16 | for(char c = '1'; c <= '9'; c++) 17 | { 18 | int count = 0; 19 | //loop counts the frequency of each digit 20 | for(int i = 0; i < concatstr.length(); i++) 21 | { 22 | char ch = concatstr.charAt(i); 23 | //compares the character of concatstr with i 24 | if(ch == c) 25 | //incerments the count by 1 if the specified condition returns true 26 | count++; 27 | } 28 | //returns true if any of the condition returns true 29 | if(count > 1 || count == 0) 30 | { 31 | found = false; 32 | break; 33 | } 34 | } 35 | if(found) 36 | System.out.println(num + " is a fascinating number."); 37 | else 38 | System.out.println(num + " is not a fascinating number."); 39 | } 40 | } -------------------------------------------------------------------------------- /fibo.java: -------------------------------------------------------------------------------- 1 | class FibonacciExample1{ 2 | public static void main(String args[]) 3 | { 4 | int n1=0,n2=1,n3,i,count=10; 5 | System.out.print(n1+" "+n2);//printing 0 and 1 6 | 7 | for(i=2;i -1) && ( array [i] > key ) ) { 8 | array [i+1] = array [i]; 9 | i--; 10 | } 11 | array[i+1] = key; 12 | } 13 | } 14 | 15 | public static void main(String a[]){ 16 | int[] arr1 = {9,14,3,2,43,11,58,22}; 17 | System.out.println("Before Insertion Sort"); 18 | for(int i:arr1){ 19 | System.out.print(i+" "); 20 | } 21 | System.out.println(); 22 | 23 | insertionSort(arr1);//sorting array using insertion sort 24 | 25 | System.out.println("After Insertion Sort"); 26 | for(int i:arr1){ 27 | System.out.print(i+" "); 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /int2char.java: -------------------------------------------------------------------------------- 1 | class IntToCharExample1{ 2 | public static void main(String args[]){ 3 | int a=65; 4 | char c=(char)a; 5 | System.out.println(c); 6 | }} -------------------------------------------------------------------------------- /int2dbl.java: -------------------------------------------------------------------------------- 1 | class IntToDoubleExample1{ 2 | public static void main(String args[]){ 3 | int i=200; 4 | double d=i; 5 | System.out.println(d); 6 | }} -------------------------------------------------------------------------------- /int2long.java: -------------------------------------------------------------------------------- 1 | class IntToLongExample1{ 2 | public static void main(String args[]){ 3 | int i=200; 4 | long l=i; 5 | System.out.println(l); 6 | }} -------------------------------------------------------------------------------- /int2str.java: -------------------------------------------------------------------------------- 1 | class IntToStringExample1{ 2 | public static void main(String args[]){ 3 | int i=200; 4 | String s=String.valueOf(i); 5 | System.out.println(i+100);//300 because + is binary plus operator 6 | System.out.println(s+100);//200100 because + is string concatenation operator 7 | }} class int2str { 8 | 9 | } 10 | -------------------------------------------------------------------------------- /keith_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class KeithNumberExample1 3 | { 4 | //user-defined function that checks if the given number is Keith or not 5 | static boolean isKeith(int x) 6 | { 7 | //List stores all the digits of the X 8 | ArrayList terms=new ArrayList(); 9 | //n denotes the number of digits 10 | int temp = x, n = 0; 11 | //executes until the condition becomes false 12 | while (temp > 0) 13 | { 14 | //determines the last digit of the number and add it to the List 15 | terms.add(temp%10); 16 | //removes the last digit 17 | temp = temp/10; 18 | //increments the number of digits (n) by 1 19 | n++; 20 | } 21 | //reverse the List 22 | Collections.reverse(terms); 23 | int next_term = 0, i = n; 24 | //finds next term for the series 25 | //loop executes until the condition returns true 26 | while (next_term < x) 27 | { 28 | next_term = 0; 29 | //next term is the sum of previous n terms (it depends on number of digits the number has) 30 | for (int j=1; j<=n; j++) 31 | next_term = next_term + terms.get(i-j); 32 | terms.add(next_term); 33 | i++; 34 | } 35 | //when the control comes out of the while loop, there will be two conditions: 36 | //either next_term will be equal to x or greater than x 37 | //if equal, the given number is Keith, else not 38 | return (next_term == x); 39 | } 40 | //driver code 41 | public static void main(String[] args) 42 | { 43 | //calling the user-defined method inside the if statement and print the result accordingly 44 | if (isKeith(19)) 45 | System.out.println("Yes, the given number is a Keith number."); 46 | else 47 | System.out.println("No, the given number is not a Keith number."); 48 | if(isKeith(742)) 49 | System.out.println("Yes, the given number is a Keith number."); 50 | else 51 | System.out.println("No, the given number is not a Keith number."); 52 | if(isKeith(4578)) 53 | System.out.println("Yes, the given number is a Keith number."); 54 | else 55 | System.out.println("No, the given number is not a Keith number."); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /kotlin_pat.java: -------------------------------------------------------------------------------- 1 | class Pattern16 2 | { 3 | public static void main(String[] args) 4 | { 5 | int i, j, rows=9; 6 | //Prints upper half pattern 7 | for (i = rows; i >= 1; i--) 8 | { 9 | for (j = 1; j <= i; j++) 10 | { 11 | System.out.print(j+" "); 12 | } 13 | System.out.println(); 14 | } 15 | //Prints lower half pattern 16 | for (i = 2; i <= rows; i++) 17 | { 18 | for (j = 1; j <= i; j++) 19 | { 20 | System.out.print(j+" "); 21 | } 22 | System.out.println(); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /krishnamurthy_num.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | //create KrishnamurthyNumber class to check whether the given number is a Krishnamurthy number or not 4 | class KrishnamurthyNumber { 5 | 6 | // create fact() method to calculate the factorial of the number 7 | static int fact(int number) 8 | { 9 | int f = 1; 10 | while (number != 0) { 11 | f = f * number; 12 | number--; 13 | } 14 | return f; 15 | } 16 | 17 | // create checkNumber() method that returns true when it founds number krishnamurthy 18 | static boolean checkNumber(int number) 19 | { 20 | int sum = 0; //initialize sum to 0 21 | 22 | int tempNumber = number; //create a copy of the original number 23 | 24 | //perform operation until tempNumber will not equal to 0 25 | while (tempNumber != 0) { 26 | // calculate the factorial of the last digit of the tempNumber and then add it to the sum 27 | sum = sum + fact(tempNumber % 10); 28 | 29 | // replace the value of tempNumber by tempNumber/10 30 | tempNumber = tempNumber / 10; 31 | } 32 | 33 | // Check whether the number is equal to the sum or not. If both are equal, number is krishnamurthy number 34 | if(sum == number) 35 | return true; 36 | else 37 | return false; 38 | } 39 | 40 | // main() method start 41 | public static void main(String[] args) 42 | { 43 | int n; //initialize variable n 44 | 45 | //create scanner class object to read data from user 46 | Scanner sc = new Scanner(System.in); 47 | 48 | //custom message 49 | System.out.println("Enter any number:"); 50 | 51 | //store user entered value into variable n 52 | n = sc.nextInt(); 53 | 54 | if (checkNumber(n)) 55 | System.out.println(n + " is a krishnamurthy number"); 56 | else 57 | System.out.println(n + "is not a krishnamurthy number"); 58 | } 59 | } -------------------------------------------------------------------------------- /left_pascals.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class LeftPascalTrianglePattern 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, k, rows; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter the number of rows you want to print: "); 9 | rows = sc.nextInt(); 10 | for (i= 1; i<= rows ; i++) 11 | { 12 | for (j=i; j =1; i--) 23 | { 24 | for(j=i; j<=rows;j++) 25 | { 26 | System.out.print(" "); 27 | } 28 | for(k=1; k=0; j--) 13 | { 14 | //prints space between two stars 15 | System.out.print(" "); 16 | } 17 | //inner loop for columns 18 | for (j=0; j<=i; j++ ) 19 | { 20 | //prints star 21 | System.out.print("* "); 22 | } 23 | //throws the cursor in a new line after printing each line 24 | System.out.println(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /linsearch.java: -------------------------------------------------------------------------------- 1 | public class LinearSearchExample{ 2 | public static int linearSearch(int[] arr, int key){ 3 | for(int i=0;i (size/2)) 28 | System.out.println("Given matrix is a sparse matrix"); 29 | else 30 | System.out.println("Given matrix is not a sparse matrix"); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /mat13.java: -------------------------------------------------------------------------------- 1 | public class mat13 { 2 | public static void main(String args[]){ 3 | //creating a matrix 4 | int original[][]={{1,3,4},{2,4,3},{3,4,5}}; 5 | 6 | //creating another matrix to store transpose of a matrix 7 | int transpose[][]=new int[3][3]; //3 rows and 3 columns 8 | 9 | //Code to transpose a matrix 10 | for(int i=0;i<3;i++){ 11 | for(int j=0;j<3;j++){ 12 | transpose[i][j]=original[j][i]; 13 | } 14 | } 15 | 16 | System.out.println("Printing Matrix without transpose:"); 17 | for(int i=0;i<3;i++){ 18 | for(int j=0;j<3;j++){ 19 | System.out.print(original[i][j]+" "); 20 | } 21 | System.out.println();//new line 22 | } 23 | System.out.println("Printing Matrix After Transpose:"); 24 | for(int i=0;i<3;i++){ 25 | for(int j=0;j<3;j++){ 26 | System.out.print(transpose[i][j]+" "); 27 | } 28 | System.out.println();//new line 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /mat2.java: -------------------------------------------------------------------------------- 1 | public class mat2 { 2 | public static void main(String args[]){ 3 | //creating two matrices 4 | int a[][]={{1,1,1},{2,2,2},{3,3,3}}; 5 | int b[][]={{1,1,1},{2,2,2},{3,3,3}}; 6 | 7 | //creating another matrix to store the multiplication of two matrices 8 | int c[][]=new int[3][3]; //3 rows and 3 columns 9 | 10 | //multiplying and printing multiplication of 2 matrices 11 | for(int i=0;i<3;i++){ 12 | for(int j=0;j<3;j++){ 13 | c[i][j]=0; 14 | for(int k=0;k<3;k++) 15 | { 16 | c[i][j]+=a[i][k]*b[k][j]; 17 | }//end of k loop 18 | System.out.print(c[i][j]+" "); //printing matrix element 19 | }//end of j loop 20 | System.out.println();//new line 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /mat3.java: -------------------------------------------------------------------------------- 1 | public class mat3 { 2 | public static void main(String[] args) { 3 | int rows, cols; 4 | 5 | //Initialize matrix a 6 | int a[][] = { 7 | {4, 5, 6}, 8 | {3, 4, 1}, 9 | {1, 2, 3} 10 | }; 11 | 12 | //Initialize matrix b 13 | int b[][] = { 14 | {2, 0, 3}, 15 | {2, 3, 1}, 16 | {1, 1, 1} 17 | }; 18 | 19 | //Calculates number of rows and columns present in given matrix 20 | rows = a.length; 21 | cols = a[0].length; 22 | 23 | //Array diff will hold the result 24 | int diff[][] = new int[rows][cols]; 25 | 26 | //Performs subtraction of matrices a and b. Store the result in matrix diff 27 | for(int i = 0; i < rows; i++){ 28 | for(int j = 0; j < cols; j++){ 29 | diff[i][j] = a[i][j] - b[i][j]; 30 | } 31 | } 32 | 33 | System.out.println("Subtraction of two matrices: "); 34 | for(int i = 0; i < rows; i++){ 35 | for(int j = 0; j < cols; j++){ 36 | System.out.print(diff[i][j] + " "); 37 | } 38 | System.out.println(); 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /mat4.java: -------------------------------------------------------------------------------- 1 | public class mat4 { 2 | public static void main(String[] args) { 3 | int row1, col1, row2, col2; 4 | boolean flag = true; 5 | 6 | //Initialize matrix a 7 | int a[][] = { 8 | {1, 2, 3}, 9 | {8, 4, 6}, 10 | {4, 5, 7} 11 | }; 12 | 13 | //Initialize matrix b 14 | int b[][] = { 15 | {1, 2, 3}, 16 | {8, 4, 6}, 17 | {4, 5, 7} 18 | }; 19 | 20 | //Calculates the number of rows and columns present in the first matrix 21 | 22 | row1 = a.length; 23 | col1 = a[0].length; 24 | 25 | //Calculates the number of rows and columns present in the second matrix 26 | 27 | row2 = b.length; 28 | col2 = b[0].length; 29 | 30 | //Checks if dimensions of both the matrices are equal 31 | if(row1 != row2 || col1 != col2){ 32 | System.out.println("Matrices are not equal"); 33 | } 34 | else { 35 | for(int i = 0; i < row1; i++){ 36 | for(int j = 0; j < col1; j++){ 37 | if(a[i][j] != b[i][j]){ 38 | flag = false; 39 | break; 40 | } 41 | } 42 | } 43 | 44 | if(flag) 45 | System.out.println("Matrices are equal"); 46 | else 47 | System.out.println("Matrices are not equal"); 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /mat5.java: -------------------------------------------------------------------------------- 1 | public class mat5 { 2 | public static void main(String[] args) { 3 | int rows, cols; 4 | //Initialize matrix a 5 | int a[][] = { 6 | {1, 2, 3}, 7 | {8, 6, 4}, 8 | {4, 5, 6} 9 | }; 10 | 11 | //Calculates number of rows and columns present in given matrix 12 | rows = a.length; 13 | cols = a[0].length; 14 | 15 | if(rows != cols){ 16 | System.out.println("Matrix should be a square matrix"); 17 | } 18 | else { 19 | //Performs required operation to convert given matrix into lower triangular matrix 20 | System.out.println("Lower triangular matrix: "); 21 | for(int i = 0; i < rows; i++){ 22 | for(int j = 0; j < cols; j++){ 23 | if(j > i) 24 | System.out.print("0 "); 25 | else 26 | System.out.print(a[i][j] + " "); 27 | } 28 | System.out.println(); 29 | } 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /mat6.java: -------------------------------------------------------------------------------- 1 | public class mat6 { 2 | public static void main(String[] args) { 3 | int rows, cols; 4 | 5 | //Initialize matrix a 6 | int a[][] = { 7 | {1, 2, 3}, 8 | {8, 6, 4}, 9 | {4, 5, 6} 10 | }; 11 | 12 | //Calculates number of rows and columns present in given matrix 13 | rows = a.length; 14 | cols = a[0].length; 15 | 16 | if(rows != cols){ 17 | System.out.println("Matrix should be a square matrix"); 18 | } 19 | else { 20 | //Performs required operation to convert given matrix into upper triangular matrix 21 | System.out.println("Upper triangular matrix: "); 22 | for(int i = 0; i < rows; i++){ 23 | for(int j = 0; j < cols; j++){ 24 | if(i > j) 25 | System.out.print("0 "); 26 | else 27 | System.out.print(a[i][j] + " "); 28 | } 29 | System.out.println(); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /mat7.java: -------------------------------------------------------------------------------- 1 | public class mat7 { 2 | public static void main(String[] args) { 3 | int rows, cols, countOdd = 0, countEven = 0; 4 | 5 | //Initialize matrix a 6 | int a[][] = { 7 | {4, 1, 3}, 8 | {3, 5, 7}, 9 | {8, 2, 6} 10 | }; 11 | 12 | //Calculates number of rows and columns present in given matrix 13 | rows = a.length; 14 | cols = a[0].length; 15 | 16 | //Counts the number of even elements and odd elements 17 | for(int i = 0; i < rows; i++){ 18 | for(int j = 0; j < cols; j++){ 19 | if(a[i][j] % 2 == 0) 20 | countEven++; 21 | else 22 | countOdd++; 23 | } 24 | } 25 | 26 | System.out.println("Frequency of odd numbers: " + countOdd); 27 | System.out.println("Frequency of even numbers: " + countEven); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /mat8.java: -------------------------------------------------------------------------------- 1 | public class mat8 { 2 | public static void main(String[] args) { 3 | int row1, col1, row2, col2; 4 | //Initialize matrix a 5 | int a[][] = { 6 | {1, 3, 2}, 7 | {3, 1, 1}, 8 | {1, 2, 2} 9 | }; 10 | 11 | //Initialize matrix b 12 | int b[][] = { 13 | {2, 1, 1}, 14 | {1, 0, 1}, 15 | {1, 3, 1} 16 | }; 17 | 18 | //Calculates number of rows and columns present in first matrix 19 | row1 = a.length; 20 | col1 = a[0].length; 21 | 22 | //Calculates the number of rows and columns present in the second matrix 23 | 24 | row2 = b.length; 25 | col2 = b[0].length; 26 | 27 | //For two matrices to be multiplied, 28 | //number of columns in first matrix must be equal to number of rows in second matrix 29 | if(col1 != row2){ 30 | System.out.println("Matrices cannot be multiplied"); 31 | } 32 | else{ 33 | //Array prod will hold the result 34 | int prod[][] = new int[row1][col2]; 35 | 36 | //Performs product of matrices a and b. Store the result in matrix prod 37 | for(int i = 0; i < row1; i++){ 38 | for(int j = 0; j < col2; j++){ 39 | for(int k = 0; k < row2; k++){ 40 | prod[i][j] = prod[i][j] + a[i][k] * b[k][j]; 41 | } 42 | } 43 | } 44 | 45 | System.out.println("Product of two matrices: "); 46 | for(int i = 0; i < row1; i++){ 47 | for(int j = 0; j < col2; j++){ 48 | System.out.print(prod[i][j] + " "); 49 | } 50 | System.out.println(); 51 | } 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /mat9.java: -------------------------------------------------------------------------------- 1 | public class mat9 { 2 | public static void main(String[] args) { 3 | int rows, cols, sumRow, sumCol; 4 | 5 | //Initialize matrix a 6 | int a[][] = { 7 | {1, 2, 3}, 8 | {4, 5, 6}, 9 | {7, 8, 9} 10 | }; 11 | 12 | //Calculates number of rows and columns present in given matrix 13 | rows = a.length; 14 | cols = a[0].length; 15 | 16 | //Calculates sum of each row of given matrix 17 | for(int i = 0; i < rows; i++){ 18 | sumRow = 0; 19 | for(int j = 0; j < cols; j++){ 20 | sumRow = sumRow + a[i][j]; 21 | } 22 | System.out.println("Sum of " + (i+1) +" row: " + sumRow); 23 | } 24 | 25 | //Calculates sum of each column of given matrix 26 | for(int i = 0; i < cols; i++){ 27 | sumCol = 0; 28 | for(int j = 0; j < rows; j++){ 29 | sumCol = sumCol + a[j][i]; 30 | } 31 | System.out.println("Sum of " + (i+1) +" column: " + sumCol); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /mystery_num.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class MysteryNumberExample1 3 | { 4 | //function that finds reverse of the given number 5 | static int reverse(int x) 6 | { 7 | //converts the given number into string 8 | String str = Integer.toString(x); 9 | //stores string 10 | String string=""; 11 | for(int i=str.length()-1;i>=0;i--) 12 | { 13 | //stores the reverse of the string 14 | string=string+str.charAt(i); 15 | } 16 | //converts the string into integer 17 | int rev=Integer.parseInt(str); 18 | //returns the reverse number 19 | return rev; 20 | } 21 | //function that checks the number is mystery or not 22 | static boolean isMysteryNo(int num) 23 | { 24 | for (int i=1; i <= num/2; i++) 25 | { 26 | //calling the function that reverse a number and assign it to j 27 | int j = reverse(i); 28 | //compares the sum of two numbers is equal to given number or not 29 | if (i + j == num) 30 | { 31 | //prints a pair of numbers whose sum is the given number 32 | System.out.println( i + " " + j); 33 | System.out.println(num+ " is a mystery number."); 34 | //returns a boolean value if pair is found 35 | return true; 36 | } 37 | } 38 | System.out.println("The given number is not a mystery number."); 39 | //returns false if pair is not found 40 | return false; 41 | } 42 | //driver code 43 | public static void main(String args[]) 44 | { 45 | Scanner sc=new Scanner(System.in); 46 | System.out.print("Enter a number: "); 47 | //reading an integer from the user 48 | int num = sc.nextInt(); 49 | //calling the user-defined function to check the number is a mystery or not 50 | isMysteryNo(num); 51 | } 52 | } -------------------------------------------------------------------------------- /neon.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class NeonNumberExample1 3 | { 4 | public static void main(String args[]) 5 | { 6 | int sum = 0, n; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter the number to check: "); 9 | //raeds an integer from the user 10 | n = sc.nextInt(); 11 | //calculate square of the given number 12 | int square = n * n; 13 | //loop executes until the condition becomes false 14 | while(square != 0) 15 | { 16 | //find the last digit of the square 17 | int digit = square % 10; 18 | //adds digits to the variable sum 19 | sum = sum + digit; 20 | //removes the last digit of the variable square 21 | square = square / 10; 22 | } 23 | //compares the given number (n) with sum 24 | if(n == sum) 25 | System.out.println(n + " is a Neon Number."); 26 | else 27 | System.out.println(n + " is not a Neon Number."); 28 | } 29 | } -------------------------------------------------------------------------------- /nth_prime.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class NthPrimeNumberExample 3 | { 4 | public static void main(String[] args) 5 | { 6 | //constructor of the Scanner class 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter the value of n to compute the nth prime number: "); 9 | //reading an integer from the user 10 | int n = sc.nextInt(); 11 | int num=1, count=0, i; 12 | while (count < n) 13 | { 14 | num=num+1; 15 | for (i = 2; i <= num; i++) 16 | { 17 | //determines the modulo and compare it with 0 18 | if (num % i == 0) 19 | { 20 | //breaks the loop if the above condition returns true 21 | break; 22 | } 23 | } 24 | if (i == num) 25 | { 26 | //increments the count variable by 1 if the number is prime 27 | count = count+1; 28 | } 29 | } 30 | //prints the nth prime number 31 | System.out.println("The " +n +"th prime number is: " + num); 32 | } 33 | } -------------------------------------------------------------------------------- /num2word.java: -------------------------------------------------------------------------------- 1 | class NumberToWordExample1 2 | { 3 | //user-defined static method that converts a number into words 4 | static void numberToWords(char num[]) 5 | { 6 | //determines the number of digits in the given number 7 | int len = num.length; 8 | //checks the given number has number or not 9 | if (len == 0) 10 | { 11 | //if the given number is empty prints the following statement 12 | System.out.println("The string is empty."); 13 | return; 14 | } 15 | //here, we have specified the length of the number to 4 16 | //it means that the number (that you want to convert) should be four or less than four digits 17 | if (len > 4) 18 | { 19 | //if the given number is more than four-digit number, it prints the following statement 20 | System.out.println("\n The given number has more than 4 digits."); 21 | return; 22 | } 23 | //string type array for one-digit numbers 24 | String[] onedigit = new String[] {"Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"}; 25 | //string type array for two digits numbers 26 | //the first index is empty because it makes indexing easy 27 | String[] twodigits = new String[] {"", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"}; 28 | //string type array of tens multiples 29 | //the first two indexes are empty because it makes indexing easy 30 | String[] multipleoftens = new String[] {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"}; 31 | //string type array of power of tens 32 | String[] poweroftens = new String[] {"Hundred", "Thousand"}; 33 | //Used for debugging purpose only 34 | //the valueOf() method returns the string representation of the character array argument 35 | System.out.print(String.valueOf(num) + ": "); 36 | //checks whether the length of the given string is one or not 37 | if (len == 1) 38 | { 39 | //if the above condition returns true, it accesses the corresponding index and prints the value of that index 40 | //[num[0]-'0']: getting the number equal the decimal value of the character (assuming the char is the digit) 41 | System.out.println(onedigit[num[0]-'0']); 42 | return; 43 | } 44 | int x = 0; 45 | //executes until num does not become not '\0' 46 | while (x < num.length) 47 | { 48 | //executes if the length of the string is greater than equal to three 49 | if (len >= 3) 50 | { 51 | if (num[x] - '0' != 0) 52 | { 53 | System.out.print(onedigit[num[x] - '0'] + " "); 54 | //here length can be 3 or 4 55 | System.out.print(poweroftens[len - 3]+ " "); 56 | } 57 | //decrements the length of the string by 1 58 | --len; 59 | } 60 | //executes if the given number has two digits 61 | else 62 | { 63 | //the if-statement handles the numbers from 10 to 19 only 64 | if (num[x] - '0' == 1) 65 | { 66 | //adding the digits of the given number 67 | //the logic behind sum up the digits is that we will use the sum for accessing the index of the array 68 | //for example: 17, sum of digits = 8 69 | //we will access the 8th index in twodigits[] array i.e. Seventeen 70 | int sum = num[x] - '0' + num[x + 1] - '0'; 71 | System.out.println(twodigits[sum]); 72 | return; 73 | } 74 | //the else-if statement handles the number 20 only 75 | //compares the tens and unit place with 2 and 0 respectively 76 | else if (num[x] - '0' == 2 && num[x + 1] - '0' == 0) 77 | { 78 | //executes if the above else-if condition returns true 79 | System.out.println("Twenty"); 80 | return; 81 | } 82 | //the else block handles the numbers from 21 to 100 83 | else 84 | { 85 | int i = (num[x] - '0'); 86 | if (i > 0) 87 | //prints the ith index element of the array multipleoftens[] 88 | System.out.print(multipleoftens[i]+ " "); 89 | else 90 | //prints space 91 | System.out.print(""); 92 | //increments the variable i by 1 93 | ++x; 94 | //checks whether the number is not equal to zero, it means the number has only a digit 95 | if (num[x] - '0' != 0) 96 | //prints the ith index element of the array onedigit[] 97 | System.out.println(onedigit[num[x] - '0']); 98 | } 99 | } 100 | //increments the variable i by 1 101 | ++x; 102 | } 103 | } 104 | //main() method 105 | public static void main(String args[]) 106 | { 107 | //calling the user-defined method and that invokes another predefined method toCharArray() 108 | //the method toCharArray() converts the given number into character array 109 | numberToWords("1111".toCharArray()); 110 | numberToWords("673".toCharArray()); 111 | numberToWords("85".toCharArray()); 112 | numberToWords("5".toCharArray()); 113 | numberToWords("0".toCharArray()); 114 | numberToWords("20".toCharArray()); 115 | numberToWords("1000".toCharArray()); 116 | numberToWords("12345".toCharArray()); 117 | //passing empty string 118 | numberToWords("".toCharArray()); 119 | } 120 | } -------------------------------------------------------------------------------- /num_diamond.java: -------------------------------------------------------------------------------- 1 | class Pattern4 2 | { 3 | public static void main(String[] args) 4 | { 5 | for (int i = 1; i <= 4; i++) 6 | { 7 | int n = 8; 8 | for (int j = 1; j<= n - i; j++) 9 | { 10 | System.out.print(" "); 11 | } 12 | for (int k = i; k >= 1; k--) 13 | { 14 | System.out.print(k); 15 | } 16 | for (int l = 2; l <= i; l++) 17 | { 18 | System.out.print(l); 19 | } 20 | System.out.println(); 21 | } 22 | for (int i = 3; i >= 1; i--) 23 | { 24 | int n = 10; 25 | for (int j = 0; j<= n - i; j++) 26 | { 27 | System.out.print(" "); 28 | } 29 | for (int k = i; k >= 1; k--) 30 | { 31 | System.out.print(k); 32 | } 33 | for (int l = 2; l <= i; l++) 34 | { 35 | System.out.print(l); 36 | } 37 | System.out.println(); 38 | } 39 | } 40 | } -------------------------------------------------------------------------------- /num_hourglass.java: -------------------------------------------------------------------------------- 1 | class Pattern10 2 | { 3 | public static void main(String[] args) 4 | { 5 | int n = 10; 6 | for (int i = 1; i <= n; i++) 7 | { 8 | for (int j = 1; j < i; j++) 9 | { 10 | System.out.print(" "); 11 | } 12 | for (int k = i; k <= n; k++) 13 | { 14 | System.out.print(k+" "); 15 | } 16 | System.out.println(); 17 | } 18 | for (int i = n-1; i >= 1; i--) 19 | { 20 | for (int j = 1; j < i; j++) 21 | { 22 | System.out.print(" "); 23 | } 24 | for (int k = i; k <= n; k++) 25 | { 26 | System.out.print(k+" "); 27 | } 28 | System.out.println(); 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /numpat_1.java: -------------------------------------------------------------------------------- 1 | class Pattern8 2 | { 3 | public static void main(String[] args) 4 | { 5 | int rows=9; //number of rows to print 6 | for (int i = 1; i <= rows; i++) 7 | { 8 | int num; 9 | if(i%2 == 0) 10 | { 11 | num = 0; 12 | for (int j = 1; j <= rows; j++) 13 | { 14 | System.out.print(num); 15 | num = (num == 0)? 1 : 0; 16 | } 17 | } 18 | else 19 | { 20 | num = 1; 21 | for (int j = 1; j <= rows; j++) 22 | { 23 | System.out.print(num); 24 | num = (num == 0)? 1 : 0; 25 | } 26 | } 27 | System.out.println(); 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /numpat_2.java: -------------------------------------------------------------------------------- 1 | class Pattern18 2 | { 3 | public static void main(String[] args) 4 | { 5 | int rows=8; 6 | for (int i = 1; i <= rows; i++) 7 | { 8 | for (int j = 1; j <= rows-i; j++) 9 | { 10 | System.out.print(1); 11 | } 12 | for (int j = 1; j <= i; j++) 13 | { 14 | System.out.print(i); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /numpat_3.java: -------------------------------------------------------------------------------- 1 | class Pattern20 2 | { 3 | public static void main(String[] args) 4 | { 5 | int i, j, k, rows=9; 6 | for(i=1;i< rows+1 ;i++) 7 | { 8 | for(j=i; j < rows+1 ;j++) 9 | { 10 | System.out.print(j + " "); 11 | } 12 | for(k=1; k < i ;k++) 13 | { 14 | System.out.print(k + " "); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /numpat_box.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class Pattern21 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, min, n; //n is the number up to you want to print 7 | System.out.print("Enter the value of n: "); 8 | Scanner sc= new Scanner(System.in); 9 | n=sc.nextInt(); 10 | //loo for upper left part 11 | for (i = 1; i <= n; i++) 12 | { 13 | for (j = 1; j <= n; j++) 14 | { 15 | min = i < j ? i : j; 16 | System.out.print(n - min + 1+ " "); 17 | } 18 | //loop for upper right part 19 | for (j = n - 1; j >= 1; j--) 20 | { 21 | min = i < j ? i : j; 22 | System.out.print(n - min + 1+ " "); 23 | } 24 | System.out.println(); 25 | } 26 | //loop for lower left part 27 | for (i = n - 1; i >= 1; i--) 28 | { 29 | for (j = 1; j <= n; j++) 30 | { 31 | min = i < j ? i : j; 32 | System.out.print(n - min + 1+ " "); 33 | } 34 | //loop for lower right part 35 | for (j = n - 1; j >= 1; j--) 36 | { 37 | min = i < j ? i : j; 38 | System.out.print(n - min + 1+ " "); 39 | } 40 | System.out.println(); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /obj2str.java: -------------------------------------------------------------------------------- 1 | class Emp{} 2 | class ObjectToStringExample{ 3 | public static void main(String args[]){ 4 | Emp e=new Emp(); 5 | String s=e.toString(); 6 | String s2=String.valueOf(e); 7 | System.out.println(s); 8 | System.out.println(s2); 9 | }} -------------------------------------------------------------------------------- /oct2dec.java: -------------------------------------------------------------------------------- 1 | class OctalToDecimalExample1{ 2 | public static void main(String args[]){ 3 | //Declaring an octal number 4 | String octalString="121"; 5 | //Converting octal number into decimal 6 | int decimal=Integer.parseInt(octalString,8); 7 | //Printing converted decimal number 8 | System.out.println(decimal); 9 | }} -------------------------------------------------------------------------------- /palin.java: -------------------------------------------------------------------------------- 1 | class PalindromeExample{ 2 | public static void main(String args[]){ 3 | int r,sum=0,temp; 4 | int n=454;//It is the number variable to be checked for palindrome 5 | 6 | temp=n; 7 | while(n>0){ 8 | r=n%10; //getting remainder 9 | sum=(sum*10)+r; 10 | n=n/10; 11 | } 12 | if(temp==sum) 13 | System.out.println("palindrome number "); 14 | else 15 | System.out.println("not palindrome"); 16 | } 17 | } -------------------------------------------------------------------------------- /perfect_sq.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class CheckPerfectSquareExample1 3 | { 4 | //user-defined method that checks the number is perfect square or not 5 | static boolean checkPerfectSquare(double number) 6 | { 7 | //calculating the square root of the given number 8 | double sqrt=Math.sqrt(number); 9 | //finds the floor value of the square root and comparing it with zero 10 | return ((sqrt - Math.floor(sqrt)) == 0); 11 | } 12 | //main method 13 | public static void main(String[] args) 14 | { 15 | System.out.print("Enter any number: "); 16 | //object of the Scanner class 17 | Scanner sc=new Scanner(System.in); 18 | //reading a number of type double from the user 19 | double number=sc.nextDouble(); 20 | //calling the user defined method 21 | if (checkPerfectSquare(number)) 22 | System.out.print("Yes, the given number is perfect square."); 23 | else 24 | System.out.print("No, the given number is not perfect square."); 25 | } 26 | } -------------------------------------------------------------------------------- /peterson_num.java: -------------------------------------------------------------------------------- 1 | 2 | import java.util.*; 3 | class PetersonNumberExample1 4 | { 5 | //an array is defined for the quickly find the factorial 6 | static long[] factorial = new long[] { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600}; 7 | //driver code 8 | public static void main(String args[]) 9 | { 10 | //constructor of the Scanner class 11 | Scanner sc = new Scanner(System.in); 12 | System.out.print("Enter a number to check: "); 13 | //reading a number from the user 14 | int n=sc.nextInt(); 15 | //calling the user-defined function to check Peterson number 16 | if (isPeterson(n)) 17 | System.out.println("The given number is a Peterson number."); 18 | else 19 | System.out.println("The given number is not a Peterson number."); 20 | } 21 | //function to check the given number is Peterson or not 22 | static boolean isPeterson(int n) 23 | { 24 | int num = n; 25 | int sum = 0; 26 | //loop executes until the condition becomes false 27 | while (n > 0) 28 | { 29 | //determines the last digit of the given number 30 | int digit = n % 10; 31 | //determines the factorial of the digit and add it to the variable sum 32 | sum += factorial[digit]; 33 | //removes the last digit of the given number 34 | n = n / 10; 35 | } 36 | //compares sum with num if they are equal returns the number itself 37 | return (sum == num); 38 | } 39 | } -------------------------------------------------------------------------------- /prime.java: -------------------------------------------------------------------------------- 1 | class Prime{ 2 | public static void main(String args[]){ 3 | int i,m=0,flag=0; 4 | int n=3;//it is the number to be checked 5 | m=n/2; 6 | if(n==0||n==1){ 7 | System.out.println(n+" is not prime number"); 8 | }else{ 9 | for(i=2;i<=m;i++){ 10 | if(n%i==0){ 11 | System.out.println(n+" is not prime number"); 12 | flag=1; 13 | break; 14 | } 15 | } 16 | if(flag==0) { System.out.println(n+" is prime number"); } 17 | }//end of else 18 | } 19 | } -------------------------------------------------------------------------------- /pyramid.java: -------------------------------------------------------------------------------- 1 | class PyramidPattern 2 | { 3 | public static void main(String args[]) 4 | { 5 | //i for rows and j for columns 6 | //row denotes the number of rows you want to print 7 | int i, j, row = 6; 8 | //Outer loop work for rows 9 | for (i=0; i1; j--) 13 | { 14 | //prints space between two stars 15 | System.out.print(" "); 16 | } 17 | //inner loop for columns 18 | for (j=0; j<=i; j++ ) 19 | { 20 | //prints star 21 | System.out.print("* "); 22 | } 23 | //throws the cursor in a new line after printing each line 24 | System.out.println(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /random.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | class RandomNumberExample3 3 | { 4 | public static void main(String args[]) 5 | { 6 | // creating an object of Random class 7 | Random random = new Random(); 8 | // Generates random integers 0 to 49 9 | int x = random.nextInt(50); 10 | // Generates random integers 0 to 999 11 | int y = random.nextInt(1000); 12 | // Prints random integer values 13 | System.out.println("Randomly Generated Integers Values"); 14 | System.out.println(x); 15 | System.out.println(y); 16 | // Generates Random doubles values 17 | double a = random.nextDouble(); 18 | double b = random.nextDouble(); 19 | // Prints random double values 20 | System.out.println("Randomly Generated Double Values"); 21 | System.out.println(a); 22 | System.out.println(b); 23 | // Generates Random float values 24 | float f=random.nextFloat(); 25 | float i=random.nextFloat(); 26 | // Prints random float values 27 | System.out.println("Randomly Generated Float Values"); 28 | System.out.println(f); 29 | System.out.println(i); 30 | // Generates Random Long values 31 | long p = random.nextLong(); 32 | long q = random.nextLong(); 33 | // Prints random long values 34 | System.out.println("Randomly Generated Long Values"); 35 | System.out.println(p); 36 | System.out.println(q); 37 | // Generates Random boolean values 38 | boolean m=random.nextBoolean(); 39 | boolean n=random.nextBoolean(); 40 | // Prints random boolean values 41 | System.out.println("Randomly Generated Boolean Values"); 42 | System.out.println(m); 43 | System.out.println(n); 44 | } 45 | } -------------------------------------------------------------------------------- /rev_blank_pyramid.java: -------------------------------------------------------------------------------- 1 | 2 | class DownTrianglePattern 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, k, rows=9; 7 | for (i=rows; i>= 1 ; i--) 8 | { 9 | for (j = i; j= 1; i--) 23 | { 24 | //Prints i spaces at the beginning of each row 25 | for (int j = 1; j < i; j++) 26 | { 27 | System.out.print(" "); 28 | } 29 | //Prints i to rows value at the end of each row 30 | for (int j = i; j <= rows; j++) 31 | { 32 | System.out.print(j); 33 | } 34 | System.out.println(); 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /reverse.java: -------------------------------------------------------------------------------- 1 | class ReverseNumberExample1 2 | { 3 | public static void main(String[] args) 4 | { 5 | int number = 987654, reverse = 0; 6 | while(number != 0) 7 | { 8 | int remainder = number % 10; 9 | reverse = reverse * 10 + remainder; 10 | number = number/10; 11 | } 12 | System.out.println("The reverse of the given number is: " + reverse); 13 | } 14 | } -------------------------------------------------------------------------------- /right_pascals.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class RightPascalTrianglePattern 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, rows; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter the number of rows you want to print: "); 9 | rows = sc.nextInt(); 10 | for (i= 0; i<= rows-1; i++) 11 | { 12 | for (j=0; j<=i; j++) 13 | { 14 | System.out.print("*"+ " "); 15 | } 16 | System.out.println(""); 17 | } 18 | for (i=rows-1; i>=0; i--) 19 | { 20 | for(j=0; j <= i-1;j++) 21 | { 22 | System.out.print("*"+ " "); 23 | } 24 | System.out.println(""); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /right_triangle_pat.java: -------------------------------------------------------------------------------- 1 | class RightTrianglePattern 2 | { 3 | public static void main(String args[]) 4 | { 5 | //i for rows and j for columns 6 | //row denotes the number of rows you want to print 7 | int i, j, row=6; 8 | //outer loop for rows 9 | for(i=0; i= 0; i--) 23 | { 24 | for (j=0; j1) 9 | { 10 | if(n%i==0) 11 | { 12 | sum=sum+findSumOfDigit(i); 13 | n=n/i; 14 | } 15 | else 16 | { 17 | do 18 | { 19 | i++; 20 | } 21 | while(!isPrime(i)); 22 | } 23 | } 24 | //returns the sum of digits of prime factors 25 | return sum; 26 | } 27 | //function finds the sum of digits of the given numbers 28 | static int findSumOfDigit(int n) 29 | { 30 | //stores the sum 31 | int s=0; 32 | while(n>0) 33 | { 34 | //finds the last digit of the number and add it to the variable s 35 | s=s+n%10; 36 | //removes the last digit from the given number 37 | n=n/10; 38 | } 39 | //returns the sum of digits of the number 40 | return s; 41 | } 42 | //function checks if the factor is prime or not 43 | static boolean isPrime(int k) 44 | { 45 | boolean b=true; 46 | int d=2; 47 | while(d0) 14 | { 15 | //finds the last digit of the number 16 | lastdigit=num%10; 17 | //adds last digit to the variable sum 18 | sum=sum+lastdigit; 19 | //calculates the product 20 | product=product*lastdigit; 21 | //removes the last digit from the given number 22 | num=num/10; 23 | } 24 | //compares the sum and product 25 | if(sum==product) 26 | //prints if the above condition returns true 27 | System.out.println("The given number is a spy number."); 28 | else 29 | //prints if the above condition returns false 30 | System.out.println("The given number is not a spy number."); 31 | } 32 | } -------------------------------------------------------------------------------- /str1.java: -------------------------------------------------------------------------------- 1 | public class str1 2 | { 3 | public static void main(String[] args) { 4 | String string = "The best of both worlds"; 5 | int count = 0; 6 | 7 | //Counts each character except space 8 | for(int i = 0; i < string.length(); i++) { 9 | if(string.charAt(i) != ' ') 10 | count++; 11 | } 12 | 13 | //Displays the total number of characters present in the given string 14 | System.out.println("Total number of characters in a string: " + count); 15 | } 16 | } -------------------------------------------------------------------------------- /str10.java: -------------------------------------------------------------------------------- 1 | public class str10 { 2 | public static void main(String[] args) { 3 | 4 | String str1="Great Power"; 5 | StringBuffer newStr=new StringBuffer(str1); 6 | 7 | for(int i = 0; i < str1.length(); i++) { 8 | 9 | //Checks for lower case character 10 | if(Character.isLowerCase(str1.charAt(i))) { 11 | //Convert it into upper case using toUpperCase() function 12 | newStr.setCharAt(i, Character.toUpperCase(str1.charAt(i))); 13 | } 14 | //Checks for upper case character 15 | else if(Character.isUpperCase(str1.charAt(i))) { 16 | //Convert it into upper case using toLowerCase() function 17 | newStr.setCharAt(i, Character.toLowerCase(str1.charAt(i))); 18 | } 19 | } 20 | System.out.println("String after case conversion : " + newStr); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /str11.java: -------------------------------------------------------------------------------- 1 | public class str11 { 2 | public static void main(String[] args) { 3 | String string = "Once in a blue moon"; 4 | char ch = '-'; 5 | 6 | //Replace space with specific character ch 7 | string = string.replace(' ', ch); 8 | 9 | System.out.println("String after replacing spaces with given character: "); 10 | System.out.println(string); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /str12.java: -------------------------------------------------------------------------------- 1 | public class str12 { 2 | public static void main(String[] args) { 3 | String string = "Kayak"; 4 | boolean flag = true; 5 | 6 | //Converts the given string into lowercase 7 | string = string.toLowerCase(); 8 | 9 | //Iterate the string forward and backward, compare one character at a time 10 | //till middle of the string is reached 11 | for(int i = 0; i < string.length()/2; i++){ 12 | if(string.charAt(i) != string.charAt(string.length()-i-1)){ 13 | flag = false; 14 | break; 15 | } 16 | } 17 | if(flag) 18 | System.out.println("Given string is palindrome"); 19 | else 20 | System.out.println("Given string is not a palindrome"); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /str13.java: -------------------------------------------------------------------------------- 1 | public class str13 { 2 | public static void main(String[] args) { 3 | String str1 = "abcde", str2 = "deabc"; 4 | 5 | if(str1.length() != str2.length()){ 6 | System.out.println("Second string is not a rotation of first string"); 7 | } 8 | else { 9 | //Concatenate str1 with str1 and store it in str1 10 | str1 = str1.concat(str1); 11 | //Check whether str2 is present in str1 12 | if(str1.indexOf(str2) != -1) 13 | System.out.println("Second string is a rotation of first string"); 14 | else 15 | System.out.println("Second string is not a rotation of first string"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /str14.java: -------------------------------------------------------------------------------- 1 | public class str14 { 2 | public static void main(String[] args) { 3 | String str = "grass is greener on the other side"; 4 | int[] freq = new int[str.length()]; 5 | char minChar = str.charAt(0), maxChar = str.charAt(0); 6 | int i, j, min, max; 7 | 8 | //Converts given string into character array 9 | char string[] = str.toCharArray(); 10 | 11 | //Count each word in given string and store in array freq 12 | for(i = 0; i < string.length; i++) { 13 | freq[i] = 1; 14 | for(j = i+1; j < string.length; j++) { 15 | if(string[i] == string[j] && string[i] != ' ' && string[i] != '0') { 16 | freq[i]++; 17 | 18 | //Set string[j] to 0 to avoid printing visited character 19 | string[j] = '0'; 20 | } 21 | } 22 | } 23 | 24 | //Determine minimum and maximum occurring characters 25 | min = max = freq[0]; 26 | for(i = 0; i freq[i] && freq[i] != '0') { 31 | min = freq[i]; 32 | minChar = string[i]; 33 | } 34 | //If max is less than frequency of a character 35 | //then, store frequency in max and corresponding character in maxChar 36 | if(max < freq[i]) { 37 | max = freq[i]; 38 | maxChar = string[i]; 39 | } 40 | } 41 | 42 | System.out.println("Minimum occurring character: " + minChar); 43 | System.out.println("Maximum occurring character: " + maxChar); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /str15.java: -------------------------------------------------------------------------------- 1 | public class str15 { 2 | public static void main(String[] args) { 3 | String string = "Dream big"; 4 | //Stores the reverse of given string 5 | String reversedStr = ""; 6 | 7 | //Iterate through the string from last and add each character to variable reversedStr 8 | for(int i = string.length()-1; i >= 0; i--){ 9 | reversedStr = reversedStr + string.charAt(i); 10 | } 11 | 12 | System.out.println("Original string: " + string); 13 | //Displays the reverse of given string 14 | System.out.println("Reverse of given string: " + reversedStr); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /str16.java: -------------------------------------------------------------------------------- 1 | public class str16 { 2 | public static void main(String[] args) { 3 | String string1 = "Great responsibility"; 4 | int count; 5 | 6 | //Converts given string into character array 7 | char string[] = string1.toCharArray(); 8 | 9 | System.out.println("Duplicate characters in a given string: "); 10 | //Counts each character present in the string 11 | for(int i = 0; i 1 && string[i] != '0') 22 | System.out.println(string[i]); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /str17.java: -------------------------------------------------------------------------------- 1 | public class str17 { 2 | public static void main(String[] args) { 3 | String string = "Big black bug bit a big black dog on his big black nose"; 4 | int count; 5 | 6 | //Converts the string into lowercase 7 | string = string.toLowerCase(); 8 | 9 | //Split the string into words using built-in function 10 | String words[] = string.split(" "); 11 | 12 | System.out.println("Duplicate words in a given string : "); 13 | for(int i = 0; i < words.length; i++) { 14 | count = 1; 15 | for(int j = i+1; j < words.length; j++) { 16 | if(words[i].equals(words[j])) { 17 | count++; 18 | //Set words[j] to 0 to avoid printing visited word 19 | words[j] = "0"; 20 | } 21 | } 22 | 23 | //Displays the duplicate word if count is greater than 1 24 | if(count > 1 && words[i] != "0") 25 | System.out.println(words[i]); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /str18.java: -------------------------------------------------------------------------------- 1 | public class str18 { 2 | public static void main(String[] args) { 3 | String str = "picture perfect"; 4 | int[] freq = new int[str.length()]; 5 | int i, j; 6 | 7 | //Converts given string into character array 8 | char string[] = str.toCharArray(); 9 | 10 | for(i = 0; i =1;i--) 44 | { 45 | boolean flagl=false; 46 | for(int l=1;l<=i;l++) 47 | { 48 | if(!flagl){ 49 | System.out.print("*"); 50 | flagl=true; 51 | } 52 | else 53 | { 54 | System.out.print(" "); 55 | System.out.print("*"); 56 | } 57 | } 58 | for(int l=1;l<=space;l++) 59 | { 60 | System.out.print(" "); 61 | } 62 | space=space+4; 63 | boolean flagr=false; 64 | for(int l=1;l<=i;l++) 65 | { 66 | if(!flagr){ 67 | System.out.print("*"); 68 | flagr=true; 69 | } 70 | else 71 | { 72 | System.out.print(" "); 73 | System.out.print("*"); 74 | } 75 | } 76 | System.out.print("\n"); 77 | } 78 | 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /str20.java: -------------------------------------------------------------------------------- 1 | public class str20 { 2 | public static void main(String[] args) { 3 | String string = "characters "; 4 | 5 | //Displays individual characters from given string 6 | System.out.println("Individual characters from given string: "); 7 | 8 | //Iterate through the string and display individual character 9 | for(int i = 0; i < string.length(); i++){ 10 | System.out.print(string.charAt(i) + " "); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /str2bool.java: -------------------------------------------------------------------------------- 1 | class StringToBooleanExample{ 2 | public static void main(String args[]){ 3 | String s1="true"; 4 | String s2="TRue"; 5 | String s3="ok"; 6 | boolean b1=Boolean.parseBoolean(s1); 7 | boolean b2=Boolean.parseBoolean(s2); 8 | boolean b3=Boolean.parseBoolean(s3); 9 | System.out.println(b1); 10 | System.out.println(b2); 11 | System.out.println(b3); 12 | }} -------------------------------------------------------------------------------- /str2char.java: -------------------------------------------------------------------------------- 1 | class StringToCharExample1{ 2 | public static void main(String args[]){ 3 | String s="hello"; 4 | char c=s.charAt(0);//returns h 5 | System.out.println("1st character is: "+c); 6 | } 7 | } -------------------------------------------------------------------------------- /str2date.java: -------------------------------------------------------------------------------- 1 | import java.text.SimpleDateFormat; 2 | import java.util.Date; 3 | class StringToDateExample1 { 4 | public static void main(String[] args)throws Exception { 5 | String sDate1="31/12/1998"; 6 | Date date1=new SimpleDateFormat("dd/MM/yyyy").parse(sDate1); 7 | System.out.println(sDate1+"\t"+date1); 8 | } 9 | } -------------------------------------------------------------------------------- /str2dbl.java: -------------------------------------------------------------------------------- 1 | class StringToDoubleExample{ 2 | public static void main(String args[]){ 3 | String s="23.6"; 4 | double d=Double.parseDouble(s); 5 | System.out.println(d); 6 | }} -------------------------------------------------------------------------------- /str2flt.java: -------------------------------------------------------------------------------- 1 | class StringToFloatExample{ 2 | public static void main(String args[]){ 3 | String s="23.6"; 4 | float f=Float.parseFloat(s); 5 | System.out.println(f); 6 | } 7 | } -------------------------------------------------------------------------------- /str2int.java: -------------------------------------------------------------------------------- 1 | class StringToIntExample1{ 2 | public static void main(String args[]){ 3 | //Declaring String variable 4 | String s="200"; 5 | //Converting String into int using Integer.parseInt() 6 | int i=Integer.parseInt(s); 7 | //Printing value of i 8 | System.out.println(i); 9 | } 10 | } -------------------------------------------------------------------------------- /str2long.java: -------------------------------------------------------------------------------- 1 | class StringToLongExample{ 2 | public static void main(String args[]){ 3 | String s="9990449935"; 4 | long l=Long.parseLong(s); 5 | System.out.println(l); 6 | }} -------------------------------------------------------------------------------- /str2obj.java: -------------------------------------------------------------------------------- 1 | class StringToObjectExample{ 2 | public static void main(String args[]){ 3 | String s="hello"; 4 | Object obj=s; 5 | System.out.println(obj); 6 | } 7 | } -------------------------------------------------------------------------------- /str3.java: -------------------------------------------------------------------------------- 1 | public class str3 { 2 | public static void main(String[] args) { 3 | 4 | //Counter variable to store the count of vowels and consonant 5 | int vCount = 0, cCount = 0; 6 | 7 | //Declare a string 8 | String str = "This is a really simple sentence"; 9 | 10 | //Converting entire string to lower case to reduce the comparisons 11 | str = str.toLowerCase(); 12 | 13 | for(int i = 0; i < str.length(); i++) { 14 | //Checks whether a character is a vowel 15 | if(str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i' || str.charAt(i) == 'o' || str.charAt(i) == 'u') { 16 | //Increments the vowel counter 17 | vCount++; 18 | } 19 | //Checks whether a character is a consonant 20 | else if(str.charAt(i) >= 'a' && str.charAt(i)<='z') { 21 | //Increments the consonant counter 22 | cCount++; 23 | } 24 | } 25 | System.out.println("Number of vowels: " + vCount); 26 | System.out.println("Number of consonants: " + cCount); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /str4.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class str4 { 3 | public static void main (String [] args) { 4 | String str1="Brag"; 5 | String str2="Grab"; 6 | 7 | //Converting both the string to lower case. 8 | str1 = str1.toLowerCase(); 9 | str2 = str2.toLowerCase(); 10 | 11 | //Checking for the length of strings 12 | if (str1.length() != str2.length()) { 13 | System.out.println("Both the strings are not anagram"); 14 | } 15 | else { 16 | //Converting both the arrays to character array 17 | char[] string1 = str1.toCharArray(); 18 | char[] string2 = str2.toCharArray(); 19 | 20 | //Sorting the arrays using in-built function sort () 21 | Arrays.sort(string1); 22 | Arrays.sort(string2); 23 | 24 | //Comparing both the arrays using in-built function equals () 25 | if(Arrays.equals(string1, string2) == true) { 26 | System.out.println("Both the strings are anagram"); 27 | } 28 | else { 29 | System.out.println("Both the strings are not anagram"); 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /str5.java: -------------------------------------------------------------------------------- 1 | public class str5 { 2 | public static void main(String[] args) { 3 | String str = "aaaabbbbcccc"; 4 | 5 | //Stores the length of the string 6 | int len = str.length(); 7 | //n determines the variable that divide the string in 'n' equal parts 8 | int n = 3; 9 | int temp = 0, chars = len/n; 10 | //Stores the array of string 11 | String[] equalStr = new String [n]; 12 | //Check whether a string can be divided into n equal parts 13 | if(len % n != 0) { 14 | System.out.println("Sorry this string cannot be divided into "+ n +" equal parts."); 15 | } 16 | else { 17 | for(int i = 0; i < len; i = i+chars) { 18 | //Dividing string in n equal part using substring() 19 | String part = str.substring(i, i+chars); 20 | equalStr[temp] = part; 21 | temp++; 22 | } 23 | System.out.println(n + " equal parts of given string are "); 24 | for(int i = 0; i < equalStr.length; i++) { 25 | System.out.println(equalStr[i]); 26 | } 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /str6.java: -------------------------------------------------------------------------------- 1 | public class str6 { 2 | public static void main(String[] args) { 3 | 4 | String str = "FUN"; 5 | int len = str.length(); 6 | int temp = 0; 7 | //Total possible subsets for string of size n is n*(n+1)/2 8 | String arr[] = new String[len*(len+1)/2]; 9 | 10 | //This loop maintains the starting character 11 | for(int i = 0; i < len; i++) { 12 | //This loop adds the next character every iteration for the subset to form and add it to the array 13 | for(int j = i; j < len; j++) { 14 | arr[temp] = str.substring(i, j+1); 15 | temp++; 16 | } 17 | } 18 | 19 | //This loop prints all the subsets formed from the string. 20 | System.out.println("All subsets for given string are: "); 21 | for(int i = 0; i < arr.length; i++) { 22 | System.out.println(arr[i]); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /str7.java: -------------------------------------------------------------------------------- 1 | public class str7 { 2 | public static String lcp(String s, String t){ 3 | int n = Math.min(s.length(),t.length()); 4 | for(int i = 0; i < n; i++){ 5 | if(s.charAt(i) != t.charAt(i)){ 6 | return s.substring(0,i); 7 | } 8 | } 9 | return s.substring(0,n); 10 | } 11 | 12 | public static void main(String[] args) { 13 | String str = "acbdfghybdf"; 14 | String lrs=""; 15 | int n = str.length(); 16 | for(int i = 0; i < n; i++){ 17 | for(int j = i+1; j < n; j++){ 18 | //Checks for the largest common factors in every substring 19 | String x = lcp(str.substring(i,n),str.substring(j,n)); 20 | //If the current prefix is greater than previous one 21 | //then it takes the current one as longest repeating sequence 22 | if(x.length() > lrs.length()) lrs=x; 23 | } 24 | } 25 | System.out.println("Longest repeating sequence: "+lrs); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /str8.java: -------------------------------------------------------------------------------- 1 | public class str8 { 2 | public static String swapString(String a, int i, int j) { 3 | char[] b =a.toCharArray(); 4 | char ch; 5 | ch = b[i]; 6 | b[i] = b[j]; 7 | b[j] = ch; 8 | return String.valueOf(b); 9 | } 10 | 11 | public static void main(String[] args) 12 | { 13 | String str = "ABC"; 14 | int len = str.length(); 15 | System.out.println("All the permutations of the string are: "); 16 | generatePermutation(str, 0, len); 17 | } 18 | 19 | //Function for generating different permutations of the string 20 | public static void generatePermutation(String str, int start, int end) 21 | { 22 | //Prints the permutations 23 | if (start == end-1) 24 | System.out.println(str); 25 | else 26 | { 27 | for (int i = start; i < end; i++) 28 | { 29 | //Swapping the string by fixing a character 30 | str = swapString(str,start,i); 31 | //Recursively calling function generatePermutation() for rest of the characters 32 | generatePermutation(str,start+1,end); 33 | //Backtracking and swapping the characters again. 34 | str = swapString(str,start,i); 35 | } 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /str9.java: -------------------------------------------------------------------------------- 1 | public class str9 { 2 | public static void main(String[] args) { 3 | 4 | String str1="Remove white spaces"; 5 | 6 | //Removes the white spaces using regex 7 | str1 = str1.replaceAll("\\s+", ""); 8 | 9 | System.out.println("String after removing all the white spaces : " + str1); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /strontio_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class StrontioNumberExample1 3 | { 4 | public static void main(String args[]) 5 | { 6 | Scanner sc=new Scanner(System.in); 7 | System.out.print("Enter the number: "); 8 | //reading an integer from the user 9 | int num=sc.nextInt(); 10 | int n=num; 11 | //first, we have multiplied a number by 2 12 | //the resultant is divided by 1000 that gives the remainder and removes the first digit 13 | //at last, the resultant is divided by 10 that removes the last digit 14 | //therefore, we get a two-digit number that are mean digits of the given number 15 | num=(num*2%1000)/10; 16 | //divide the two-digit number (that we get from the above) by 10 and find the remainder 17 | //compares the remainder and quotient 18 | if(num%10==num/10) 19 | //if equal, prints strontio number 20 | System.out.println(n+ " is a strontio number."); 21 | else 22 | //prints if not a strontio number 23 | System.out.println(n+ " is not a strontio number."); 24 | } 25 | } -------------------------------------------------------------------------------- /sunny_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class SunnyNumberExample1 3 | { 4 | //driver code 5 | public static void main(String args[]) 6 | { 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter a number to check: "); 9 | //reading a number from the user 10 | int N=sc.nextInt(); 11 | //calling user-defined function 12 | isSunnyNumber(N); 13 | } 14 | //function checks whether the given is a perfect square or not 15 | static boolean findPerfectSquare(double num) 16 | { 17 | //finds the square root of the ggiven number 18 | double square_root = Math.sqrt(num); 19 | //if square root is an integer 20 | return((square_root - Math.floor(square_root)) == 0); 21 | } 22 | //function that checks whether the given number is Sunny or not 23 | static void isSunnyNumber(int N) 24 | { 25 | //checks N+1 is perfect square or not 26 | if (findPerfectSquare(N + 1)) 27 | { 28 | System.out.println("The given number is a sunny number."); 29 | } 30 | //executes if N+1 is not a perfect square 31 | else 32 | { 33 | System.out.println("The given number is not a sunny number."); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /tech_num.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class TechNumberExample2 3 | { 4 | public static void main(String args[]) 5 | { 6 | int n, num, firstHalf, secondHalf, digits = 0, squareOfSum = 0; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter a number to check: "); 9 | //reads an integer from the user 10 | n = sc.nextInt(); 11 | //assign the value of n into num 12 | num = n; 13 | //the loop executes until the condition returns false 14 | while (num > 0) 15 | { 16 | //incerements the variable digits by 1 17 | digits++; 18 | //removes the last digit of the given number 19 | num = num / 10; 20 | } 21 | //check if the given number has an even number of digits or not 22 | if (digits % 2 == 0) 23 | { 24 | num = n; 25 | //determines the first half of the given number 26 | firstHalf = num % (int) Math.pow(10, digits / 2); 27 | //determines the second half of the given number 28 | secondHalf = num / (int) Math.pow(10, digits / 2); 29 | //calculate the square of both the numbers 30 | squareOfSum = (firstHalf + secondHalf) * (firstHalf + secondHalf); 31 | //compares the square of the sum with the given number 32 | if (n == squareOfSum) 33 | { 34 | System.out.println(n+" is a tech number."); 35 | } 36 | else 37 | { 38 | System.out.println(n+" is not a tech number."); 39 | } 40 | } 41 | else 42 | { 43 | System.out.println(n+ " is not a tech number."); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /timestp2date.java: -------------------------------------------------------------------------------- 1 | import java.sql.Timestamp; 2 | import java.util.Date; 3 | class TimestampToDateExample1 { 4 | public static void main(String args[]){ 5 | Timestamp ts=new Timestamp(System.currentTimeMillis()); 6 | Date date=new Date(ts.getTime()); 7 | System.out.println(date); 8 | } 9 | } -------------------------------------------------------------------------------- /tri_numpat_1.java: -------------------------------------------------------------------------------- 1 | class Pattern1 2 | { 3 | public static void main(String args[]) 4 | { 5 | int i, j,number, n=7; 6 | //loop for rows 7 | for(i=0; i= 1; j--) 15 | { 16 | System.out.print(j+" "); 17 | } 18 | System.out.println(); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /tri_numpat_11.java: -------------------------------------------------------------------------------- 1 | class Pattern19 2 | { 3 | public static void main(String args[]) 4 | { 5 | int rows=9; 6 | for (int i = 1; i <= rows; i++) 7 | { 8 | int num = i; 9 | for (int j = 1; j <= i; j++) 10 | { 11 | System.out.print(num+" "); 12 | num = num+rows-j; 13 | } 14 | System.out.println(); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /tri_numpat_2.java: -------------------------------------------------------------------------------- 1 | class Pattern2 2 | { 3 | public static void main(String[] args) 4 | { 5 | int i, j, k = 1; 6 | //inner loop 7 | for (i = 1; i <= 7; i++) 8 | { 9 | //outer loop 10 | for (j = 1; j< i + 1; j++) 11 | { 12 | //prints the value of k 13 | System.out.print(k++ + " "); 14 | } 15 | //throws the cursor at the next line 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /tri_numpat_3.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Pattern5 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, rows; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter the number of rows you want to print: "); 9 | rows = sc.nextInt(); 10 | for (i = 1; i <= rows; i++) 11 | { 12 | for (j = 1; j <= i; j++) 13 | { 14 | System.out.print(i+" "); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /tri_numpat_4.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | class Pattern6 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, rows; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter the number of rows youy want to print: "); 9 | rows = sc.nextInt(); 10 | for (i = rows; i >= 1; i--) 11 | { 12 | for (j = rows; j >= i; j--) 13 | { 14 | System.out.print(j+" "); 15 | } 16 | 17 | System.out.println(); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /tri_numpat_5.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | class Pattern7 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, n; 7 | Scanner sc = new Scanner(System.in); 8 | System.out.print("Enter the number of rows you want to print: "); 9 | n = sc.nextInt(); 10 | for (i = 1; i <= n; i++) 11 | { 12 | for (j = i; j >= 1; j--) 13 | { 14 | System.out.print(j+" "); 15 | } 16 | System.out.println(); 17 | } 18 | } 19 | } -------------------------------------------------------------------------------- /tri_numpat_6.java: -------------------------------------------------------------------------------- 1 | 2 | class Pattern9 3 | { 4 | public static void main(String[] args) 5 | { 6 | int i, j, rows=9; 7 | for (i = 1; i <= rows; i++) 8 | { 9 | for (j = 1; j <= i; j++) 10 | { 11 | if(j%2 == 0) 12 | { 13 | System.out.print(0); 14 | } 15 | else 16 | { 17 | System.out.print(1); 18 | } 19 | } 20 | System.out.println(); 21 | } 22 | } 23 | } -------------------------------------------------------------------------------- /tri_numpat_7.java: -------------------------------------------------------------------------------- 1 | class Pattern11 2 | { 3 | public static void main(String[] args) 4 | { 5 | int rows=8; 6 | //Prints upper half pattern 7 | for (int i = 1; i <= rows; i++) 8 | { 9 | for (int j = 1; j <= i; j++) 10 | { 11 | System.out.print(j+" "); 12 | } 13 | System.out.println(); 14 | } 15 | //prints lower half pattern 16 | for (int i = rows-1; i >= 1; i--) 17 | { 18 | for (int j = 1; j <= i; j++) 19 | { 20 | System.out.print(j+" "); 21 | } 22 | System.out.println(); 23 | } 24 | } 25 | } -------------------------------------------------------------------------------- /tri_numpat_8.java: -------------------------------------------------------------------------------- 1 | class Pattern12 2 | { 3 | public static void main(String[] args) 4 | { 5 | int rows=9; 6 | for (int i = 1; i <= rows; i++) 7 | { 8 | for (int j = rows; j >= i; j--) 9 | { 10 | System.out.print(j+" "); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /tri_numpat_9.java: -------------------------------------------------------------------------------- 1 | class Pattern14 2 | { 3 | public static void main(String[] args) 4 | { 5 | int i, j, rows=9; 6 | for (i = rows; i >= 1; i--) 7 | { 8 | for (j = 1; j <= i; j++) 9 | { 10 | System.out.print(j+" "); 11 | } 12 | System.out.println(); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /xylem_phloem_num.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | class XylemPhloemExample 4 | { 5 | public static void main(String args[]) 6 | { 7 | //the variable extreme_sum stores the sum of extreme digits 8 | //the variable mean_sum stores the sum of mean digits 9 | int num, extreme_sum = 0, mean_sum = 0, n; 10 | Scanner sc= new Scanner (System.in); 11 | System.out.print("Enter a number: "); 12 | //reading an integer from the user 13 | num = sc.nextInt(); 14 | //finds the absolute value of the given number 15 | num = Math.abs(num); 16 | //copying the given number into n 17 | n = num; 18 | //the while loop executes until the specified condition becomes false 19 | while(n != 0) 20 | { 21 | //returns true if one of the conditions is true 22 | if(n == num || n < 10) 23 | //finds the last digit and add it to the variable extreme_sum 24 | extreme_sum = extreme_sum + n % 10; 25 | else 26 | //finds the mean digits and add it to the variable mean_sum 27 | mean_sum = mean_sum + n % 10; 28 | //removes the last digit from the number 29 | n = n / 10; 30 | } 31 | System.out.println("The sum of extreme digits: " + extreme_sum ); 32 | System.out.println("The sum of mean digits: " + mean_sum); 33 | //comparing the sum of extreme digits and with the sum of mean digits 34 | if(extreme_sum == mean_sum) 35 | //prints if sums are equal 36 | System.out.println(num + " is a xylem number."); 37 | else 38 | //prints if sums are not equal 39 | System.out.println(num + " is a phloem number."); 40 | } 41 | } --------------------------------------------------------------------------------