commit 430dfcee8b6791418ae038eb517251b87326e591 Author: psavarmattas Date: Wed Apr 14 16:00:09 2021 +0530 first commit diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..949f42e --- /dev/null +++ b/Calculator.java @@ -0,0 +1,44 @@ +// Program that implements the arithmetic operation (Calculator) using switch statement. + +import java.util.Scanner; + +public class Calculator { + public static void main(String[] args) { + + char operator; + Double number1, number2, result; + Scanner input = new Scanner(System.in); + System.out.println("Type the operator you want to use: +, -, *, or /"); + operator = input.next().charAt(0); + System.out.println("Enter first number"); + number1 = input.nextDouble(); + System.out.println("Enter second number"); + number2 = input.nextDouble(); + switch (operator) { + case '+': + result = number1 + number2; + System.out.println(number1 + " + " + number2 + " = " + result); + break; + + case '-': + result = number1 - number2; + System.out.println(number1 + " - " + number2 + " = " + result); + break; + + case '*': + result = number1 * number2; + System.out.println(number1 + " * " + number2 + " = " + result); + break; + + case '/': + result = number1 / number2; + System.out.println(number1 + " / " + number2 + " = " + result); + break; + + default: + System.out.println("Invalid operator!"); + break; + } + input.close(); + } +} diff --git a/EvenOdd.java b/EvenOdd.java new file mode 100644 index 0000000..478f8b2 --- /dev/null +++ b/EvenOdd.java @@ -0,0 +1,12 @@ +// Program to find out the number entered is Even/Odd (Static Method). + +public class EvenOdd { + public static void main(String[] args) { + int num= 25; + System.out.println("You enter number: " + num); + if(num % 2 == 0) + System.out.println(num + " is even"); + else + System.out.println(num + " is odd"); + } +} diff --git a/EvenOddDynamic.java b/EvenOddDynamic.java new file mode 100644 index 0000000..5cd463b --- /dev/null +++ b/EvenOddDynamic.java @@ -0,0 +1,18 @@ +// Program to find out the number entered is Even/Odd (Dynamic Method). + +import java.util.Scanner; + +public class EvenOddDynamic { + public static void main(String[] args) { + int num; + System.out.println("Enter an Integer:"); + Scanner input = new Scanner(System.in); + num = input.nextInt(); + if (num % 2 == 0) { + System.out.println("Entered number is even."); + } + else { + System.out.println("Entered number is odd."); + } + } +} diff --git a/FibonacciSeries.java b/FibonacciSeries.java new file mode 100644 index 0000000..0097af7 --- /dev/null +++ b/FibonacciSeries.java @@ -0,0 +1,21 @@ +// Program to generate fibonacci series upto n value (Dynamic Method). + +import java.util.Scanner; + +public class FibonacciSeries { + public static void main(String args[]) { + Scanner sc = new Scanner(System.in); + int sum = 0, n; + int a = 0; + int b = 1; + System.out.println("Enter the nth value: "); + n = sc.nextInt(); + System.out.println("Fibonacci series: "); + while(sum <= n) { + System.out.print(sum + " "); + a = b; + b = sum; + sum = a + b; + } + } +} diff --git a/FibonacciSeriesStatic.java b/FibonacciSeriesStatic.java new file mode 100644 index 0000000..4ebe49f --- /dev/null +++ b/FibonacciSeriesStatic.java @@ -0,0 +1,14 @@ +// Program to generate fibonacci series upto n value (Static Method). + +public class FibonacciSeriesStatic { + public static void main(String args[]) { + int n1=0,n2=1,n3,i,count=10; + System.out.print(n1+" "+n2); + for(i=2;i= num2 && num1 >= num3) + System.out.println(num1+" is the largest Number"); + + else if (num2 >= num1 && num2 >= num3) + System.out.println(num2+" is the largest Number"); + else + System.out.println(num3+" is the largest Number"); + } +} diff --git a/PrimeNo.java b/PrimeNo.java new file mode 100644 index 0000000..0430bd8 --- /dev/null +++ b/PrimeNo.java @@ -0,0 +1,34 @@ +// Program to display first n Prime number (Dynamic Method). + +import java.util.Scanner; + +public class PrimeNo { + public static void main(String[] args) { + int n; + int status = 1; + int num = 3; + Scanner scanner = new Scanner(System.in); + System.out.println("Enter the number of Prime numbers to be printed:"); + n = scanner.nextInt(); + if (n >= 1) { + System.out.println("First "+n+" prime numbers are:"); + 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++; + } + } +} \ No newline at end of file diff --git a/PrimeNoStatic.java b/PrimeNoStatic.java new file mode 100644 index 0000000..753fa8e --- /dev/null +++ b/PrimeNoStatic.java @@ -0,0 +1,25 @@ +// Program to display first n Prime number (Static Method). + +class PrimeNoStatic { + public static void main(String args[]) { + int n; + int status = 1; + int num = 3; + System.out.println("First 100 prime numbers are:"); + System.out.println(2); + for ( int i = 2 ; i <=100 ; ) { + 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++; + } + } +} \ No newline at end of file diff --git a/PrimeOrNot.java b/PrimeOrNot.java new file mode 100644 index 0000000..c492e7a --- /dev/null +++ b/PrimeOrNot.java @@ -0,0 +1,26 @@ +// Program to find out whether the number is Prime or Not (Dynamic Method); + +import java.util.Scanner; + +public class PrimeOrNot { + public static void main(String[] args) { + int num, i = 2; + System.out.println("Enter the number: "); + Scanner s = new Scanner(System.in); + num = s.nextInt(); + boolean flag = false; + while (i <= num / 2) { + if (num % i == 0) { + flag = true; + break; + } + ++i; + } + if (!flag) { + System.out.println(num + " is a prime number"); + } + else { + System.out.println(num + " is not a prime number"); + } + } +} \ No newline at end of file diff --git a/PrimeOrNotStatic.java b/PrimeOrNotStatic.java new file mode 100644 index 0000000..818f717 --- /dev/null +++ b/PrimeOrNotStatic.java @@ -0,0 +1,19 @@ +// Program to find out the prime or not (Static Method). + +public class PrimeOrNotStatic { + public static void main(String[] args) { + int num = 29; + boolean flag = false; + for (int i = 2; i <= num / 2; ++i) { + 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/README.MD b/README.MD new file mode 100644 index 0000000..ec768bd --- /dev/null +++ b/README.MD @@ -0,0 +1,63 @@ +# JAVA Programs for everybody to try! + +_`Last Updated: April 14' 2021`_ + +## Introduction: + +Here are programs that every budding programmer who is learning to code in Java should start with. +These programs are a compilation of many different types of programs and levels of programming you should try. +If you want to start with levels of coding in mind use the table (serial) to refer which program is of which category. +If you want in which serial you should code rather than the levels then refer to table (levels) for a better understanding. + +## Pre-Requisites: + +1. A coding IDE or just use the CMD/Terminal on your Windows Pc/Mac respectively (I used VS Code for this). + +2. Check that Java is properly installedin your system and the Path to Java is integratively set in your system. + +3. Start by trying to create the program by yourself if you encounter any problems refer to the code in the program file, +if the problem still persist then create a issue or use Google/Stack Overlflow for help. + +4. Done! You have successfully created the program in Java by your own. + +## Serial: + +|Serial No.|Program Name|Description of the program| +|----------|------------|--------------------------| +|1 | HelloWorld.java | Program to print out "Hello World" | +|2 | EvenOdd.java | Program to find out the number entered is Even/Odd (Static Method). | +|3 | EvenOddDynamic.java | Program to find out the number entered is Even/Odd (Dynamic Method). | +|4 | LargestNumberStatic.java | Program to find out the largest number (Static Method). | +|5 | LargestNumber.java | Program to find out the largest number (Dynamic Method). | +|6 | PrimeOrNotStatic.java | Program to find out the prime or not (Static Method). | +|7 | PrimeOrNot.java | Program to find out the prime or not (Dynamic Method). | +|8 | PrimeNoStatic.java | Program to display first n Prime number (Static Method). | +|9 | PrimeNo.java | Program to display first n Prime number (Dynamic Method). | +|10 | FibonacciSeriesStatic.java | Program to generate fibonacci series upto n value (Static Method). | +|11 | FibonacciSeries.java | Program to generate fibonacci series upto n value (Dynamic Method). | +|12 | RootsOfQuadracticEquationStatic.java | Program to find the root of quadratic equation (Static Method). | +|13 | RootsOfQuadracticEquation.java | Program to find the root of quadratic equation (Dynamic Method). | + + +## Levels: + +|Level No.|Program Name|Description of the program| +|----------|------------|--------------------------| +|1 | HelloWorld.java | Program to print out "Hello World" | +|1 | EvenOdd.java | Program to find out the number entered is Even/Odd (Static Method). | +|1 | LargestNumberStatic.java | Program to find out the largest number (Static Method). | +|1 | PrimeOrNotStatic.java | Program to find out the prime or not (Static Method). | +|1 | PrimeNoStatic.java | Program to display first n Prime number (Static Method). | +|1 | FibonacciSeriesStatic.java | Program to generate fibonacci series upto n value (Static Method). | +|1 | RootsOfQuadracticEquationStatic.java | Program to find the root of quadratic equation (Static Method). | +|2 | EvenOddDynamic.java | Program to find out the number entered is Even/Odd (Dynamic Method). | +|2 | LargestNumber.java | Program to find out the largest number (Dynamic Method). | +|2 | PrimeOrNot.java | Program to find out the prime or not (Dynamic Method). | +|2 | PrimeNo.java | Program to display first n Prime number (Dynamic Method). | +|2 | FibonacciSeries.java | Program to generate fibonacci series upto n value (Dynamic Method). | +|2 | RootsOfQuadracticEquation.java | Program to find the root of quadratic equation (Dynamic Method). | + +## [License](): + +Copyright (c) 2021 PSMForums. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. + diff --git a/RootsOfQuadraticEquation.java b/RootsOfQuadraticEquation.java new file mode 100644 index 0000000..e7155ba --- /dev/null +++ b/RootsOfQuadraticEquation.java @@ -0,0 +1,25 @@ +// Program to find the root of quadratic equation (Dynamic Method). + +import java.util.Scanner; + +public class RootsOfQuadraticEquation { + public static void main(String args[]){ + double secondRoot = 0, firstRoot = 0; + Scanner sc = new Scanner(System.in); + System.out.println("Enter the value of a :"); + double a = sc.nextDouble(); + System.out.println("Enter the value of b :"); + double b = sc.nextDouble(); + System.out.println("Enter the value of c :"); + double c = sc.nextDouble(); + double determinant = (b*b)-(4*a*c); + double sqrt = Math.sqrt(determinant); + if(determinant>0){ + firstRoot = (-b + sqrt)/(2*a); + secondRoot = (-b - sqrt)/(2*a); + System.out.println("Roots are :: "+ firstRoot +" and "+secondRoot); + }else if(determinant == 0){ + System.out.println("Root is :: "+(-b + sqrt)/(2*a)); + } + } +} diff --git a/RootsOfQuadraticEquationStatic.java b/RootsOfQuadraticEquationStatic.java new file mode 100644 index 0000000..4fc92d8 --- /dev/null +++ b/RootsOfQuadraticEquationStatic.java @@ -0,0 +1,24 @@ +// Program to find the root of quadratic equation (Static Method). + +public class RootsOfQuadraticEquationStatic { + public static void main(String[] args) { + double a = 2.3, b = 4, c = 5.6; + double root1, root2; + double determinant = b * b - 4 * a * c; + if (determinant > 0) { + root1 = (-b + Math.sqrt(determinant)) / (2 * a); + root2 = (-b - Math.sqrt(determinant)) / (2 * a); + System.out.format("root1 = %.2f and root2 = %.2f", root1, root2); + } + else if (determinant == 0) { + root1 = root2 = -b / (2 * a); + System.out.format("root1 = root2 = %.2f;", root1); + } + else { + double real = -b / (2 * a); + double imaginary = Math.sqrt(-determinant) / (2 * a); + System.out.format("root1 = %.2f+%.2fi", real, imaginary); + System.out.format("\nroot2 = %.2f-%.2fi", real, imaginary); + } + } +} \ No newline at end of file