Find Roots of a quadratic equation
import java.util.Scanner;
public class QuadraticEquation {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the coefficient a: ");
double a = scanner.nextDouble();
System.out.print("Enter the coefficient b: ");
double b = scanner.nextDouble();
System.out.print("Enter the coefficient c: ");
double c = scanner.nextDouble();
double discriminant = b * b - 4 * a * c;
double root1, root2;
if (discriminant > 0) {
root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
System.out.println("The roots are real and different.");
System.out.println("Root 1: " + root1);
System.out.println("Root 2: " + root2);
} else if (discriminant == 0) {
root1 = root2 = -b / (2 * a);
System.out.println("The roots are real and the same.");
System.out.println("Root 1 = Root 2: " + root1);
}
else {
double realPart = -b / (2 * a);
double imaginaryPart = Math.sqrt(-discriminant) / (2 * a);
System.out.println("The roots are complex and different.");
System.out.println("Root 1: " + realPart + " + " + imaginaryPart + "i");
System.out.println("Root 2: " + realPart + " - " + imaginaryPart + "i");
}
scanner.close();
}
}
**********************************************************
Learn switch case
import java.util.Scanner;
public class CaseStatementExample {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Choose an option:");
System.out.println("1. Say Hello");
System.out.println("2. Display Date");
System.out.println("3. Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.println("Hello!");
break;
case 2:
java.util.Date date = new java.util.Date();
System.out.println("Current date and time: " + date.toString());
break;
case 3:
System.out.println("Exiting the program...");
break;
default:
System.out.println("Invalid choice! Please choose a valid option.");
break;
}
scanner.close();
}
}
*****************************************************
Check for Prime
import java.util.Scanner;
public class PrimeChecker {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.print("Enter a number: ");int number = scanner.nextInt();if (isPrime(number)) {System.out.println(number + " is a prime number.");} else {System.out.println(number + " is not a prime number.");}scanner.close();}public static boolean isPrime(int num) {if (num <= 1) {return false;}for (int i = 2; i <= Math.sqrt(num); i++) {if (num % i == 0) { return false;}}return true;}}***********************************************
Fibonacci Series Using Recursion
import java.util.Scanner;
public class FibonacciSeries {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of terms: ");
int terms = scanner.nextInt();
System.out.println("Fibonacci Series up to " + terms + " terms:");
for (int i = 0; i < terms; i++) {
System.out.print(fibonacci(i) + " ");
}
scanner.close();
}
public static int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
****************************************************
Prime number <1000 using nested loops
public class PrimeNumbers {
public static void main(String[] args) {
System.out.println("Prime numbers less than 1000:");
for (int num = 2; num < 1000; num++) {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(num + " ");
}
}
}
}
*************************************************************
gcd of two numbers using Euclid algorithm
import java.util.Scanner;
public class GCD {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
int gcd = findGCD(num1, num2);
System.out.println("The GCD of " + num1 + " and " + num2 + " is: " + gcd);
scanner.close();
}
public static int findGCD(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}
***************************************************************************
Sum of digits, reverse and palindrome check
import java.util.Scanner;
public class DigitBasedOperations {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int number = scanner.nextInt();
int sumOfDigits = calculateSumOfDigits(number);
int reversedNumber = reverseNumber(number);
boolean isPalindrome = checkPalindrome(number);
System.out.println("Sum of digits: " + sumOfDigits);
System.out.println("Reversed number: " + reversedNumber);
System.out.println("Is palindrome: " + isPalindrome);
scanner.close();
}
public static int calculateSumOfDigits(int num) {
int sum = 0;
while (num != 0) {
sum += num % 10;
num /= 10;
}
return sum;
}
public static int reverseNumber(int num) {
int reversed = 0;
while (num != 0) {
int digit = num % 10;
reversed = reversed * 10 + digit;
num /= 10;
}
return reversed;
}
public static boolean checkPalindrome(int num) {
return num == reverseNumber(num);
}
}
Comments
Post a Comment