From 6908f738fa5427fba532df6a246842ce6b0be2a9 Mon Sep 17 00:00:00 2001 From: Ajit Singh Rawat <47390463+AJIT-SINGH-RAWAT@users.noreply.github.com> Date: Mon, 5 Oct 2020 10:29:10 +0530 Subject: [PATCH 01/63] program to find whether given string is palindrome or not. --- palindrome.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 palindrome.java diff --git a/palindrome.java b/palindrome.java new file mode 100644 index 0000000..3f4f3e1 --- /dev/null +++ b/palindrome.java @@ -0,0 +1,18 @@ +import java.util.Scanner; +class Palindrome +{ + public static void main(String args[]) + { + String original, reverse = ""; // Objects of String class + Scanner sc = new Scanner(System.in); + System.out.println("Enter a string"); + original = sc.nextLine(); + int length = original.length(); + for ( int i = length - 1; i >= 0; i-- ) + reverse = reverse + original.charAt(i); + if (original.equals(reverse)) + System.out.println("Enter string is palindrome."); + else + System.out.println("Enter string is not a palindrome."); + } +} From faa04339c87363886f9b559939e4450b7255b839 Mon Sep 17 00:00:00 2001 From: DSubhajit-Dey <69747319+DSubhajit-Dey@users.noreply.github.com> Date: Mon, 5 Oct 2020 11:22:51 +0530 Subject: [PATCH 02/63] Prime Numbers with a Twist /*Java program to check whether a number entered by user is prime or not for only positive numbers, if the number is negative then ask the user to re-enter the number*/ --- Prime Numbers with a Twist | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Prime Numbers with a Twist diff --git a/Prime Numbers with a Twist b/Prime Numbers with a Twist new file mode 100644 index 0000000..7c7255f --- /dev/null +++ b/Prime Numbers with a Twist @@ -0,0 +1,39 @@ +//Prime number is a number which is divisible by 1 and another by itself only. + +import java.util.Scanner; +class Main +{ +public static void main(String[] args) +{ +Scanner sc = new Scanner(System.in); +//input a number from user +System.out.println("Enter the number to be checked : "); +int n = sc.nextInt(); +//create object of class CheckPrime +Main ob=new Main(); +//calling function with value n, as parameter +ob.check(n); +} +//function for checking number is positive or negative +void check(int n) +{ +if(n<0) +System.out.println("Please enter a positive integer"); +else +prime(n); +} +//function for checking number is prime or not +void prime(int n) +{ +int c=0; +for(int i=2;i=1) +System.out.println("Entered number is not a prime number"); +else +System.out.println("Entered number is a prime number"); +} +} From 7516d5e3ce6455d25a013c021a7ee7fd5a1aae60 Mon Sep 17 00:00:00 2001 From: DSubhajit-Dey <69747319+DSubhajit-Dey@users.noreply.github.com> Date: Mon, 5 Oct 2020 11:27:44 +0530 Subject: [PATCH 03/63] Checking if a given year is leap year or not /*Java program to check whether a year entered by user is a leap year or not and a leap year is a year which is completely divisible by 4,but the year should not be a century year except it is divisible by 400*/ Explanation: To check whether a year is leap or not Step 1: We first divide the year by 4. If it is not divisible by 4 then it is not a leap year. If it is divisible by 4 leaving remainder 0 Step 2: We divide the year by 100 If it is not divisible by 100 then it is a leap year. If it is divisible by 100 leaving remainder 0 Step 3: We divide the year by 400 If it is not divisible by 400 then it is a leap year. If it is divisible by 400 leaving remainder 0 Then it is a leap year --- Checking if a given year is leap year or not | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Checking if a given year is leap year or not diff --git a/Checking if a given year is leap year or not b/Checking if a given year is leap year or not new file mode 100644 index 0000000..971a59e --- /dev/null +++ b/Checking if a given year is leap year or not @@ -0,0 +1,17 @@ +import java.util.Scanner; +public class Main +{ + public static void main(String[] args) + { + //scanner class declaration + Scanner sc=new Scanner(System.in); + //input year from user + System.out.println("Enter a Year"); + int year = sc.nextInt(); + //condition for checking year entered by user is a leap year or not + if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) + System.out.println(year + " is a leap year."); + else + System.out.println(year + " is not a leap year."); + } +} From c2104ade7363a462e1ce894791b2769330bf2df5 Mon Sep 17 00:00:00 2001 From: Amit Kumar Mitra <50025230+amit14mitra@users.noreply.github.com> Date: Mon, 5 Oct 2020 11:33:57 +0530 Subject: [PATCH 04/63] Added Java Map Program --- MapDemo.java | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 MapDemo.java diff --git a/MapDemo.java b/MapDemo.java new file mode 100644 index 0000000..f17d317 --- /dev/null +++ b/MapDemo.java @@ -0,0 +1,34 @@ +/* Here is given a phone book that consists of people's names and their phone number. +After that I will take some person's name as query. +For each query I print the phone number of that person. */ + +import java.util.*; +import java.io.*; + +class Solution{ + public static void main(String []argh) + { + Scanner in = new Scanner(System.in); + int n=in.nextInt(); + in.nextLine(); + Map m=new HashMap(); + for(int i=0;i Date: Mon, 5 Oct 2020 11:56:22 +0530 Subject: [PATCH 05/63] min & max how to find extreme values ie. minimum and maximum. --- minimum&maximum.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 minimum&maximum.java diff --git a/minimum&maximum.java b/minimum&maximum.java new file mode 100644 index 0000000..8cf3e37 --- /dev/null +++ b/minimum&maximum.java @@ -0,0 +1,23 @@ +//https://m.facebook.com/story.php?story_fbid=2848722018739963&id=100008065788593 +//subscribed by Sam Parker + +import java.util.Collections; +import java.util.ArrayList; + +class Main { + public static void main(String[] args) { + // Creating an ArrayList + ArrayList numbers = new ArrayList<>(); + numbers.add(1); + numbers.add(2); + numbers.add(3); + + // Using min() + int min = Collections.min(numbers); + System.out.println("Minimum Element: " + min); + + // Using max() + int max = Collections.max(numbers); + System.out.println("Maximum Element: " + max); + } +} From 3ee4d350a854a8fa366479fa08962bc451053dfd Mon Sep 17 00:00:00 2001 From: Sailee Salunke <68729659+sailee2029@users.noreply.github.com> Date: Mon, 5 Oct 2020 11:59:23 +0530 Subject: [PATCH 06/63] Fibonacci Sequence for first n terms --- Fibonacci Sequence | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Fibonacci Sequence diff --git a/Fibonacci Sequence b/Fibonacci Sequence new file mode 100644 index 0000000..96fc777 --- /dev/null +++ b/Fibonacci Sequence @@ -0,0 +1,25 @@ +package fibonacci; + +/** + * + * @author Sailee + */ +public class Fibonacci { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + int n=10 , t1=0, t2=1; + System.out.print("First"+n+"terms:"); + for(int i=1; i<=n ; ++i) + { + System.out.println(t1); + + int sum=t1+t2; + t1=t2; + t2=sum; + } + } + +} From 62047191a8572b3ba48d6150875e5e7a15583e5d Mon Sep 17 00:00:00 2001 From: Dev_Sumit <58663629+Mathur777@users.noreply.github.com> Date: Mon, 5 Oct 2020 13:52:53 +0530 Subject: [PATCH 07/63] printnumber programm to print number 1 to 100 in java --- printnumber | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 printnumber diff --git a/printnumber b/printnumber new file mode 100644 index 0000000..9fe68b1 --- /dev/null +++ b/printnumber @@ -0,0 +1,15 @@ +public class Printnumber +{ + public static void main(String[] args) + { + //print the result + System.out.println("Output is : "); + + //loop to print 1 to 10. + for(int i = 1; i <= 10; i++) + { + System.out.println(i); + } + } +} + From 684964c6de4a6048d5a5ca095ccddb27eb348a01 Mon Sep 17 00:00:00 2001 From: BhargavReddyg <65347488+BhargavReddyg@users.noreply.github.com> Date: Mon, 5 Oct 2020 14:15:03 +0530 Subject: [PATCH 08/63] Added the Sieve of Eratosthenes Program --- Sieve of Eratosthenes | 51 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Sieve of Eratosthenes diff --git a/Sieve of Eratosthenes b/Sieve of Eratosthenes new file mode 100644 index 0000000..f2ef21e --- /dev/null +++ b/Sieve of Eratosthenes @@ -0,0 +1,51 @@ +// { Driver Code Starts +//Initial Template for Java +import java.io.*; +import java.util.*; + +class GFG +{ + public static void main(String args[])throws IOException + { + Scanner sc = new Scanner(System.in); + int t = sc.nextInt(); + while(t-- > 0) + { + int N=sc.nextInt(); + + Solution ob = new Solution(); + ArrayList primes = ob.sieveOfEratosthenes(N); + for(int prime : primes) { + System.out.print(prime+" "); + } + System.out.println(); + } + } +} +// } Driver Code Ends + + +//User function Template for Java +class Solution{ + static ArrayList sieveOfEratosthenes(int N){ + // code here + ArrayList numbers=new ArrayList(); + int i=0; + + + boolean isPrime[]=new boolean[N+1]; + Arrays.fill(isPrime,true); + + for(i=2;i*i<=N;i++){ + if(isPrime[i]){ + for(int j=2*i;j<=N;j=j+i) + isPrime[j]=false; + } + } + for(i=2;i<=N;i++){ + if(isPrime[i]) + numbers.add(i); + } + return numbers; + } +} From a08f32f6dd0295e0f2f3e123dbddcdd92b966ba1 Mon Sep 17 00:00:00 2001 From: Tharindu Rewatha <60292980+TharinduRewatha@users.noreply.github.com> Date: Mon, 5 Oct 2020 15:20:27 +0530 Subject: [PATCH 09/63] Show_PrimeNumbers.java Program to display first 100 prime numbers --- Show_PrimeNumbers.java | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 Show_PrimeNumbers.java diff --git a/Show_PrimeNumbers.java b/Show_PrimeNumbers.java new file mode 100644 index 0000000..66d3a40 --- /dev/null +++ b/Show_PrimeNumbers.java @@ -0,0 +1,42 @@ +//https://www.facebook.com/permalink.php?story_fbid=2750473708542571&id=100007399066161 +Subscribed by tharindu Rewatha + +class PrimeNumberDemo +{ + public static void main(String args[]) + { + int n; + int status = 1; + int num = 3; + //For capturing the value of n + Scanner scanner = new Scanner(System.in); + System.out.println("Enter the value of n:"); + //The entered value is stored in the var n + n = scanner.nextInt(); + if (n >= 1) + { + System.out.println("First "+n+" prime numbers are:"); + //2 is a known prime number + System.out.println(2); + } + + for ( int i = 2 ; i <=n ; ) + { + for ( int j = 2 ; j <= Math.sqrt(num) ; j++ ) + { + if ( num%j == 0 ) + { + status = 0; + break; + } + } + if ( status != 0 ) + { + System.out.println(num); + i++; + } + status = 1; + num++; + } + } +} From 3ee6004c734235943398f9f607af0b6049194867 Mon Sep 17 00:00:00 2001 From: Tharindu Rewatha <60292980+TharinduRewatha@users.noreply.github.com> Date: Mon, 5 Oct 2020 15:33:09 +0530 Subject: [PATCH 10/63] Selection_Sort.java --- Selection_Sort.java | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Selection_Sort.java diff --git a/Selection_Sort.java b/Selection_Sort.java new file mode 100644 index 0000000..141fdfc --- /dev/null +++ b/Selection_Sort.java @@ -0,0 +1,49 @@ +//https://www.facebook.com/permalink.php?story_fbid=2750473708542571&id=100007399066161 +//Subscribed by tharindu Rewatha + +class JavaExample +{ + void selectionSort(int arr[]) + { + int len = arr.length; + + for (int i = 0; i < len-1; i++) + { + // Finding the minimum element in the unsorted part of array + int min = i; + for (int j = i+1; j < len; j++) + if (arr[j] < arr[min]) + min = j; + + /* Swapping the found minimum element with the first + * element of the sorted subarray using temp variable + */ + int temp = arr[min]; + arr[min] = arr[i]; + arr[i] = temp; + } + } + + // Displays the array elements + void printArr(int arr[]) + { + for (int i=0; i Date: Mon, 5 Oct 2020 17:36:49 +0530 Subject: [PATCH 11/63] Create Java Program to calculate Compound Interest.java --- ... Program to calculate Compound Interest.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Java Program to calculate Compound Interest.java diff --git a/Java Program to calculate Compound Interest.java b/Java Program to calculate Compound Interest.java new file mode 100644 index 0000000..9467663 --- /dev/null +++ b/Java Program to calculate Compound Interest.java @@ -0,0 +1,17 @@ +#https://m.facebook.com/story.php?story_fbid=2714895672163155&id=100009282474866 +#subscribe by code house + + +public class JavaExample { + + public void calculate(int p, int t, double r, int n) { + double amount = p * Math.pow(1 + (r / n), n * t); + double cinterest = amount - p; + System.out.println("Compound Interest after " + t + " years: "+cinterest); + System.out.println("Amount after " + t + " years: "+amount); + } + public static void main(String args[]) { + JavaExample obj = new JavaExample(); + obj.calculate(2000, 5, .08, 12); + } +} From b55697ee4e6435f16e26b0c133aafd9e9580c051 Mon Sep 17 00:00:00 2001 From: kk3600 <67818713+kk3600@users.noreply.github.com> Date: Mon, 5 Oct 2020 19:46:10 +0530 Subject: [PATCH 12/63] added addtion of binary number in java --- add two binary numbers.java | 43 +++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 add two binary numbers.java diff --git a/add two binary numbers.java b/add two binary numbers.java new file mode 100644 index 0000000..b50a7db --- /dev/null +++ b/add two binary numbers.java @@ -0,0 +1,43 @@ +//https://www.facebook.com/swati.mallik.180/posts/158104889312424 +//subscribed by swati +import java.util.Scanner; +public class JavaExample { + public static void main(String[] args) + { + //Two variables to hold two input binary numbers + long b1, b2; + int i = 0, carry = 0; + + //This is to hold the output binary number + int[] sum = new int[10]; + + //To read the input binary numbers entered by user + Scanner scanner = new Scanner(System.in); + + //getting first binary number from user + System.out.print("Enter first binary number: "); + b1 = scanner.nextLong(); + //getting second binary number from user + System.out.print("Enter second binary number: "); + b2 = scanner.nextLong(); + + //closing scanner after use to avoid memory leak + scanner.close(); + while (b1 != 0 || b2 != 0) + { + sum[i++] = (int)((b1 % 10 + b2 % 10 + carry) % 2); + carry = (int)((b1 % 10 + b2 % 10 + carry) / 2); + b1 = b1 / 10; + b2 = b2 / 10; + } + if (carry != 0) { + sum[i++] = carry; + } + --i; + System.out.print("Output: "); + while (i >= 0) { + System.out.print(sum[i--]); + } + System.out.print("\n"); + } +} From 1e1ee72b82167ab853c65e41b93265ddbb49ec8e Mon Sep 17 00:00:00 2001 From: mritunjay7065 <57717552+mritunjay7065@users.noreply.github.com> Date: Mon, 5 Oct 2020 19:47:19 +0530 Subject: [PATCH 13/63] I have written a code to show the implementation of abstract class in java --- Abstract_Class_Example.java | 137 ++++++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 Abstract_Class_Example.java diff --git a/Abstract_Class_Example.java b/Abstract_Class_Example.java new file mode 100644 index 0000000..4ff5db7 --- /dev/null +++ b/Abstract_Class_Example.java @@ -0,0 +1,137 @@ +//Subscribed by Mritunjay Kumar +//https://www.facebook.com/Mritunjay70/posts/1445812775628125 +abstract class Person { + String name; + int age; + + Person() { + + } + Person(String n, int r) { + name = n; + age = r; + } + + void setData(String name1, int age1) + { + name = name1; + age = age1; + } + + void printDetails() + { + System.out.print("Name: "+name+" Age: "+age); + } + + abstract void exceptional(); + + final void isAdult() { + if (age > 18) System.out.println("\t Yes"); + } +} // class Person ends + +class Student extends Person +{ + double cpi; + + Student() { + + } + Student(String n, int r, double c) { + + super(n, r); + cpi = c; + } + + void setData(String name1, int age1, double cpi1) + { + super.setData(name1, age1); + cpi = cpi1; + } + + void printDetails() + { + System.out.println(); + super.printDetails(); + System.out.print(" CPI: "+cpi); + } + + void f() { + + } + + void exceptional() + { + if (cpi > 9.5) System.out.print(" Exceptional "); + } + + void isAdult(Person p) { + + } +} // class Student ends here + +abstract class test extends Student { + abstract void f(); + void f2(){ + + } +} + +class Faculty extends Person +{ + float noOfPub; + + Faculty() { + + } + Faculty(String n, int r, float nop) { + super(n, r); + noOfPub = nop; + } + + void setData(String name1, int age1, int nop) + { + super.setData(name1, age1); + noOfPub = nop; + } + + void printDetails() + { + System.out.println(); + super.printDetails(); + System.out.print(" No of Pub: "+noOfPub); + } + + void exceptional() + { + if (noOfPub > 100) System.out.print(" Exceptional "); + } +} + +public class AbstractDemo { + + public static void main(String[] args) + { + Student s1 = new Student(), s2 = new Student(); + Faculty f1 = new Faculty(), f2 = new Faculty(); + + s1.setData("A", 10, 9.7); s2.setData("B", 10, 7.0); + f1.setData("C", 70, 300); f2.setData("D", 75, 50); + + Person [] p= new Person[4]; + + p[0] = s1; + p[1] = s2; + p[2] = f1; + p[3] = f2; + + for (int index=0; index Date: Mon, 5 Oct 2020 19:48:16 +0530 Subject: [PATCH 14/63] Create check power on a number.java --- check power on a number.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 check power on a number.java diff --git a/check power on a number.java b/check power on a number.java new file mode 100644 index 0000000..a9db6ce --- /dev/null +++ b/check power on a number.java @@ -0,0 +1,20 @@ + +//https://www.facebook.com/swati.mallik.180/posts/158104889312424 +//subscribed by swati +public class JavaExample { + public static void main(String[] args) { + //Here number is the base and p is the exponent + int number = 2, p = 5; + long result = 1; + + //Copying the exponent value to the loop counter + int i = p; + for (;i != 0; --i) + { + result *= number; + } + + //Displaying the output + System.out.println(number+"^"+p+" = "+result); + } +} From 68ffd4a807b3f5e0a2aa16bacefd8abba5cdbcdb Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 5 Oct 2020 13:02:17 -0300 Subject: [PATCH 15/63] add data --- Data.java | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ testeData.java | 14 ++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Data.java create mode 100644 testeData.java diff --git a/Data.java b/Data.java new file mode 100644 index 0000000..8d79433 --- /dev/null +++ b/Data.java @@ -0,0 +1,48 @@ +package data; + +import javax.swing.*; +import java.awt.*; +import java.awt.event.*; +import java.util.*; + +public class Data extends JFrame{ + JLabel rotulo,rotulo2; + int ds,dia,mes,ano; + Calendar data; + String diasemana[]={"Domingo","Segunda - Feira","Terça - Feira","Quarta - Feira", + "Quinta - Feira","Sexta - Feira","Sabado"}; + String meses[]={"Janeiro","Fevereiro","Março","Abril","Maio","Junho", + "Julho","Agosto","Setembro","Outubro","Novembro","Dezembro"}; + + public Data(){ + super("Exemplo da Data"); + Container tela = getContentPane(); + tela.setLayout(null); + rotulo = new JLabel(""); + rotulo2 = new JLabel(""); + + rotulo.setBounds(20,30,280,20); + rotulo2.setBounds(20,60,280,20); + + data = Calendar.getInstance(); + + ds = data.get(Calendar.DAY_OF_WEEK); + dia = data.get(Calendar.DAY_OF_MONTH); + mes = data.get(Calendar.MONTH); + ano = data.get(Calendar.YEAR); + + //Rotulo.setText("Data: "+dia+"/"+(mes+1)+"/"+ano); + + rotulo.setText("Data: "+ds+" "+dia+"/"+(mes+1)+"/"+ano); + rotulo2.setText("Data: "+diasemana[ds-1]+", "+dia+" de "+meses[mes]+" de "+ano); + + //Rotulo.setText("Data: "+dia+" de "+meses[mes]+" de "+ano); + + tela.add(rotulo); + tela.add(rotulo2); + setSize(300, 200); + setVisible(true); + setLocationRelativeTo(null); } + + +} \ No newline at end of file diff --git a/testeData.java b/testeData.java new file mode 100644 index 0000000..f76af7b --- /dev/null +++ b/testeData.java @@ -0,0 +1,14 @@ +package data; + +import javax.swing.JFrame; + +/** + * + * @author Aluno + */ +public class TesteData { + public static void main(String[] args) { + Data app = new Data(); + app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + } +} \ No newline at end of file From 5d09be2ba0c710847e42fbbf79285df742030551 Mon Sep 17 00:00:00 2001 From: Abhigyan-22 <56131296+Abhigyan-22@users.noreply.github.com> Date: Mon, 5 Oct 2020 23:04:55 +0530 Subject: [PATCH 16/63] Create Fibonacci series Fibonacci series --- Fibonacci series | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Fibonacci series diff --git a/Fibonacci series b/Fibonacci series new file mode 100644 index 0000000..7326348 --- /dev/null +++ b/Fibonacci series @@ -0,0 +1,15 @@ +class FibonacciExample1{ +public static void main(String args[]) +{ + int n1=0,n2=1,n3,i,count=10; + System.out.print(n1+" "+n2);//printing 0 and 1 + + for(i=2;i Date: Mon, 5 Oct 2020 23:16:20 +0530 Subject: [PATCH 17/63] Update Fibonacci series --- Fibonacci series | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Fibonacci series b/Fibonacci series index 7326348..5f05f20 100644 --- a/Fibonacci series +++ b/Fibonacci series @@ -1,3 +1,5 @@ +//https://www.facebook.com/swati.mallik.180/posts/158104889312424 +//subscribed by swati class FibonacciExample1{ public static void main(String args[]) { From 5a032c27560afcceaa57ae56e92fbe767cad720a Mon Sep 17 00:00:00 2001 From: anjaan <72304630+Code-hunter-star@users.noreply.github.com> Date: Tue, 6 Oct 2020 00:12:29 +0530 Subject: [PATCH 18/63] Create add two date creating program to add two date --- add two date | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 add two date diff --git a/add two date b/add two date new file mode 100644 index 0000000..5ccc406 --- /dev/null +++ b/add two date @@ -0,0 +1,22 @@ +import java.util.Calendar; + +public class AddDates { + + public static void main(String[] args) { + + Calendar c1 = Calendar.getInstance(); + Calendar c2 = Calendar.getInstance(); + Calendar cTotal = (Calendar) c1.clone(); + + cTotal.add(Calendar.YEAR, c2.get(Calendar.YEAR)); + cTotal.add(Calendar.MONTH, c2.get(Calendar.MONTH) + 1); // Zero-based months + cTotal.add(Calendar.DATE, c2.get(Calendar.DATE)); + cTotal.add(Calendar.HOUR_OF_DAY, c2.get(Calendar.HOUR_OF_DAY)); + cTotal.add(Calendar.MINUTE, c2.get(Calendar.MINUTE)); + cTotal.add(Calendar.SECOND, c2.get(Calendar.SECOND)); + cTotal.add(Calendar.MILLISECOND, c2.get(Calendar.MILLISECOND)); + + System.out.format("%s + %s = %s", c1.getTime(), c2.getTime(), cTotal.getTime()); + + } +} From d252e1662d9ab9b03ebf937a585ad7b22c14d6ad Mon Sep 17 00:00:00 2001 From: mofolactic <69076841+mofolactic@users.noreply.github.com> Date: Tue, 6 Oct 2020 01:18:29 +0530 Subject: [PATCH 19/63] Create ReverseString.java --- ReverseString.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 ReverseString.java diff --git a/ReverseString.java b/ReverseString.java new file mode 100644 index 0000000..5b0a7cd --- /dev/null +++ b/ReverseString.java @@ -0,0 +1,24 @@ +public class Main +{ + public static void main(String[] args) { + String s="I love my India"; + int i=s.length()-1; + String ans=""; + while(i>=0) + { + if(i<0) + break; + while(i>=0 && s.charAt(i)==' ') + i--; + int j=i; + while(i>=0 && s.charAt(i)!=' ') + i--; + if(ans.isEmpty()){ + ans=ans.concat(s.substring(i+1,j+1)); + }else{ + ans=ans.concat(" "+s.substring(i+1,j+1)); + } + } + System.out.println(ans); + } +} From 835099ca1da9ac17ff321e74f92ac0f77764cca4 Mon Sep 17 00:00:00 2001 From: Meghraj2520 <46535983+Meghraj2520@users.noreply.github.com> Date: Tue, 6 Oct 2020 05:53:30 +0530 Subject: [PATCH 20/63] linear search in java . linear search in java . --- linear search in java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 linear search in java diff --git a/linear search in java b/linear search in java new file mode 100644 index 0000000..cc0b6e6 --- /dev/null +++ b/linear search in java @@ -0,0 +1,15 @@ +public class LinearSearchExample{ +public static int linearSearch(int[] arr, int key){ + for(int i=0;i Date: Tue, 6 Oct 2020 10:33:56 +0530 Subject: [PATCH 21/63] Create Reversed Pyramid Star Pattern --- Reversed Pyramid Star Pattern | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Reversed Pyramid Star Pattern diff --git a/Reversed Pyramid Star Pattern b/Reversed Pyramid Star Pattern new file mode 100644 index 0000000..6e68dd4 --- /dev/null +++ b/Reversed Pyramid Star Pattern @@ -0,0 +1,25 @@ +import java.util.Scanner; +public class Pattern +{ + public static void main(String[] args) +{ + Scanner sc = new Scanner(System.in); + System.out.println("Enter the number of rows: "); + + int rows = sc.nextInt(); + for (int i= 0; i<= rows-1 ; i++) + { + for (int j=0; j<=i; j++) + { + System.out.print(" "); + } + for (int k=0; k<=rows-1-i; k++) + { + System.out.print("*" + " "); + } + System.out.println(); + } + sc.close(); + +} +} From b0b36d610f633e76cea04d3e1524650d60928da1 Mon Sep 17 00:00:00 2001 From: rohitcode236 <56353684+rohitcode236@users.noreply.github.com> Date: Tue, 6 Oct 2020 11:47:51 +0530 Subject: [PATCH 22/63] Create subsets_array.java --- subsets_array.java | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 subsets_array.java diff --git a/subsets_array.java b/subsets_array.java new file mode 100644 index 0000000..fccae75 --- /dev/null +++ b/subsets_array.java @@ -0,0 +1,45 @@ +import java.io.*; +import java.util.*; + +public class Main{ + +public static void main(String[] args) throws Exception { + // write your code here + Scanner scn = new Scanner(System.in); + int n = scn.nextInt(); + int[] arr = new int[n]; + for(int i= 0;i< n;i++){ + arr[i] = scn.nextInt(); + } + int count = (int)Math.pow(2,n); + for(int i=0;i Date: Tue, 6 Oct 2020 13:47:11 +0530 Subject: [PATCH 23/63] Added complex.java --- complex.java | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 complex.java diff --git a/complex.java b/complex.java new file mode 100644 index 0000000..7047a0b --- /dev/null +++ b/complex.java @@ -0,0 +1,73 @@ +#https://www.facebook.com/harsha.gupta.1610/posts/2375944699218906 +#subscribed by Harsha Kumari + +import java.util.*; +class Complex { + + int real, imag; + Complex() + { + } + Complex(int tempReal, int tempImag) + { + real = tempReal; + imag = tempImag; + } + Complex(Complex com) + { + real=com.real; + imag=com.imag; + } + void showComplex() + { + System.out.println("Complex number: "+ real + " + "+ imag + "i"); + } + Complex addComplex(Complex C) + { + Complex temp = new Complex(); + temp.real = real + C.real; + temp.imag = imag + C.imag; + return temp; + } + Complex subtractComplex(Complex C) + { + Complex temp = new Complex(); + temp.real = real - C.real; + temp.imag = imag - C.imag; + return temp; + } + Complex multiplyComplex(Complex C) + { + Complex temp = new Complex(); + temp.real = real * C.real; + temp.imag = imag * C.imag; + return temp; + } + +} + + +public class Complexdemo { + public static void main(String[] args) + { + Complex C1 = new Complex(3, 2); + C1.showComplex(); + Complex C2 = new Complex(9, 5); + C2.showComplex(); + Complex C3 = new Complex(); + + System.out.println("Copy constructor called"); + Complex C4 = new Complex(C1); + C4.showComplex(); + + C3 = C1.addComplex(C2); + System.out.print("Sum of "); + C3.showComplex(); + C3 = C1.subtractComplex(C2); + System.out.print("Difference of "); + C3.showComplex(); + C3 = C1.multiplyComplex(C2); + System.out.print("Multiply of "); + C3.showComplex(); + } +} From 5e134b452ef12d1fde51e79ff87c68de9be79c9d Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Tue, 6 Oct 2020 17:45:26 +0530 Subject: [PATCH 24/63] Added Check Algorithm Program --- Check Algorithm.java | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Check Algorithm.java diff --git a/Check Algorithm.java b/Check Algorithm.java new file mode 100644 index 0000000..07448d2 --- /dev/null +++ b/Check Algorithm.java @@ -0,0 +1,43 @@ +// https://www.facebook.com/abhi.sensharma/posts/1241788872853419 +// Subscribed by Abhishek Sharma + +// Here I have added the Check Algorithm program using Java + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int T = sc.nextInt(); + for (int tc = 0; tc < T; ++tc) { + String S = sc.next(); + + System.out.println(solve(S) ? "YES" : "NO"); + } + + sc.close(); + } + + static boolean solve(String S) { + StringBuilder compressed = new StringBuilder(); + char current = 0; + int count = -1; + for (int i = 0; i <= S.length(); ++i) { + if (i != S.length() && S.charAt(i) == current) { + count++; + } else { + if (count > 0) { + compressed.append(current).append(count); + } + + if (i != S.length()) { + current = S.charAt(i); + count = 1; + } + } + } + + return compressed.length() < S.length(); + } +} From e4b08c5636fba28ba22faff71f6af14b52ebcb62 Mon Sep 17 00:00:00 2001 From: Abhishek Sharma Date: Tue, 6 Oct 2020 18:04:24 +0530 Subject: [PATCH 25/63] Added Daily Train problem solution --- Daily Train.java | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 Daily Train.java diff --git a/Daily Train.java b/Daily Train.java new file mode 100644 index 0000000..1a950df --- /dev/null +++ b/Daily Train.java @@ -0,0 +1,47 @@ +// https://www.facebook.com/abhi.sensharma/posts/1241788872853419 + +// subcribed by Abhishek Sharma. + +// Daily Train problem solution using Java + +import java.util.Scanner; +import java.util.stream.IntStream; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + + int X = sc.nextInt(); + int N = sc.nextInt(); + String[] cars = new String[N]; + for (int i = 0; i < cars.length; i++) { + cars[i] = sc.next(); + } + System.out.println(solve(X, cars)); + + sc.close(); + } + + static int solve(int X, String[] cars) { + int result = 0; + for (String car : cars) { + for (int i = 0; i < 9; i++) { + int freeNum = (int) IntStream.of(i * 4, i * 4 + 1, i * 4 + 2, i * 4 + 3, 53 - i * 2, 52 - i * 2) + .filter(j -> car.charAt(j) == '0').count(); + + if (freeNum >= X) { + result += C(freeNum, X); + } + } + } + return result; + } + + static int C(int n, int m) { + int result = 1; + for (int i = 0; i < m; i++) { + result = result * (n - i) / (i + 1); + } + return result; + } +} From 53fed0821efc100f9015db93abe34f83b00fc9af Mon Sep 17 00:00:00 2001 From: KshitizKhandal <56514304+KshitizKhandal@users.noreply.github.com> Date: Tue, 6 Oct 2020 18:28:25 +0530 Subject: [PATCH 26/63] Sum of Array To sum up all the elements of an array --- Sum of array | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Sum of array diff --git a/Sum of array b/Sum of array new file mode 100644 index 0000000..c757c4a --- /dev/null +++ b/Sum of array @@ -0,0 +1,11 @@ +class SumOfArray{ + public static void main(String args[]){ + int[] array = {10, 20, 30, 40, 50, 10}; + int sum = 0; + //Advanced for loop + for( int num : array) { + sum = sum+num; + } + System.out.println("Sum of array elements is:"+sum); + } +} From 4fca5e5c8f9d59972a437afff3f573c5461b6e72 Mon Sep 17 00:00:00 2001 From: nishitv2898 <36642477+nishitv2898@users.noreply.github.com> Date: Tue, 6 Oct 2020 19:29:51 +0530 Subject: [PATCH 27/63] Added Unique Number program --- unique_number.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 unique_number.java diff --git a/unique_number.java b/unique_number.java new file mode 100644 index 0000000..2a3391b --- /dev/null +++ b/unique_number.java @@ -0,0 +1,40 @@ +// https://www.facebook.com/nishit.vibhandik/posts/1414548848736181 +// Subscribed by Nishit Vibhandik + +import java.util.Scanner; + +public class UniqueNumber +{ + public static void main(String[] args) + { + // TODO code application logic here + int r1, r2, n, num1, num2, c = 0; + Scanner sc = new Scanner(System.in); + System.out.print("Enter number="); + n = sc.nextInt(); + num1 = n; + num2 = n; + while (num1 > 0) + { + r1 = num1 % 10; + while (num2 > 0) + { + r2 = num2 % 10; + if (r1 == r2) + { + c++; + } + num2 = num2 / 10; + } + num1 = num1 / 10; + } + if (c == 1) + { + System.out.println("Unique Number"); + } + else + { + System.out.println("Not Unique Number"); + } + } +} From e1751bbe6c18c68ca8e400a64a0c8750177763c0 Mon Sep 17 00:00:00 2001 From: 3aryansingh <43572087+3aryansingh@users.noreply.github.com> Date: Tue, 6 Oct 2020 23:53:32 +0530 Subject: [PATCH 28/63] Create fibonaccinum.java --- fibonaccinum.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 fibonaccinum.java diff --git a/fibonaccinum.java b/fibonaccinum.java new file mode 100644 index 0000000..7326348 --- /dev/null +++ b/fibonaccinum.java @@ -0,0 +1,15 @@ +class FibonacciExample1{ +public static void main(String args[]) +{ + int n1=0,n2=1,n3,i,count=10; + System.out.print(n1+" "+n2);//printing 0 and 1 + + for(i=2;i Date: Wed, 7 Oct 2020 01:33:39 +0530 Subject: [PATCH 29/63] unique no in java A number is said to be unique , if the digits in it are not repeated. for example, 12345 is a unique number. 123445 is not a unique number. --- unique no.java | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 unique no.java diff --git a/unique no.java b/unique no.java new file mode 100644 index 0000000..6d03609 --- /dev/null +++ b/unique no.java @@ -0,0 +1,40 @@ +# https://www.facebook.com/hitesh.parashar.908/posts/109880424218712 +# submitted by hitesh parashar + +import java.util.Scanner; + +public class UniqueNumber +{ + public static void main(String[] args) + { + // TODO code application logic here + int r1, r2, n, num1, num2, c = 0; + Scanner sc = new Scanner(System.in); + System.out.print("Enter number="); + n = sc.nextInt(); + num1 = n; + num2 = n; + while (num1 > 0) + { + r1 = num1 % 10; + while (num2 > 0) + { + r2 = num2 % 10; + if (r1 == r2) + { + c++; + } + num2 = num2 / 10; + } + num1 = num1 / 10; + } + if (c == 1) + { + System.out.println("Unique Number"); + } + else + { + System.out.println("Not Unique Number"); + } + } +} From 6108d7fe38cf7f690be307c8003cad5ad34ac2b4 Mon Sep 17 00:00:00 2001 From: kuldeepzack <71119191+kuldeepzack@users.noreply.github.com> Date: Wed, 7 Oct 2020 10:28:25 +0530 Subject: [PATCH 30/63] Create Find LCA in Binary Tree using RMQ --- Find LCA in Binary Tree using RMQ | 456 ++++++++++++++++++++++++++++++ 1 file changed, 456 insertions(+) create mode 100644 Find LCA in Binary Tree using RMQ diff --git a/Find LCA in Binary Tree using RMQ b/Find LCA in Binary Tree using RMQ new file mode 100644 index 0000000..1e2df64 --- /dev/null +++ b/Find LCA in Binary Tree using RMQ @@ -0,0 +1,456 @@ +// Java program to find LCA of u and v by reducing problem to RMQ + + + +import java.util.*; + + +// A binary tree node + +class Node +{ + + Node left, right; + + int data; + + + + Node(int item) + + { + + data = item; + + left = right = null; + + } +} + + + +class St_class +{ + + int st; + + int stt[] = new int[10000]; +} + + + +class BinaryTree +{ + + Node root; + + int v = 9; // v is the highest value of node in our tree + + int euler[] = new int[2 * v - 1]; // for euler tour sequence + + int level[] = new int[2 * v - 1]; // level of nodes in tour sequence + + int f_occur[] = new int[2 * v - 1]; // to store 1st occurrence of nodes + + int fill; // variable to fill euler and level arrays + + St_class sc = new St_class(); + + + + // log base 2 of x + + int Log2(int x) + + { + + int ans = 0; + + int y = x >>= 1; + + while (y-- != 0) + + ans++; + + return ans; + + } + + + + int swap(int a, int b) + + { + + return a; + + } + + + + /* A recursive function to get the minimum value in a given range + + of array indexes. The following are parameters for this function. + + + + st --> Pointer to segment tree + + index --> Index of current node in the segment tree. Initially + + 0 is passed as root is always at index 0 + + ss & se --> Starting and ending indexes of the segment represented + + by current node, i.e., st[index] + + qs & qe --> Starting and ending indexes of query range */ + + int RMQUtil(int index, int ss, int se, int qs, int qe, St_class st) + + { + + // If segment of this node is a part of given range, then return + + // the min of the segment + + if (qs <= ss && qe >= se) + + return st.stt[index]; + + + + // If segment of this node is outside the given range + + else if (se < qs || ss > qe) + + return -1; + + + + // If a part of this segment overlaps with the given range + + int mid = (ss + se) / 2; + + + + int q1 = RMQUtil(2 * index + 1, ss, mid, qs, qe, st); + + int q2 = RMQUtil(2 * index + 2, mid + 1, se, qs, qe, st); + + + + if (q1 == -1) + + return q2; + + else if (q2 == -1) + + return q1; + + + + return (level[q1] < level[q2]) ? q1 : q2; + + } + + + + // Return minimum of elements in range from index qs (query start) to + + // qe (query end). It mainly uses RMQUtil() + + int RMQ(St_class st, int n, int qs, int qe) + + { + + // Check for erroneous input values + + if (qs < 0 || qe > n - 1 || qs > qe) + + { + + System.out.println("Invalid input"); + + return -1; + + } + + + + return RMQUtil(0, 0, n - 1, qs, qe, st); + + } + + + + // A recursive function that constructs Segment Tree for array[ss..se]. + + // si is index of current node in segment tree st + + void constructSTUtil(int si, int ss, int se, int arr[], St_class st) + + { + + // If there is one element in array, store it in current node of + + // segment tree and return + + if (ss == se) + + st.stt[si] = ss; + + else + + { + + // If there are more than one elements, then recur for left and + + // right subtrees and store the minimum of two values in this node + + int mid = (ss + se) / 2; + + constructSTUtil(si * 2 + 1, ss, mid, arr, st); + + constructSTUtil(si * 2 + 2, mid + 1, se, arr, st); + + + + if (arr[st.stt[2 * si + 1]] < arr[st.stt[2 * si + 2]]) + + st.stt[si] = st.stt[2 * si + 1]; + + else + + st.stt[si] = st.stt[2 * si + 2]; + + } + + } + + + + /* Function to construct segment tree from given array. This function + + allocates memory for segment tree and calls constructSTUtil() to + + fill the allocated memory */ + + int constructST(int arr[], int n) + + { + + // Allocate memory for segment tree + + // Height of segment tree + + int x = Log2(n) + 1; + + + + // Maximum size of segment tree + + int max_size = 2 * (1 << x) - 1; // 2*pow(2,x) -1 + + + + sc.stt = new int[max_size]; + + + + // Fill the allocated memory st + + constructSTUtil(0, 0, n - 1, arr, sc); + + + + // Return the constructed segment tree + + return sc.st; + + } + + + + // Recursive version of the Euler tour of T + + void eulerTour(Node node, int l) + + { + + /* if the passed node exists */ + + if (node != null) + + { + + euler[fill] = node.data; // insert in euler array + + level[fill] = l; // insert l in level array + + fill++; // increment index + + + + /* if unvisited, mark first occurrence */ + + if (f_occur[node.data] == -1) + + f_occur[node.data] = fill - 1; + + + + /* tour left subtree if exists, and remark euler + + and level arrays for parent on return */ + + if (node.left != null) + + { + + eulerTour(node.left, l + 1); + + euler[fill] = node.data; + + level[fill] = l; + + fill++; + + } + + + + /* tour right subtree if exists, and remark euler + + and level arrays for parent on return */ + + if (node.right != null) + + { + + eulerTour(node.right, l + 1); + + euler[fill] = node.data; + + level[fill] = l; + + fill++; + + } + + } + + } + + + + // returns LCA of node n1 and n2 assuming they are present in tree + + int findLCA(Node node, int u, int v) + + { + + /* Mark all nodes unvisited. Note that the size of + + firstOccurrence is 1 as node values which vary from + + 1 to 9 are used as indexes */ + + Arrays.fill(f_occur, -1); + + + + /* To start filling euler and level arrays from index 0 */ + + fill = 0; + + + + /* Start Euler tour with root node on level 0 */ + + eulerTour(root, 0); + + + + /* construct segment tree on level array */ + + sc.st = constructST(level, 2 * v - 1); + + + + /* If v before u in Euler tour. For RMQ to work, first + + parameter 'u' must be smaller than second 'v' */ + + if (f_occur[u] > f_occur[v]) + + u = swap(u, u = v); + + + + // Starting and ending indexes of query range + + int qs = f_occur[u]; + + int qe = f_occur[v]; + + + + // query for index of LCA in tour + + int index = RMQ(sc, 2 * v - 1, qs, qe); + + + + /* return LCA node */ + + return euler[index]; + + + + } + + + + // Driver program to test above functions + + public static void main(String args[]) + + { + + BinaryTree tree = new BinaryTree(); + + + + // Let us create the Binary Tree as shown in the diagram. + + tree.root = new Node(1); + + tree.root.left = new Node(2); + + tree.root.right = new Node(3); + + tree.root.left.left = new Node(4); + + tree.root.left.right = new Node(5); + + tree.root.right.left = new Node(6); + + tree.root.right.right = new Node(7); + + tree.root.left.right.left = new Node(8); + + tree.root.left.right.right = new Node(9); + + + + int u = 4, v = 9; + + System.out.println("The LCA of node " + u + " and " + v + " is " + + + tree.findLCA(tree.root, u, v)); + + } + + +} From 6d1d14d2731dbb60e6096e94d70513508a7a5069 Mon Sep 17 00:00:00 2001 From: divyanshu243 <72489157+divyanshu243@users.noreply.github.com> Date: Wed, 7 Oct 2020 12:36:53 +0530 Subject: [PATCH 31/63] Create binary into decimal --- binary into decimal | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 binary into decimal diff --git a/binary into decimal b/binary into decimal new file mode 100644 index 0000000..f2f0d40 --- /dev/null +++ b/binary into decimal @@ -0,0 +1,39 @@ + +// Java program to convert +// binary to decimal + +// Function to convert +// binary to decimal +class GFG { + static int binaryToDecimal(int n) + { + int num = n; + int dec_value = 0; + + // Initializing base + // value to 1, i.e 2^0 + int base = 1; + + int temp = num; + while (temp > 0) { + int last_digit = temp % 10; + temp = temp / 10; + + dec_value += last_digit * base; + + base = base * 2; + } + + return dec_value; + } + + // Driver Code + public static void main(String[] args) + { + int num = 10101001; + System.out.println(binaryToDecimal(num)); + } +} + + + From 473ecb2b2e9f4ad0bb60d4c05e792cffd81d0071 Mon Sep 17 00:00:00 2001 From: Shantanu Roy Date: Wed, 7 Oct 2020 13:59:29 +0530 Subject: [PATCH 32/63] Added program for Bitonic_Array --- Bitonic_Array.java | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 Bitonic_Array.java diff --git a/Bitonic_Array.java b/Bitonic_Array.java new file mode 100644 index 0000000..ea6fca4 --- /dev/null +++ b/Bitonic_Array.java @@ -0,0 +1,71 @@ +/* https://www.facebook.com/shanroy1999/posts/2795036240730237 +Subscribed by : Shantanu Roy(shan.roy1999@gmail.com) */ + +import java.util.Scanner; + +class Bitonic_Array +{ + public static int isBitonic(int arr[], int N) + { + if (arr[0] > arr[1]) + return -1; + + int i, j; + for (i = 2; i < N; i++) { + if (arr[i - 1] >= arr[i]) { + break; + } + } + + if (i == N - 1) { + return 1; + } + + for (j = i + 1; j < N; j++) { + if (arr[j - 1] <= arr[j]) { + break; + } + } + + if (j != N) { + return -1; + } + + return 1; + } + + public static void main(String args[]) + { + System.out.println("Enter number of elements in array"); + Scanner sc = new Scanner(System.in); + int N = sc.nextInt(); + System.out.println("Enter elements of array"); + int[] arr = new int[N]; + for (int i = 0; i < N; i++) { + arr[i] = sc.nextInt(); + } + int ans = isBitonic(arr, N); + if (ans == -1) + System.out.println("The array is not bitonic"); + else + System.out.println("The array is bitonic"); + } +} + +/* +Input: +N = 5 +arr = {0, 1, 2, 3, 4} +Output: +The array is not bitonic +Input: +N = 5 +arr = {0, 2, 4, 3, 1} +Output: +The array is bitonic +Input: +N = 5 +arr = {4, 3, 2, 1, 0} +Output: +The array is not bitonic +*/ From c95a9ae96f2bd582e64a93c565130d17d3f4d87a Mon Sep 17 00:00:00 2001 From: garima1820 <67752635+garima1820@users.noreply.github.com> Date: Wed, 7 Oct 2020 18:26:58 +0530 Subject: [PATCH 33/63] Added a permutation program --- Permutation.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Permutation.java diff --git a/Permutation.java b/Permutation.java new file mode 100644 index 0000000..4e1a9e8 --- /dev/null +++ b/Permutation.java @@ -0,0 +1,22 @@ +/* This code will find all permutations of a given string +Recursion is used here*/ +import java.util.ArrayList; + +public class Permutation1 { +public static void main(String[] args) { + printPer("1234",""); +} + +//this method will find all permutations + public static void printPer(String ques,String ans){ + if(ques.length()==0){ + System.out.println(ans); + return; + } + for(int i=0;i Date: Wed, 7 Oct 2020 17:40:55 +0100 Subject: [PATCH 34/63] added bool --- bool.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 bool.java diff --git a/bool.java b/bool.java new file mode 100644 index 0000000..2ec8a18 --- /dev/null +++ b/bool.java @@ -0,0 +1,10 @@ +#web.facebook.com/idajo.jeff/posts/128970875635248 +#subscribed by codehouse india +public class MyClass { + public static void main(String[] args) { + boolean isJavaFun = true; + boolean isFishTasty = false; + System.out.println(isJavaFun); + System.out.println(isFishTasty); + } +} From 33302350f7f89cfa799ab4cf3bf7c5af4832e758 Mon Sep 17 00:00:00 2001 From: DSubhajit-Dey <69747319+DSubhajit-Dey@users.noreply.github.com> Date: Thu, 8 Oct 2020 18:28:33 +0530 Subject: [PATCH 35/63] CALCULATOR USING JAVA --- CALCULATOR USING JAVA | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 CALCULATOR USING JAVA diff --git a/CALCULATOR USING JAVA b/CALCULATOR USING JAVA new file mode 100644 index 0000000..c1dd98c --- /dev/null +++ b/CALCULATOR USING JAVA @@ -0,0 +1,41 @@ +package math; + +import java.util.Scanner; + +public class CalculatorDemo { + + public static void main(String[] args) { + double num1,num2; + Scanner sc=new Scanner(System.in); + System.out.println("Enter the numbers"); + num1=sc.nextDouble(); + num2=sc.nextDouble(); + System.out.println("Enter the operator (+,-,*,/)"); + char op =sc.next().charAt(0); + double o=0; + switch(op) { + case '+': + o=num1+num2; + break; + case '-': + o=num1-num2; + break; + case '*': + o=num1*num2; + break; + case '/': + o=num1/num2; + break; + default: + System.out.println("You enter wrong input"); + break; + + } + System.out.println("The final result:"); + System.out.println(); + System.out.println(num1 + " "+ op +" "+ num2 +" = "+o); + + + } + +} From 81af8e4932c3b9d15b332bf992a71bd483218ee8 Mon Sep 17 00:00:00 2001 From: ankit10126 <72246869+ankit10126@users.noreply.github.com> Date: Thu, 8 Oct 2020 21:54:59 +0530 Subject: [PATCH 36/63] Create string palindrome --- string palindrome | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 string palindrome diff --git a/string palindrome b/string palindrome new file mode 100644 index 0000000..407fe15 --- /dev/null +++ b/string palindrome @@ -0,0 +1,42 @@ +#https://www.facebook.com/permalink.php?story_fbid=2439553476338458&id=100008514873543 +# subscibe by Code House +// Java implementation of the approach +public class GFG { + + // Function that returns true if + // str is a palindrome + static boolean isPalindrome(String str) + { + + // Pointers pointing to the beginning + // and the end of the string + int i = 0, j = str.length() - 1; + + // While there are characters toc compare + while (i < j) { + + // If there is a mismatch + if (str.charAt(i) != str.charAt(j)) + return false; + + // Increment first pointer and + // decrement the other + i++; + j--; + } + + // Given string is a palindrome + return true; + } + + // Driver code + public static void main(String[] args) + { + String str = "geeks"; + + if (isPalindrome(str)) + System.out.print("Yes"); + else + System.out.print("No"); + } +} From f5fe4dbd2e4bff79a4857e7458212f478e31b8c1 Mon Sep 17 00:00:00 2001 From: Abhigyan-22 <56131296+Abhigyan-22@users.noreply.github.com> Date: Thu, 8 Oct 2020 22:05:42 +0530 Subject: [PATCH 37/63] Create Java program to find the total number of possible Binary Search Trees with N keys Java program to find the total number of possible Binary Search Trees with N keys --- ...f possible Binary Search Trees with N keys | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Java program to find the total number of possible Binary Search Trees with N keys diff --git a/Java program to find the total number of possible Binary Search Trees with N keys b/Java program to find the total number of possible Binary Search Trees with N keys new file mode 100644 index 0000000..8938a63 --- /dev/null +++ b/Java program to find the total number of possible Binary Search Trees with N keys @@ -0,0 +1,51 @@ +public class BinarySearchTree { + + //Represent the node of binary tree + public static class Node{ + int data; + Node left; + Node right; + + public Node(int data){ + //Assign data to the new node, set left and right children to null + this.data = data; + this.left = null; + this.right = null; + } + } + + //Represent the root of binary tree + public Node root; + + public BinarySearchTree(){ + root = null; + } + + //factorial() will calculate the factorial of given number + public int factorial(int num) { + int fact = 1; + if(num == 0) + return 1; + else { + while(num > 1) { + fact = fact * num; + num--; + } + return fact; + } + } + + //numOfBST() will calculate the total number of possible BST by calculating Catalan Number for given key + public int numOfBST(int key) { + int catalanNumber = factorial(2 * key)/(factorial(key + 1) * factorial(key)); + return catalanNumber; + } + + public static void main(String[] args) { + + BinarySearchTree bt = new BinarySearchTree(); + + //Display total number of possible binary search tree with key 5 + System.out.println("Total number of possible Binary Search Trees with given key: " + bt.numOfBST(5)); + } +} From 85a069778f9969f7e72bd181bd7153af32619983 Mon Sep 17 00:00:00 2001 From: Abhigyan-22 <56131296+Abhigyan-22@users.noreply.github.com> Date: Thu, 8 Oct 2020 22:20:53 +0530 Subject: [PATCH 38/63] Create Java program to create a doubly linked list of n nodes and count the number of nodes Java program to create a doubly linked list of n nodes and count the number of nodes --- ...t of n nodes and count the number of nodes | 91 +++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 Java program to create a doubly linked list of n nodes and count the number of nodes diff --git a/Java program to create a doubly linked list of n nodes and count the number of nodes b/Java program to create a doubly linked list of n nodes and count the number of nodes new file mode 100644 index 0000000..806aaff --- /dev/null +++ b/Java program to create a doubly linked list of n nodes and count the number of nodes @@ -0,0 +1,91 @@ +public class CountList { + + //Represent a node of the doubly linked list + + class Node{ + int data; + Node previous; + Node next; + + public Node(int data) { + this.data = data; + } + } + + //Represent the head and tail of the doubly linked list + Node head, tail = null; + + //addNode() will add a node to the list + public void addNode(int data) { + //Create a new node + Node newNode = new Node(data); + + //If list is empty + if(head == null) { + //Both head and tail will point to newNode + head = tail = newNode; + //head's previous will point to null + head.previous = null; + //tail's next will point to null, as it is the last node of the list + tail.next = null; + } + else { + //newNode will be added after tail such that tail's next will point to newNode + tail.next = newNode; + //newNode's previous will point to tail + newNode.previous = tail; + //newNode will become new tail + tail = newNode; + //As it is last node, tail's next will point to null + tail.next = null; + } + } + + //countNodes() will count the nodes present in the list + public int countNodes() { + int counter = 0; + //Node current will point to head + Node current = head; + + while(current != null) { + //Increment the counter by 1 for each node + counter++; + current = current.next; + } + return counter; + } + + //display() will print out the elements of the list + public void display() { + //Node current will point to head + Node current = head; + if(head == null) { + System.out.println("List is empty"); + return; + } + System.out.println("Nodes of doubly linked list: "); + while(current != null) { + //Prints each node by incrementing the pointer. + + System.out.print(current.data + " "); + current = current.next; + } + } + + public static void main(String[] args) { + + CountList dList = new CountList(); + //Add nodes to the list + dList.addNode(1); + dList.addNode(2); + dList.addNode(3); + dList.addNode(4); + dList.addNode(5); + + //Displays the nodes present in the list + dList.display(); + + //Counts the nodes present in the given list + System.out.println("\nCount of nodes present in the list: " + dList.countNodes()); + } +} From 82d644e180ede5e047f933842e7f0458534994eb Mon Sep 17 00:00:00 2001 From: Aditya kumar <72574147+adityakumar007@users.noreply.github.com> Date: Fri, 9 Oct 2020 00:57:20 +0530 Subject: [PATCH 39/63] nested for loops patterns --- pattern | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 pattern diff --git a/pattern b/pattern new file mode 100644 index 0000000..adef3ae --- /dev/null +++ b/pattern @@ -0,0 +1,30 @@ +package Pattern; +import java.util.Scanner; + +public class Pattern { + + public static void main(String[] args) { + Scanner sc= new Scanner(System.in); + int n=sc.nextInt(); + System.out.println("*"); + + + + + for(int i=2;i<=n-1;i++) { + System.out.print("* "); + + for(int j=1;j<=i-2;j++) { + System.out.print(" "); + } + System.out.print("* "); + System.out.println(); + } + + for(int i=1;i<=n;i++) { + System.out.print("* "); + } + + } + +} From 041f8737af74a736e08e405a917d67373d165e4a Mon Sep 17 00:00:00 2001 From: Sneha Satish Joshi <68151791+Snehajoshi312@users.noreply.github.com> Date: Fri, 9 Oct 2020 20:53:23 +0530 Subject: [PATCH 40/63] 1add.java Example 1: Convert boolean to string using valueOf() --- 1add.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 1add.java diff --git a/1add.java b/1add.java new file mode 100644 index 0000000..a158954 --- /dev/null +++ b/1add.java @@ -0,0 +1,16 @@ +class Main { + public static void main(String[] args) { + + // create boolean variables + boolean booleanValue1 = true; + boolean booleanValue2 = false; + + // convert boolean to string + // using valueOf() + String stringValue1 = String.valueOf(booleanValue1); + String stringValue2 = String.valueOf(booleanValue2); + + System.out.println(stringValue1); // true + System.out.println(stringValue2); // true + } +} From 73fa25c0e2c3e8cb12e9a40aa0a6ad857f49cbd2 Mon Sep 17 00:00:00 2001 From: Sneha Satish Joshi <68151791+Snehajoshi312@users.noreply.github.com> Date: Fri, 9 Oct 2020 21:28:13 +0530 Subject: [PATCH 41/63] sn,java arraylist --- sn.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 sn.java diff --git a/sn.java b/sn.java new file mode 100644 index 0000000..86a26b1 --- /dev/null +++ b/sn.java @@ -0,0 +1,18 @@ +import java.util.ArrayList; + +class Main { + public static void main(String[] args){ + // create ArrayList + ArrayList languages = new ArrayList<>(); + + // add() method without the index parameter + languages.add("Java"); + languages.add("C"); + languages.add("Python"); + System.out.println("ArrayList: " + languages); + + // add() method with the index parameter + languages.add(1, "JavaScript"); + System.out.println("Updated ArrayList: " + languages); + } +} From 6df96bcbcf6ff44cf9f04f67855f35e8db4d576e Mon Sep 17 00:00:00 2001 From: Naved2019khan <72648913+Naved2019khan@users.noreply.github.com> Date: Sat, 10 Oct 2020 15:58:23 +0530 Subject: [PATCH 42/63] Create Calculate Compound Interest --- Calculate Compound Interest | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Calculate Compound Interest diff --git a/Calculate Compound Interest b/Calculate Compound Interest new file mode 100644 index 0000000..83080f6 --- /dev/null +++ b/Calculate Compound Interest @@ -0,0 +1,24 @@ +#include +#include + +int main() +{ + float principal, rate, year, ci; + + printf("Enter principal: "); + scanf("%f", &principal); + + printf("Enter rate: "); + scanf("%f", &rate); + + printf("Enter time in years: "); + scanf("%f", &year); + + //calculate compound interest + + ci=principal*((pow((1+rate/100),year)-1)); + + printf("Compound interest is: %f\n",ci); + + return 0; +} From 181b875fb205ffd476ee0b637bd3fb53955fa3d2 Mon Sep 17 00:00:00 2001 From: Naved2019khan <72648913+Naved2019khan@users.noreply.github.com> Date: Sat, 10 Oct 2020 16:12:29 +0530 Subject: [PATCH 43/63] Create Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph) --- ...et 1 (Detect Cycle in an Undirected Graph) | 193 ++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph) diff --git a/Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph) b/Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph) new file mode 100644 index 0000000..d53ad22 --- /dev/null +++ b/Disjoint Set (Or Union-Find) | Set 1 (Detect Cycle in an Undirected Graph) @@ -0,0 +1,193 @@ +// Java Program for union-find algorithm to detect cycle in a graph + +import java.util.*; + +import java.lang.*; + +import java.io.*; + + + +class Graph +{ + + int V, E; // V-> no. of vertices & E->no.of edges + + Edge edge[]; // /collection of all edges + + + + class Edge + + { + + int src, dest; + + }; + + + + // Creates a graph with V vertices and E edges + + Graph(int v,int e) + + { + + V = v; + + E = e; + + edge = new Edge[E]; + + for (int i=0; i Date: Sat, 10 Oct 2020 19:33:28 +0530 Subject: [PATCH 44/63] Create Magic number --- Magic number | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 Magic number diff --git a/Magic number b/Magic number new file mode 100644 index 0000000..6144e9c --- /dev/null +++ b/Magic number @@ -0,0 +1,32 @@ +import java.util.Scanner; + +public class MagicNumber +{ + public static void main(String[] args) + { + int n, r = 1, num, sum = 0; + Scanner sc = new Scanner(System.in); + System.out.print("Enter number="); + n = sc.nextInt(); + num = n; + while (num > 9) + { + while (num > 0) + { + r = num % 10; + sum = sum + r; + num = num / 10; + } + num = sum; + sum = 0; + } + if (num == 1) + { + System.out.println("Magic Number"); + } + else + { + System.out.println("Not Magic Number"); + } + } +} From ce787c7fc096469f048638854e928a1469463e5e Mon Sep 17 00:00:00 2001 From: Bhanu Soam <72657935+Bhanu-cyber@users.noreply.github.com> Date: Sat, 10 Oct 2020 20:25:19 +0530 Subject: [PATCH 45/63] Create Binary Tree --- Binary Tree | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 Binary Tree diff --git a/Binary Tree b/Binary Tree new file mode 100644 index 0000000..3c082af --- /dev/null +++ b/Binary Tree @@ -0,0 +1,74 @@ + + + + + + + +// Binary Tree in Java + +// Node creation +class Node { + int key; + Node left, right; + + public Node(int item) { + key = item; + left = right = null; + } +} + +class BinaryTree { + Node root; + + BinaryTree(int key) { + root = new Node(key); + } + + BinaryTree() { + root = null; + } + + // Traverse Inorder + public void traverseInOrder(Node node) { + if (node != null) { + traverseInOrder(node.left); + System.out.print(" " + node.key); + traverseInOrder(node.right); + } + } + + // Traverse Postorder + public void traversePostOrder(Node node) { + if (node != null) { + traversePostOrder(node.left); + traversePostOrder(node.right); + System.out.print(" " + node.key); + } + } + + // Traverse Preorder + public void traversePreOrder(Node node) { + if (node != null) { + System.out.print(" " + node.key); + traversePreOrder(node.left); + traversePreOrder(node.right); + } + } + + public static void main(String[] args) { + BinaryTree tree = new BinaryTree(); + + tree.root = new Node(1); + tree.root.left = new Node(2); + tree.root.right = new Node(3); + tree.root.left.left = new Node(4); + + System.out.print("Pre order Traversal: "); + tree.traversePreOrder(tree.root); + System.out.print("\nIn order Traversal: "); + tree.traverseInOrder(tree.root); + System.out.print("\nPost order Traversal: "); + tree.traversePostOrder(tree.root); + } +} From 08f017989bd3c460c670f41669e86c73c47b4eb8 Mon Sep 17 00:00:00 2001 From: Jeet1243 <72684295+Jeet1243@users.noreply.github.com> Date: Sun, 11 Oct 2020 13:19:40 +0530 Subject: [PATCH 46/63] Create Area_of_circle.java This simple java program to calculate area of circle. Please try it. Thanks --- Area_of_circle.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 Area_of_circle.java diff --git a/Area_of_circle.java b/Area_of_circle.java new file mode 100644 index 0000000..d4a36b3 --- /dev/null +++ b/Area_of_circle.java @@ -0,0 +1,17 @@ +#https://www.facebook.com/jeet/posts/2729372946102639 +# Subscribed by Code House + +import java.util.Scanner; +public class Area +{ + public static void main(String[] args) + { + int r; + double pi = 3.14, area; + Scanner s = new Scanner(System.in); + System.out.print("Enter radius of circle:"); + r = s.nextInt(); + area = pi * r * r; + System.out.println("Area of circle:"+area); + } +} From 9fb7cde82a1ff6e146cea416701b760ee4f7aba2 Mon Sep 17 00:00:00 2001 From: aditya-gite-04 <55936621+aditya-gite-04@users.noreply.github.com> Date: Mon, 12 Oct 2020 02:16:18 +0530 Subject: [PATCH 47/63] Create EVIL number --- EVIL number | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 EVIL number diff --git a/EVIL number b/EVIL number new file mode 100644 index 0000000..07cb908 --- /dev/null +++ b/EVIL number @@ -0,0 +1,54 @@ +import java.util.*; +class EvilNumber +{ + String toBinary(int n) // Function to convert a number to Binary + { + int r; + String s=""; //variable for storing the result + + char dig[]={'0','1'}; //array storing the digits (as characters) in a binary number system + + while(n>0) + { + r=n%2; //finding remainder by dividing the number by 2 + s=dig[r]+s; //adding the remainder to the result and reversing at the same time + n=n/2; + } + return s; + } + + int countOne(String s) // Function to count no of 1's in binary number + { + int c = 0, l = s.length(); + char ch; + for(int i=0; i Date: Mon, 12 Oct 2020 08:43:53 +0530 Subject: [PATCH 48/63] Create NCR.java --- NCR.java | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 NCR.java diff --git a/NCR.java b/NCR.java new file mode 100644 index 0000000..75ca580 --- /dev/null +++ b/NCR.java @@ -0,0 +1,47 @@ +import java.util.Scanner; +class NCR +{ + public static void main(String[] args) + { + Scanner s = new Scanner(System.in); + System.out.println("Enter number of times"); + int times = Integer.parseInt(s.nextLine()); + for(int i=0;i0) + { + + char c = sb.charAt(0); + if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') + { + sb.replace(0,1,"z"); + + } + else + { + sb.deleteCharAt(0); + } + if(chance) + a++; + else + b++; + chance = !chance; + } + if(a>b) + System.out.println("A"); + else if(b>a) + System.out.println("B"); + else + System.out.println("D"); + } + + } +} From c4bb1965457c6c34d1bcb56a6b678f1756a4be19 Mon Sep 17 00:00:00 2001 From: anvikshik <47172530+anvikshik@users.noreply.github.com> Date: Mon, 12 Oct 2020 09:45:29 +0530 Subject: [PATCH 49/63] Sorting LinkedList.java --- Sorting LinkedList.java | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Sorting LinkedList.java diff --git a/Sorting LinkedList.java b/Sorting LinkedList.java new file mode 100644 index 0000000..6b70da7 --- /dev/null +++ b/Sorting LinkedList.java @@ -0,0 +1,26 @@ +import java.util.Collections; +import java.util.Comparator; +import java.util.LinkedList; +public class LinkedListSorting{ public static void main(String args[]) { + +// Creating and initializing an LinkedList for sorting +LinkedList singlyLinkedList = new LinkedList<>(); +singlyLinkedList.add("Eclipse"); +singlyLinkedList.add("NetBeans"); +singlyLinkedList.add("IntelliJ"); +singlyLinkedList.add("Resharper"); +singlyLinkedList.add("Visual Studio"); +singlyLinkedList.add("notepad"); +System.out.println("LinkedList (before sorting): " + singlyLinkedList); + +//Sorting LinkedList with Collecitons.sort() method in natural order +Collections.sort(singlyLinkedList); + +System.out.println("LinkedList (after sorting in natural): " + singlyLinkedList); + +// Example 2 - Sorting LinkedList using Collection.sort() and Comparator in Java +Collections.sort(singlyLinkedList, new Comparator() { +@Override +public int compare(String s1, String s2) { return s1.length() - s2.length(); } } ); + +System.out.println("LinkedList (after sorting using Comparator): " + singlyLinkedList); } } From 5f65ddeeb995983da3f2187373b3c0d7c8ba10a1 Mon Sep 17 00:00:00 2001 From: Kldeep <72750959+Kldeep@users.noreply.github.com> Date: Mon, 12 Oct 2020 21:11:32 +0530 Subject: [PATCH 50/63] Create example of Quicksort Algorithm in java --- example of Quicksort Algorithm in java | 58 ++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 example of Quicksort Algorithm in java diff --git a/example of Quicksort Algorithm in java b/example of Quicksort Algorithm in java new file mode 100644 index 0000000..87b9ef2 --- /dev/null +++ b/example of Quicksort Algorithm in java @@ -0,0 +1,58 @@ +// Quick sort in Java + +import java.util.Arrays; + +class QuickSort { + + // Function to partition the array on the basis of pivot element + int partition(int array[], int low, int high) { + + // Select the pivot element + int pivot = array[high]; + int i = (low - 1); + + // Put the elements smaller than pivot on the left and + // greater than pivot on the right of pivot + for (int j = low; j < high; j++) { + if (array[j] <= pivot) { + i++; + int temp = array[i]; + array[i] = array[j]; + array[j] = temp; + } + } + int temp = array[i + 1]; + array[i + 1] = array[high]; + array[high] = temp; + return (i + 1); + } + + void quickSort(int array[], int low, int high) { + if (low < high) { + + // Select pivot position and put all the elements smaller + // than pivot on left and greater than pivot on right + int pi = partition(array, low, high); + + // Sort the elements on the left of pivot + quickSort(array, low, pi - 1); + + // Sort the elements on the right of pivot + quickSort(array, pi + 1, high); + } + } + + // Driver code + public static void main(String args[]) { + int[] data = { 8, 7, 2, 1, 0, 9, 6 }; + int size = data.length; + QuickSort qs = new QuickSort(); + qs.quickSort(data, 0, size - 1); + System.out.println("Sorted Array in Ascending Order: "); + System.out.println(Arrays.toString(data)); + } +} +Quicksort Complexity +Time Complexities + + From 75c6076050ba647b669a8c047faf6a9572a5bd9a Mon Sep 17 00:00:00 2001 From: Kldeep <72750959+Kldeep@users.noreply.github.com> Date: Mon, 12 Oct 2020 21:33:03 +0530 Subject: [PATCH 51/63] Create Java code explaining atan(), ceil(), copySign() method in lang.Math class --- ...il(), copySign() method in lang.Math class | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 Java code explaining atan(), ceil(), copySign() method in lang.Math class diff --git a/Java code explaining atan(), ceil(), copySign() method in lang.Math class b/Java code explaining atan(), ceil(), copySign() method in lang.Math class new file mode 100644 index 0000000..e10071c --- /dev/null +++ b/Java code explaining atan(), ceil(), copySign() method in lang.Math class @@ -0,0 +1,76 @@ +// Java program explaining lang.Math class methods +// atan(), ceil(), copySign() + + + +import java.math.*; + +public class NewClass +{ + + public static void main(String[] args) + + { + + // Use of atan() method + + double Atani = Math.atan(0); + + System.out.println("atan value of Atani : "+Atani); + + double x = Math.PI/2; + + + + // Use of toRadian() method + + x = Math.toRadians(x); + + double Atanj = Math.atan(x); + + System.out.println("atan value of Atanj : "+Atanj); + + System.out.println(""); + + + + + + // Use of ceil() method + + double val = 15.34 ,ceilval; + + ceilval = Math.ceil(val); + + System.out.println("ceil value of val : "+ceilval); + + System.out.println(""); + + + + double dblMag = val; + + double dblSign1 = 3; + + double dblSign2 = -3; + + + + + + // Use of copySign() method + + double result1 = Math.copySign(dblMag,dblSign1); + + System.out.println("copySign1 : "+result1); + + + + double result2 = Math.copySign(dblMag,dblSign2); + + System.out.println("copySign2 : "+result2); + + + + } +} From 63e1e85405832765dd8975f4aa50b3d25016eb71 Mon Sep 17 00:00:00 2001 From: Kunal0cr7 <43791088+Kunal0cr7@users.noreply.github.com> Date: Mon, 12 Oct 2020 21:54:09 +0530 Subject: [PATCH 52/63] Create airport_algo.java --- airport_algo.java | 94 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 airport_algo.java diff --git a/airport_algo.java b/airport_algo.java new file mode 100644 index 0000000..28205de --- /dev/null +++ b/airport_algo.java @@ -0,0 +1,94 @@ +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.PriorityQueue; + +public class DijkstraSingleSourceShortestPath { + + public Integer[][] singleSourceShortestPath(Integer[][] weight,int source){ + //auxiliary constants + final int SIZE = weight.length; + final int EVE = -1;//to indicate no predecessor + final int INFINITY = Integer.MAX_VALUE; + + //declare and initialize pred to EVE and minDist to INFINITY + Integer[] pred = new Integer[SIZE]; + Integer[] minDist = new Integer[SIZE]; + Arrays.fill(pred, EVE); + Arrays.fill(minDist, INFINITY); + + //set minDist[source] =0 because source is 0 distance from itself. + minDist[source] = 0; + + PriorityQueue pq = createPriorityQueue(minDist); + + while (!pq.isEmpty()) { + updatePriorityQueue(pq); + int v = pq.remove()[0]; + for (Integer[] XD : adjacency(weight, pq, v)) { + Integer x = XD[0]; + //triangle inequality + if (null != x && minDist[x] > minDist[v] + weight[v][x]) { + minDist[x] = minDist[v] + weight[v][x]; + pred[XD[0]] = v; + XD[1] = minDist[x];//update pq. + } + } + } + Integer[][] result = {pred, minDist}; + return result; + } + + /********************************************************************* + * Create a priority queue and load it with the vertices sorted by + * minDist. + ********************************************************************/ + private PriorityQueue createPriorityQueue(Integer[] dist) { + PriorityQueue pq = new PriorityQueue(11, + new Comparator() { + public int compare(Integer[] A, Integer[] B) { + return A[1] < B[1] ? -1 : 1; + } + }); + for (int v = 0; v < dist.length; v++) { + pq.add(new Integer[]{v, dist[v]}); + } + return pq; + } + + /****************************************************************** + * Retrieve all the neighbors of vertex v that are + * in the priority queue pq. + *****************************************************************/ + private List adjacency(Integer[][] G, + PriorityQueue pq, int v) { + List result = new ArrayList(); + for (Integer[] ent : pq) {// {u,key[u]} + int u = ent[0]; + if (G[v][u] != null) { + result.add(ent); + } + } + return result; + } + + /***************************************************************** + * Re-prioritize the queue based on changes in the + * minDist array. + * + * Technical Details: Dijktra's algorithm requires a priority queue + * that changes continuously to reflect changes in minDist. + * For Java it does not suffice to simply pass new values to + * the array objects that constitute the queue. The + * PriorityQueue data structure in Java only checks its structure + * when it is adding or removing elements. It is unaware of any + * direct changes to the objects it comprises. Therefore to force + * the queue to re-prioritize, an element is removed and then + * immediately added back. + * + *****************************************************************/ + private void updatePriorityQueue(PriorityQueue pq) { + pq.add(pq.remove()); + } +} From f5a148fd4c05510cc99fd2a754860156fde0a1c1 Mon Sep 17 00:00:00 2001 From: Utkarsh1520 <72489678+Utkarsh1520@users.noreply.github.com> Date: Tue, 13 Oct 2020 00:43:26 +0530 Subject: [PATCH 53/63] A java program for basic calculator operations --- Basic Calculator Operations.java | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Basic Calculator Operations.java diff --git a/Basic Calculator Operations.java b/Basic Calculator Operations.java new file mode 100644 index 0000000..b2c82ed --- /dev/null +++ b/Basic Calculator Operations.java @@ -0,0 +1,38 @@ +#Subscribed to CodeHouseIndia +import java.util.Scanner; +public class Calculator { +public static void main(String[] args) { +Scanner reader = new Scanner(System.in); +System.out.print("Enter two numbers: "); +// nextDouble() reads the next double from the keyboard +double first = reader.nextDouble(); +double second = reader.nextDouble(); +System.out.print("Enter an operator (+, -, *, /): "); +char operator = reader.next().charAt(0); +double result; +//switch case for each of the operations +switch(operator) +{ +case '+': +result = first + second; +break; +case '-': +result = first - second; +break; +case '*': +result = first * second; +break; +case '/': +result = first / second; +break; +// operator doesn't match any case constant (+, -, *, /) + + +default: +System.out.printf("Error! operator is not correct"); +return; +} +//printing the result of the operations +System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result); +} +} From f77f7fbeb6ba2542e623001898f9c723820c22dc Mon Sep 17 00:00:00 2001 From: sudarshan412 <67834912+sudarshan412@users.noreply.github.com> Date: Tue, 13 Oct 2020 01:01:14 +0530 Subject: [PATCH 54/63] Added Compound Interest program --- compundinterest.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 compundinterest.java diff --git a/compundinterest.java b/compundinterest.java new file mode 100644 index 0000000..170014f --- /dev/null +++ b/compundinterest.java @@ -0,0 +1,16 @@ +#https://www.facebook.com/suyash.ssp/posts/2629289193987053 +#posted by suyash + +public class JavaExample { + + public void calculate(int p, int t, double r, int n) { + double amount = p * Math.pow(1 + (r / n), n * t); + double cinterest = amount - p; + System.out.println("Compound Interest after " + t + " years: "+cinterest); + System.out.println("Amount after " + t + " years: "+amount); + } + public static void main(String args[]) { + JavaExample obj = new JavaExample(); + obj.calculate(2000, 5, .08, 12); + } +} From 024f57c41182b7e802e0665e4f7f12a1ca60dcc0 Mon Sep 17 00:00:00 2001 From: JayPatel1060 <72780261+JayPatel1060@users.noreply.github.com> Date: Tue, 13 Oct 2020 11:34:35 +0530 Subject: [PATCH 55/63] check automorphicity of java --- checkautomorphic.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 checkautomorphic.java diff --git a/checkautomorphic.java b/checkautomorphic.java new file mode 100644 index 0000000..43d9e8b --- /dev/null +++ b/checkautomorphic.java @@ -0,0 +1,23 @@ +# https://www.facebook.com/permalink.php?story_fbid=1289077461428963&id=100009801635737 +# subscribed by jay patel +# java program to find automorphic +import java.util.*; +class Automorphic +{ + public static void main(String args[]) throws Exception + { + Scanner sc = new Scanner(System.in); + System.out.print("Enter a Number : "); // Inputting the number + int n = sc.nextInt(); + int sq = n*n; // Finding the square + + String num = Integer.toString(n); // Converting the number to String + String square = Integer.toString(sq); // Converting the square to String + + if(square.endsWith(num)) // If the square ends with the number then it is Automorphic + System.out.print(n+" is an Automorphic Number."); + else + System.out.print(n+" is not an Automorphic Number."); + } +} + From 7bbdc4991dd3220c937fc2656d4b2ae9607f714d Mon Sep 17 00:00:00 2001 From: sagar_pro <47023454+shivamadlakha@users.noreply.github.com> Date: Tue, 13 Oct 2020 18:07:35 +0530 Subject: [PATCH 56/63] Spanning Tree Kruskal Algorithm for spanning Tree using Java --- spanning_tree | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 spanning_tree diff --git a/spanning_tree b/spanning_tree new file mode 100644 index 0000000..41964e0 --- /dev/null +++ b/spanning_tree @@ -0,0 +1,27 @@ +ValueGraph spanningTree(ValueGraph graph, boolean minSpanningTree) { + Set edges = graph.edges(); + List edgeList = new ArrayList<>(edges); + + if (minSpanningTree) { + edgeList.sort(Comparator.comparing(e -> graph.edgeValue(e).get())); + } else { + edgeList.sort(Collections.reverseOrder(Comparator.comparing(e -> graph.edgeValue(e).get()))); + } + + int totalNodes = graph.nodes().size(); + CycleDetector cycleDetector = new CycleDetector(totalNodes); + int edgeCount = 0; + + MutableValueGraph spanningTree = ValueGraphBuilder.undirected().build(); + for (EndpointPair edge : edgeList) { + if (cycleDetector.detectCycle(edge.nodeU(), edge.nodeV())) { + continue; + } + spanningTree.putEdgeValue(edge.nodeU(), edge.nodeV(), graph.edgeValue(edge).get()); + edgeCount++; + if (edgeCount == totalNodes - 1) { + break; + } + } + return spanningTree; +} From 6dbb438c72cb1c64e443ed3c6207d24449c44986 Mon Sep 17 00:00:00 2001 From: aditya-gite-04 <55936621+aditya-gite-04@users.noreply.github.com> Date: Tue, 13 Oct 2020 20:16:02 +0530 Subject: [PATCH 57/63] Create HappyNumber.java --- HappyNumber.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 HappyNumber.java diff --git a/HappyNumber.java b/HappyNumber.java new file mode 100644 index 0000000..dc1c618 --- /dev/null +++ b/HappyNumber.java @@ -0,0 +1,27 @@ +public class HappyNumber +{ + public static int isHappyNumber(int num){ + int rem = 0, sum = 0; + + while(num > 0){ + rem = num%10; + sum = sum + (rem*rem); + num = num/10; + } + return sum; + } + + public static void main(String[] args) { + int num = 82; + int result = num; + + while(result != 1 && result != 4){ + result = isHappyNumber(result); + } + + if(result == 1) + System.out.println(num + " is a happy number"); + else if(result == 4) + System.out.println(num + " is not a happy number"); + } +} From a577ce0a3cb679e0f585c44d980eb63ce49854aa Mon Sep 17 00:00:00 2001 From: anvikshik <47172530+anvikshik@users.noreply.github.com> Date: Tue, 13 Oct 2020 20:55:20 +0530 Subject: [PATCH 58/63] Caesar Cipher.java --- Caesar Cipher.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 Caesar Cipher.java diff --git a/Caesar Cipher.java b/Caesar Cipher.java new file mode 100644 index 0000000..99ec458 --- /dev/null +++ b/Caesar Cipher.java @@ -0,0 +1,35 @@ +class CaesarCipher +{ + public static StringBuffer encrypt(String text, int s) + { + StringBuffer result= new StringBuffer(); + + for (int i=0; i Date: Wed, 14 Oct 2020 20:27:13 +0530 Subject: [PATCH 59/63] Create fact.java --- fact.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 fact.java diff --git a/fact.java b/fact.java new file mode 100644 index 0000000..ae621fe --- /dev/null +++ b/fact.java @@ -0,0 +1,14 @@ +public class Factorial { + + public static void main(String[] args) { + + int num = 10; + long factorial = 1; + for(int i = 1; i <= num; ++i) + { + // factorial = factorial * i; + factorial *= i; + } + System.out.printf("Factorial of %d = %d", num, factorial); + } +} From 46da767087492f5d37dbe3d47ed74bf5df7852f6 Mon Sep 17 00:00:00 2001 From: aditya-gite-04 <55936621+aditya-gite-04@users.noreply.github.com> Date: Wed, 14 Oct 2020 23:33:22 +0530 Subject: [PATCH 60/63] Create AbundantNumber.java --- AbundantNumber.java | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 AbundantNumber.java diff --git a/AbundantNumber.java b/AbundantNumber.java new file mode 100644 index 0000000..8a80f47 --- /dev/null +++ b/AbundantNumber.java @@ -0,0 +1,56 @@ +import java.io.*; +import java.math.*; + +// Function to calculate sum of divisors +class GFG{ + static int getSum(int n) + { + int sum = 0; + + // Note that this loop runs till square + // root of n + for (int i=1; i<=(Math.sqrt(n)); i++) + { + if (n%i==0) + { + // If divisors are equal,take only + // one of them + if (n/i == i) + sum = sum + i; + + else // Otherwise take both + { + sum = sum + i; + sum = sum + (n / i); + } + } + } + + // calculate sum of all proper divisors + // only + sum = sum - n; + return sum; + } + + // Function to check Abundant Number + static boolean checkAbundant(int n) + { + // Return true if sum of divisors is + // greater than n. + return (getSum(n) > n); + } + + /* Driver program to test above function */ + public static void main(String args[])throws + IOException + { + if(checkAbundant(12)) + System.out.println("YES"); + else + System.out.println("NO"); + if(checkAbundant(15)) + System.out.println("YES"); + else + System.out.println("NO"); + } +} From 157aa5b8409053a27984bf58f24c1a33bb3dc7bf Mon Sep 17 00:00:00 2001 From: aditya-gite-04 <55936621+aditya-gite-04@users.noreply.github.com> Date: Wed, 14 Oct 2020 23:37:09 +0530 Subject: [PATCH 61/63] Update AbundantNumber.java --- AbundantNumber.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/AbundantNumber.java b/AbundantNumber.java index 8a80f47..c016854 100644 --- a/AbundantNumber.java +++ b/AbundantNumber.java @@ -2,7 +2,7 @@ import java.math.*; // Function to calculate sum of divisors -class GFG{ +class AbundantNumber{ static int getSum(int n) { int sum = 0; From 9766bb8071b2da5e8dbcf8ac6b0d0de92c32a59f Mon Sep 17 00:00:00 2001 From: aditya-gite-04 <55936621+aditya-gite-04@users.noreply.github.com> Date: Wed, 14 Oct 2020 23:37:56 +0530 Subject: [PATCH 62/63] Rename EVIL number to EvilNumber.java --- EVIL number => EvilNumber.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename EVIL number => EvilNumber.java (100%) diff --git a/EVIL number b/EvilNumber.java similarity index 100% rename from EVIL number rename to EvilNumber.java From 97b6c30d59d7be9ef36b4d4ab5681230b58b3080 Mon Sep 17 00:00:00 2001 From: Aditya kumar <72574147+adityakumar007@users.noreply.github.com> Date: Thu, 15 Oct 2020 19:32:42 +0530 Subject: [PATCH 63/63] Create java Program to Implement stack data structure java Program to Implement stack data structure --- ... Program to Implement stack data structure | 88 +++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 java Program to Implement stack data structure diff --git a/java Program to Implement stack data structure b/java Program to Implement stack data structure new file mode 100644 index 0000000..8492243 --- /dev/null +++ b/java Program to Implement stack data structure @@ -0,0 +1,88 @@ +// Stack implementation in Java + +class Stack { + + // store elements of stack + private int arr[]; + // represent top of stack + private int top; + // total capacity of the stack + private int capacity; + + // Creating a stack + Stack(int size) { + // initialize the array + // initialize the stack variables + arr = new int[size]; + capacity = size; + top = -1; + } + + // push elements to the top of stack + public void push(int x) { + if (isFull()) { + System.out.println("Stack OverFlow"); + + // terminates the program + System.exit(1); + } + + // insert element on top of stack + System.out.println("Inserting " + x); + arr[++top] = x; + } + + // pop elements from top of stack + public int pop() { + + // if stack is empty + // no element to pop + if (isEmpty()) { + System.out.println("STACK EMPTY"); + // terminates the program + System.exit(1); + } + + // pop element from top of stack + return arr[top--]; + } + + // return size of the stack + public int getSize() { + return top + 1; + } + + // check if the stack is empty + public Boolean isEmpty() { + return top == -1; + } + + // check if the stack is full + public Boolean isFull() { + return top == capacity - 1; + } + + // display elements of stack + public void printStack() { + for (int i = 0; i <= top; i++) { + System.out.print(arr[i] + ", "); + } + } + + public static void main(String[] args) { + Stack stack = new Stack(5); + + stack.push(1); + stack.push(2); + stack.push(3); + + System.out.print("Stack: "); + stack.printStack(); + + // remove element from stack + stack.pop(); + System.out.println("\nAfter popping out"); + stack.printStack(); + + } +}