diff --git a/Area&CircumfrenceofCircle b/Area&CircumfrenceofCircle new file mode 100644 index 0000000..2a96580 --- /dev/null +++ b/Area&CircumfrenceofCircle @@ -0,0 +1,19 @@ +import java.util.Scanner; +class CircleDemo +{ + static Scanner sc = new Scanner(System.in); + public static void main(String args[]) + { + System.out.print("Enter the radius: "); + /*We are storing the entered radius in double + * because a user can enter radius in decimals + */ + double radius = sc.nextDouble(); + //Area = PI*radius*radius + double area = Math.PI * (radius * radius); + System.out.println("The area of circle is: " + area); + //Circumference = 2*PI*radius + double circumference= Math.PI * 2*radius; + System.out.println( "The circumference of the circle is:"+circumference) ; + } +} diff --git a/Armstrong number b/Armstrong number new file mode 100644 index 0000000..c69a2f3 --- /dev/null +++ b/Armstrong number @@ -0,0 +1,18 @@ +Thank you bro!Just subscribed to your channel! +class ArmstrongExample{ + public static void main(String[] args) { + int c=0,a,temp; + int n=153;//It is the number to check armstrong + temp=n; + while(n>0) + { + a=n%10; + n=n/10; + c=c+(a*a*a); + } + if(temp==c) + System.out.println("armstrong number"); + else + System.out.println("Not armstrong number"); + } +} diff --git a/Beep_sound.java b/Beep_sound.java new file mode 100644 index 0000000..83de2c6 --- /dev/null +++ b/Beep_sound.java @@ -0,0 +1,9 @@ +#https://www.facebook.com/jyothiprakash.patnaikuni/posts/107593291135289 +# subscribed by jyothiprakash patnaik +import java.awt.*; +public class Beep_sound { + + public static void main(String[] args) { + Toolkit.getDefaultToolkit().beep(); + } +} diff --git a/CheckPrimeNumber b/CheckPrimeNumber new file mode 100644 index 0000000..f7f0b9e --- /dev/null +++ b/CheckPrimeNumber @@ -0,0 +1,19 @@ +public class PrimeExample{ + public static void main(String args[]){ + int i,m=0,flag=0; + int n=3;//it is the number to be checked + m=n/2; + if(n==0||n==1){ + System.out.println(n+" is not prime number"); + }else{ + for(i=2;i<=m;i++){ + if(n%i==0){ + System.out.println(n+" is not prime number"); + flag=1; + break; + } + } + if(flag==0) { System.out.println(n+" is prime number"); } + }//end of else +} +} diff --git a/Factorial b/Factorial new file mode 100644 index 0000000..6b6ee15 --- /dev/null +++ b/Factorial @@ -0,0 +1,13 @@ +//https://www.facebook.com/srishti.agrawal.714/posts/162113345582696 +//Subscribed by Srishti Agrawal +public class Factorial +{ + public static void main(String[] args) + { + int n=Integer.parseInt(args[0]); + int f=1; + for(int i=1;i<=n;i++) + f*=i; + System.out.println("Factorial is "+ f); + } +} diff --git a/FactorialProgram b/FactorialProgram new file mode 100644 index 0000000..335fbca --- /dev/null +++ b/FactorialProgram @@ -0,0 +1,10 @@ +class FactorialExample{ + public static void main(String args[]){ + int i,fact=1; + int number=5;//It is the number to calculate factorial + for(i=1;i<=number;i++){ + fact=fact*i; + } + System.out.println("Factorial of "+number+" is: "+fact); + } +} diff --git a/Fibonacci b/Fibonacci new file mode 100644 index 0000000..52aa8eb --- /dev/null +++ b/Fibonacci @@ -0,0 +1,19 @@ +public class FibonacciSeries { + + public static void main(String[] args) { + printFibonacciSeries(10); + } + + public static void printFibonacciSeries(int count) { + int a = 0; + int b = 1; + int c = 1; + for (int i = 1; i <= count; i++) { + System.out.print(a + ", "); + a = b; + b = c; + c = a + b; + } + + } +} diff --git a/Fibonacci Series b/Fibonacci Series new file mode 100644 index 0000000..e0f0779 --- /dev/null +++ b/Fibonacci Series @@ -0,0 +1,17 @@ +public class Fibonacci { + + 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.print(t1 + " + "); + + int sum = t1 + t2; + t1 = t2; + t2 = sum; + } + } +} diff --git a/HelloWorld b/HelloWorld new file mode 100644 index 0000000..e1a0fdb --- /dev/null +++ b/HelloWorld @@ -0,0 +1,5 @@ +class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } +} diff --git a/Java Array list.java b/Java Array list.java new file mode 100644 index 0000000..5812a21 --- /dev/null +++ b/Java Array list.java @@ -0,0 +1,32 @@ +import java.io.*; +import java.util.*; + +public class Solution { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int l = sc.nextInt(); + ArrayList [ ] list = new ArrayList[l]; + + for(int i = 0; i < l; i ++) { + int n = sc.nextInt(); + list[i] = new ArrayList(); + while(n -- > 0) { + list[i].add(sc.nextInt()); + } + } + + int q = sc.nextInt(); + while(q -- > 0) { + int x = sc.nextInt(); + int y = sc.nextInt(); + + try { + System.out.println(list[x - 1].get(y - 1)); + } catch (Exception e) { + System.out.println("ERROR!"); + } + } + + } +} diff --git a/Java Subarray.java b/Java Subarray.java new file mode 100644 index 0000000..2a4df14 --- /dev/null +++ b/Java Subarray.java @@ -0,0 +1,27 @@ +// https://www.facebook.com/abhi.sensharma/posts/1241788872853419 +// Subscribed by Abhishek Sharma + +import java.io.*; +import java.util.*; + +public class Solution { + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int [] arr = new int [n]; + for(int i = 0; i < n; i ++) { + arr[i] = sc.nextInt(); + } + + int count = 0, sum = 0; + for(int i = 0; i < n; i ++) { + for(int j = i; j < n; j ++) { + sum += arr[j]; + if(sum < 0) count ++; + } + sum = 0; + } + System.out.println(count); + } +} diff --git a/MyHelloWorld b/MyHelloWorld new file mode 100644 index 0000000..e1a0fdb --- /dev/null +++ b/MyHelloWorld @@ -0,0 +1,5 @@ +class HelloWorld { + public static void main(String[] args) { + System.out.println("Hello, World!"); + } +} diff --git a/Palindrome b/Palindrome new file mode 100644 index 0000000..00f167d --- /dev/null +++ b/Palindrome @@ -0,0 +1,21 @@ +//https://www.facebook.com/srishti.agrawal.714/posts/162113345582696 +//Subscribed by Code House +import java.util.Scanner; +class Palindrome { + public static void main(String args[]) { + Scanner input=new Scanner(System.in); + int rem,sum=0,rev; + int n=input.nextInt();//It is the number variable to be checked for palindrome + + rev=n; + while(n>0) { + rem=n%10; //getting remainder + sum=(sum*10)+rem; + n=n/10; + } + if(rev==sum) + System.out.println("It is a palindrome number "); + else + System.out.println("It is not a palindrome number"); +} +} diff --git a/Postordertraversal.java b/Postordertraversal.java new file mode 100644 index 0000000..2ecdd71 --- /dev/null +++ b/Postordertraversal.java @@ -0,0 +1,54 @@ +# https://www.facebook.com/dikshit.kaushal.564/posts/103324861581146 +# Subscribed By Dikshit Kaushal + +class Node { + int item; + Node left, right; + + public Node(int key) { + item = key; + left = right = null; + } +} + +class Tree { + // Root of Binary Tree + Node root; + + Tree() { + root = null; + } + + void postorder(Node node) { + if (node == null) + return; + + // traverse the left child + postorder(node.left); + + // traverse the right child + postorder(node.right); + + // traverse the root node + System.out.print(node.item + "->"); + } + + public static void main(String[] args) { + + // create an object of Tree + Tree tree = new Tree(); + + // create nodes of the tree + tree.root = new Node(1); + tree.root.left = new Node(12); + tree.root.right = new Node(9); + + // child nodes of left child + tree.root.left.left = new Node(5); + tree.root.left.right = new Node(6); + + // postorder tree traversal + System.out.println("Postorder traversal"); + tree.postorder(tree.root); + } +} diff --git a/Prime Number Code b/Prime Number Code new file mode 100644 index 0000000..bbd6035 --- /dev/null +++ b/Prime Number Code @@ -0,0 +1,19 @@ +public class Prime{ + public static void main(String args[]){ + int i,m=0,flag=0; + int n=3;//it is the number to be checked + m=n/2; + if(n==0||n==1){ + System.out.println(n+" is not prime number"); + }else{ + for(i=2;i<=m;i++){ + if(n%i==0){ + System.out.println(n+" is not prime number"); + flag=1; + break; + } + } + if(flag==0) { System.out.println(n+" is prime number"); } + }//end of else +} +} diff --git a/README.md b/README.md index 549bc65..fa28cf2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,22 @@ # Java-Programs Share your Java Programs here. + +//The palindrome program + + class Palindrome{ + public static void main(String args[]){ + int r,sum=0,temp; + int n=454;//It is the number variable to be checked for palindrome + + temp=n; + while(n>0){ + r=n%10; //getting remainder + sum=(sum*10)+r; + n=n/10; + } + if(temp==sum) + System.out.println("palindrome number "); + else + System.out.println("not palindrome"); + } +} diff --git a/Reverse b/Reverse new file mode 100644 index 0000000..bbaaa75 --- /dev/null +++ b/Reverse @@ -0,0 +1,21 @@ +public class JavaReverseString { + + public static void main(String[] args) { + System.out.println(reverseString("abc")); + System.out.println(reverseString("123!@#098*")); + } + + public static String reverseString(String in) { + if (in == null) + return null; + StringBuilder out = new StringBuilder(); + + int length = in.length(); + + for (int i = length - 1; i >= 0; i--) { + out.append(in.charAt(i)); + } + + return out.toString(); + } +} diff --git a/Sum of three numbers b/Sum of three numbers new file mode 100644 index 0000000..9c48031 --- /dev/null +++ b/Sum of three numbers @@ -0,0 +1,12 @@ +package com.company; +public class Main { + public static void main(String[] args) + { + System.out.println("The sum of these numbers is: "); + int a= 10; + int b=33; + int c=77; + int sum=a+b+c; + System.out.println("sum"); + } +} diff --git a/arpit tripathi.md b/arpit tripathi.md new file mode 100644 index 0000000..11b2295 --- /dev/null +++ b/arpit tripathi.md @@ -0,0 +1,22 @@ +public class Main { + + public static void main(String[] args) { + + int num = 29; + boolean flag = false; + for(int i = 2; i <= num/2; ++i) + { + // condition for nonprime number + if(num % i == 0) + { + flag = true; + break; + } + } + + if (!flag) + System.out.println(num + " is a prime number."); + else + System.out.println(num + " is not a prime number."); + } +} diff --git a/arpit.tripathi.md b/arpit.tripathi.md new file mode 100644 index 0000000..4038bfd --- /dev/null +++ b/arpit.tripathi.md @@ -0,0 +1,25 @@ +public class Test { + + public static void main(String args[]) { + // char grade = args[0].charAt(0); + char grade = 'C'; + + switch(grade) { + case 'A' : + System.out.println("Excellent!"); + break; + case 'B' : + case 'C' : + System.out.println("Well done"); + break; + case 'D' : + System.out.println("You passed"); + case 'F' : + System.out.println("Better try again"); + break; + default : + System.out.println("Invalid grade"); + } + System.out.println("Your grade is " + grade); + } +} diff --git a/arpittripathi.md b/arpittripathi.md new file mode 100644 index 0000000..6079da2 --- /dev/null +++ b/arpittripathi.md @@ -0,0 +1,22 @@ +package MyPackage; + +public class Armstrong { + public static void main(String[] args) { + int number = 9474, originalNumber, remainder, result = 0, n = 0; + originalNumber = number; + for (;originalNumber != 0; originalNumber /= 10) + { + n++; + } + originalNumber = number; + for (;originalNumber != 0; originalNumber /= 10) + { + remainder = originalNumber % 10; + result += Math.pow(remainder, n); + } + if(result == number) + System.out.println(number + " is an Armstrong number."); + else + System.out.println(number + " is not an Armstrong number."); + } +} diff --git a/binarytodecimal.java b/binarytodecimal.java new file mode 100644 index 0000000..e96204d --- /dev/null +++ b/binarytodecimal.java @@ -0,0 +1,33 @@ + +#https://www.facebook.com/100028679802914/posts/449717249327598/ +#subcribed by Nikita Desale +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)); + } +} diff --git a/complex_number.java b/complex_number.java new file mode 100644 index 0000000..a4391fd --- /dev/null +++ b/complex_number.java @@ -0,0 +1,31 @@ +#https://www.facebook.com/shruti.buchha.7/posts/3341900509242443 +#subscribed by shruti buchha +import java.util.*; +import java.lang.*; +import java.io.*; +public class ComplexNumber{ + + double real, img; + + ComplexNumber(double r, double i){ + this.real = r; + this.img = i; + } + + public static ComplexNumber sum(ComplexNumber c1, ComplexNumber c2) + { + + ComplexNumber temp = new ComplexNumber(0, 0); + + temp.real = c1.real + c2.real; + temp.img = c1.img + c2.img; + + return temp; + } + public static void main(String args[]) { + ComplexNumber c1 = new ComplexNumber(5.5, 4); + ComplexNumber c2 = new ComplexNumber(1.2, 3.5); + ComplexNumber temp = sum(c1, c2); + System.out.printf("Sum is: "+ temp.real+" + "+ temp.img +"i"); + } +} diff --git a/factorial.java b/factorial.java new file mode 100644 index 0000000..335fbca --- /dev/null +++ b/factorial.java @@ -0,0 +1,10 @@ +class FactorialExample{ + public static void main(String args[]){ + int i,fact=1; + int number=5;//It is the number to calculate factorial + for(i=1;i<=number;i++){ + fact=fact*i; + } + System.out.println("Factorial of "+number+" is: "+fact); + } +} diff --git a/floyd.java b/floyd.java new file mode 100644 index 0000000..540e6b4 --- /dev/null +++ b/floyd.java @@ -0,0 +1,18 @@ +#https://www.facebook.com/lakshay.gulati.5/posts/3439658002768650 +# subscribed by Lakshay +public class floyd { + + public static void main(String[] args) { + int x=1; + for (int j =1;j<=5;j++){ + for (int i=0;i=0;i--){ + store[i]=(num1%10); + num1=(num1/10); + sum=sum+(store[i]*(i+1)); + } + System.out.println(sum); + if ((sum%11)==0) { + System.out.println("Legal ISBN"); + } + else{ + System.out.println("Illegal ISBN"); + } + + } + else{ + System.out.println("Illegal ISBN"); + } + } +} diff --git a/left rotation b/left rotation new file mode 100644 index 0000000..1ef8e5c --- /dev/null +++ b/left rotation @@ -0,0 +1,37 @@ +#https://www.facebook.com/permalink.php?story_fbid=103039851600105&id=100056822710327 +#Subscribed by Sonali Gupta + +import java.io.*; +import java.util.*; +import java.text.*; +import java.math.*; +import java.util.regex.*; + +public class Solution { + public static void main(String[] args) { + Scanner in =new Scanner(System.in); + int n = in.nextInt(); + int d = in.nextInt(); + int[] arr=new int[n]; + + int i, temp, j; + + for(i=0;i0) + { + r=n%10; + sum=(sum*10)+r; + n=n/10; + } + if(temp==sum) + System.out.println("palindrome number "); + else + System.out.println("not palindrome"); + } +} diff --git a/power.java b/power.java new file mode 100644 index 0000000..4c93e98 --- /dev/null +++ b/power.java @@ -0,0 +1,17 @@ +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); + } +} diff --git a/prime.java b/prime.java new file mode 100644 index 0000000..5fdfead --- /dev/null +++ b/prime.java @@ -0,0 +1,24 @@ +//https://www.facebook.com/honey.agrawal.12935/posts/770711770445417 +// Subscribed by 1MS19CS135 Utkarsh +public class Main { + + public static void main(String[] args) { + + int num = 29; + boolean flag = false; + for(int i = 2; i <= num/2; ++i) + { + // condition for nonprime number + if(num % i == 0) + { + flag = true; + break; + } + } + + if (!flag) + System.out.println(num + " is a prime number."); + else + System.out.println(num + " is not a prime number."); + } +} diff --git a/random.java b/random.java new file mode 100644 index 0000000..07ac15c --- /dev/null +++ b/random.java @@ -0,0 +1,28 @@ +// A Java program to demonstrate random number generation +// using java.util.Random; +import java.util.Random; + +public class generateRandom{ + + public static void main(String args[]) + { + // create instance of Random class + Random rand = new Random(); + + // Generate random integers in range 0 to 999 + int rand_int1 = rand.nextInt(1000); + int rand_int2 = rand.nextInt(1000); + + // Print random integers + System.out.println("Random Integers: "+rand_int1); + System.out.println("Random Integers: "+rand_int2); + + // Generate Random doubles + double rand_dub1 = rand.nextDouble(); + double rand_dub2 = rand.nextDouble(); + + // Print random doubles + System.out.println("Random Doubles: "+rand_dub1); + System.out.println("Random Doubles: "+rand_dub2); + } +} diff --git a/rockpaperscissorgame.java b/rockpaperscissorgame.java new file mode 100644 index 0000000..f0762d9 --- /dev/null +++ b/rockpaperscissorgame.java @@ -0,0 +1,86 @@ +#https://www.facebook.com/100028679802914/posts/449717249327598/ +# Subscribed by Nikita Desale +import java.util.Random; +import java.util.Scanner; +public class Application { +public static void main(String[] args) { + System.out.println("Welcome to Rock, Paper, Scissors; Let's Play!"); + Scanner playerName = new Scanner(System.in); + Scanner roundsToWin = new Scanner(System.in); + System.out.println("Please enter your name: "); + String text = playerName.nextLine(); + System.out.println("Hello, " + text + "\nHow many rounds would you like to play?"); + int rounds = roundsToWin.nextInt(); + int value = 0; + int player = 0; + int computer = 0; + do { + // System.out.println("Current rounds played: " + value); + String[] rockPaperScissors = {"Rock", "Paper", "Scissors"}; + Random random = new Random(); + Scanner playerChoice = new Scanner(System.in); + System.out.println("Please enter Rock, Paper or Scissors: \nCapitilization Matters!"); + String choice = playerChoice.nextLine(); + System.out.println(); + int select = random.nextInt(rockPaperScissors.length); + System.out.println("Computer selection: " + rockPaperScissors[select]); + System.out.println("Your selection: " + choice); + System.out.println(); + if (choice.equals(rockPaperScissors[select])) { + System.out.println("It is a Tie"); + } + else { + if(choice.equals("Rock")) { + if(rockPaperScissors[select].equals(rockPaperScissors[1])) { + System.out.println("Paper beats rock."); + computer++; + } + if(rockPaperScissors[select].equals(rockPaperScissors[2])) { + System.out.println("Rock beats scissors."); + player++; + } + } + if(choice.equals("Paper")) { + if(rockPaperScissors[select].equals(rockPaperScissors[0])) { + System.out.println("Paper beats rock."); + player++; + } + if(rockPaperScissors[select].equals(rockPaperScissors[2])) { + System.out.println("Scissors beat paper."); + computer++; + } + } + if(choice.equals("Scissors")) { + if(rockPaperScissors[select].equals(rockPaperScissors[0])) { + System.out.println("Rock beats scissors."); + computer++; + } + if(rockPaperScissors[select].equals(rockPaperScissors[1])) { + System.out.println("Scissors cuts paper."); + player++; + } } } + System.out.println(); + System.out.println(text + ": " + player); + System.out.println("Computer: " + computer); + System.out.println(); + System.out.println(); + try { + Thread.sleep(3000); //1000 milliseconds is one second. + } catch(InterruptedException ex) { + Thread.currentThread().interrupt(); + } + value++; + } + while(value < rounds); + System.out.println("Final Score!"); + System.out.println(text + ": " + player); + System.out.println("Computer:" + computer); + System.out.println(); + if(computer > player) { + System.out.println("You Lose! Computer Wins"); + } + if(player > computer) { + System.out.println("Congratulations " + text + ", You won!"); + } + } +} diff --git a/simpleInterest.java b/simpleInterest.java new file mode 100644 index 0000000..759e596 --- /dev/null +++ b/simpleInterest.java @@ -0,0 +1,13 @@ +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); + } +} diff --git a/stringreverseusingstack.java b/stringreverseusingstack.java new file mode 100644 index 0000000..54aa89e --- /dev/null +++ b/stringreverseusingstack.java @@ -0,0 +1,56 @@ +Live Demo +import java.io.IOException; + +public class StringReverserThroughStack { + private String input; + private String output; + public StringReverserThroughStack(String in) { + input = in; + } + public String doRev() { + int stackSize = input.length(); + Stack theStack = new Stack(stackSize); + + for (int i = 0; i < input.length(); i++) { + char ch = input.charAt(i); + theStack.push(ch); + } + output = ""; + while (!theStack.isEmpty()) { + char ch = theStack.pop(); + output = output + ch; + } + return output; + } + public static void main(String[] args) throws IOException { + String input = "Java Source and Support"; + String output; + StringReverserThroughStack theReverser = + new StringReverserThroughStack(input); + output = theReverser.doRev(); + System.out.println("Reversed: " + output); + } + class Stack { + private int maxSize; + private char[] stackArray; + private int top; + + public Stack(int max) { + maxSize = max; + stackArray = new char[maxSize]; + top = -1; + } + public void push(char j) { + stackArray[++top] = j; + } + public char pop() { + return stackArray[top--]; + } + public char peek() { + return stackArray[top]; + } + public boolean isEmpty() { + return (top == -1); + } + } +} diff --git a/swap.java b/swap.java new file mode 100644 index 0000000..ea081e6 --- /dev/null +++ b/swap.java @@ -0,0 +1,17 @@ +import java.util.Scanner; +class SwapNumbers +{ +public static void main(String args[]) +{ +int z, y, temp; +System.out.println("Enter z and y"); +Scanner sct = new Scanner(System.in); //User inputs two numbers +z = sct.nextInt(); //User inputs two numbers +y = sct.nextInt(); +System.out.println("Before Swapping\nz = "+z+"\ny = "+y); +temp = z; //Swapping is done +z = y; +y = temp; +System.out.println("After Swapping\nz = "+z+"\ny = "+y); +} +} diff --git a/switchcase.java b/switchcase.java new file mode 100644 index 0000000..a6bb998 --- /dev/null +++ b/switchcase.java @@ -0,0 +1,56 @@ +#https://www.facebook.com/lakshay.gulati.5/posts/3439658002768650 +#subscribed by Lakshay + +public class switchcase { + + public static void main(String[] args) { + + System.out.println("Choose an option:-"); + System.out.println("1.To check if the no. is composite"); + System.out.println("2.To find the smallest digit"); + Scanner scan = new Scanner(System.in); + int choice = scan.nextInt(); + int store[]; + + switch(choice){ + case 1: + { + System.out.println("Enter the number"); + int num = scan.nextInt(); + for (int i = 2;i=0;j++){ + if(num==0){ + break;} + store[j]=(num%10); + num=num/10; + + } + Arrays.sort(store); + System.out.println(store[0]); + }break; + default:{ + System.out.println("Enter the Correct choice");}break; + } + + + + + } +} diff --git a/tictactoe.java b/tictactoe.java new file mode 100644 index 0000000..b400b41 --- /dev/null +++ b/tictactoe.java @@ -0,0 +1,173 @@ +// A simple program to demonstrate +// Tic-Tac-Toe Game. +import java.util.*; + +public class GFG { + + static String[] board; + static String turn; + + + // CheckWinner method will + // decide the combination + // of three box given below. + static String checkWinner() + { + for (int a = 0; a < 8; a++) { + String line = null; + + switch (a) { + case 0: + line = board[0] + board[1] + board[2]; + break; + case 1: + line = board[3] + board[4] + board[5]; + break; + case 2: + line = board[6] + board[7] + board[8]; + break; + case 3: + line = board[0] + board[3] + board[6]; + break; + case 4: + line = board[1] + board[4] + board[7]; + break; + case 5: + line = board[2] + board[5] + board[8]; + break; + case 6: + line = board[0] + board[4] + board[8]; + break; + case 7: + line = board[2] + board[4] + board[6]; + break; + } + //For X winner + if (line.equals("XXX")) { + return "X"; + } + + // For O winner + else if (line.equals("OOO")) { + return "O"; + } + } + + for (int a = 0; a < 9; a++) { + if (Arrays.asList(board).contains( + String.valueOf(a + 1))) { + break; + } + else if (a == 8) { + return "draw"; + } + } + + // To enter the X Or O at the exact place on board. + System.out.println( + turn + "'s turn; enter a slot number to place " + + turn + " in:"); + return null; + } + + // To print out the board. + /* |---|---|---| + | 1 | 2 | 3 | + |-----------| + | 4 | 5 | 6 | + |-----------| + | 7 | 8 | 9 | + |---|---|---|*/ + + static void printBoard() + { + System.out.println("|---|---|---|"); + System.out.println("| " + board[0] + " | " + + board[1] + " | " + board[2] + + " |"); + System.out.println("|-----------|"); + System.out.println("| " + board[3] + " | " + + board[4] + " | " + board[5] + + " |"); + System.out.println("|-----------|"); + System.out.println("| " + board[6] + " | " + + board[7] + " | " + board[8] + + " |"); + System.out.println("|---|---|---|"); + } + + public static void main(String[] args) + { + Scanner in = new Scanner(System.in); + board = new String[9]; + turn = "X"; + String winner = null; + + for (int a = 0; a < 9; a++) { + board[a] = String.valueOf(a + 1); + } + + System.out.println("Welcome to 3x3 Tic Tac Toe."); + printBoard(); + + System.out.println( + "X will play first. Enter a slot number to place X in:"); + + while (winner == null) { + int numInput; + + // Exception handling. + // numInput will take input from user like from 1 to 9. + // If it is not in range from 1 to 9. + // then it will show you an error "Invalid input." + try { + numInput = in.nextInt(); + if (!(numInput > 0 && numInput <= 9)) { + System.out.println( + "Invalid input; re-enter slot number:"); + continue; + } + } + catch (InputMismatchException e) { + System.out.println( + "Invalid input; re-enter slot number:"); + continue; + } + + // This game has two player x and O. + // Here is the logic to decide the turn. + if (board[numInput - 1].equals( + String.valueOf(numInput))) { + board[numInput - 1] = turn; + + if (turn.equals("X")) { + turn = "O"; + } + else { + turn = "X"; + } + + printBoard(); + winner = checkWinner(); + } + else { + System.out.println( + "Slot already taken; re-enter slot number:"); + } + } + + // If no one win or lose from both player x and O. + // then here is the logic to print "draw". + if (winner.equalsIgnoreCase("draw")) { + System.out.println( + "It's a draw! Thanks for playing."); + } + + // For winner -to display Congratulations! message. + else { + System.out.println( + "Congratulations! " + winner + + "'s have won! Thanks for playing."); + } + } +} diff --git a/wordshash.java b/wordshash.java new file mode 100644 index 0000000..2a47064 --- /dev/null +++ b/wordshash.java @@ -0,0 +1,22 @@ +import java.util.HashMap; + +public class FinalCountWords { + + public static void main(String[] args) { + String str = "This this is is done by Saket Saket"; + String[] split = str.split(" "); + + HashMap map = new HashMap(); + for (int i=0; i