Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
      • Category 1
      • Category 2
      • Category 3
      • Category 4
      • Category 5
    • Sub Menu 3
    • Sub Menu 4
  • Contact Us

Write a java program to find all roots of a quadratic equation

The standard form of an equation is:
ax2+bx+c=0

Here, a, b and c are real numbers and a can't be equal to 0.

We can calculate the root of a quadratic by using the formula

x=−b±b2−4ac−−−−−−−√2a

The ± sign indicates that there will be two roots:

root1=−b +b2−4ac−−−−−−−√2a

root2=−b -b2−4ac−−−−−−−√2a

The term b2-4ac is known as the determinant of a quadratic equation. It specifies the nature of roots. That is,

  • if determinant > 0, roots are real and different
  • if determinant < 0, roots are complex and different
  • if determinant == 0, roots are real and equal

  • Program:

public class Main {

  public static void main(String[] args) {

    // value a, b, and c
    double a = 4.6, b = 2, c = 4.2;
    double root1, root2;

    // calculate the determinant (b2 - 4ac)
    double determinant = b * b - 4 * a * c;

    // check if determinant is greater than 0
    if (determinant > 0) {

      // two real and distinct roots
      root1 = (-b + Math.sqrt(determinant)) / (2 * a);
      root2 = (-b - Math.sqrt(determinant)) / (2 * a);

      System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
    }

    // check if determinant is equal to 0
    else if (determinant == 0) {

      // two real and equal roots
      // determinant is equal to 0
      // so -b + 0 == -b
      root1 = root2 = -b / (2 * a);
      System.out.format("root1 = root2 = %.2f;", root1);
    }

    // if determinant is less than zero
    else {

      // roots are complex number and distinct
      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);
    }
  }
}

  • Output:

root1 = -0.22+0.93i
root2 = -0.22-0.93i

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here

Write a java program to find the largest among three numbers

In this program, we will find the largest among three numbers using if else statements.

Let understand about Ternary operation,

It is a part of the java conditional statement. It contains three operands. It evaluates a Boolen expression and assigned a value based on the result. We can use it in place of the if-else statement. 

The syntax of the ternary operator is Variable name = (expression) ? value if true:value if false

If above expression returns true the value before the colon is assigned to the variable Variable name, else the value after the colon is assigned to the variable.

In the blow program, three numbers -6.6, 1.3 and 3.5 are stored in variables n1, n2 and n3 respectively.

For find the largest, the following conditions are checked using if else statements

  • If n1 is greater or equals to both n2 and n3, n1 is the largest.
  • If n2 is greater or equals to both n1 and n3, n2 is the greatest.
  • Else, n3 is the greatest.

The greatest number can also be found using a nested if..else statement.

  • Program:

public class Largest {

    public static void main(String[] args) {

        double n1 = -6.6, n2 = 1.3, n3 = 3.5;

        if( n1 >= n2 && n1 >= n3)
            System.out.println(n1 + " is the largest number.");

        else if (n2 >= n1 && n2 >= n3)
            System.out.println(n2 + " is the largest number.");

        else
            System.out.println(n3 + " is the largest number.");
    }
}

  • Output:

3.5 is the largest number.

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here
Java program to check whether an alphabet is vowel or consonant Here

Write a java program to check whether an alphabet is vowel or consonant

In this program, java program to identify whether the given character is a vowel or not using if else statement. Whit the help of the below program, you will get to know how to write and print whether the given number is a vowel.

What are the Vowels?

A: def: A speech sound which is produced with the vibration of vocal chords without audible friction, which is being blocked by teeth, tongue or lips :P. We know that there are five vowels: a, e, i, o, u or A, I, E, O, U.

What are the consonants?

Apart from A, E, I, O, U rest of the alphabets are consonants. That's it.
Basically, we can write the code in two different ways, using if statement or if-else statements or java switch case statement.

Here, o is stored in a char variable ch. In java, you use double quotes (" ") for strings and single quotes (" ") for characters.

Now, to check whether ch is vowel or not, we check if ch is any of: (a, e, i, o, u). This is done using a simple if...else statement.

We can also check for vowel or consonant using a switch statement in java.

  • Program:

public class VowelConsonant {

    public static void main(String[] args) {

        char ch = 'o';

        if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' )
            System.out.println(ch + " is vowel");
        else
            System.out.println(ch + " is consonant");

    }
}                                     

  • Output:

o is vowel

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here
Java program to check whether a given number is even or odd Here

Write a java program to check whether a number is even or odd

In this program, enter any integer number as an input. Now we check its remainder with modulo operator by two. If remainder is zero the given. If remainder is 1 then the given number is odd. Hence, we show output according to the remainder.

Here is the source code of the java program to check if a given integer is odd or even. The java program is successfully compiled and run on a windows system. The program output is also shown below.

  • Program:

import java.util.Scanner;

public class EvenOdd {

    public static void main(String[] args) {

        Scanner reader = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = reader.nextInt();

        if(num % 2 == 0)
            System.out.println(num + " is even");
        else
            System.out.println(num + " is odd");
    }
}

  • Output:

Enter a number: 16
16 is even

--- Second Example ---

Enter a number: 27
27 is odd

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here
Java program to swap two numbers Here

Write a java program to swap two numbers

In this program, you will learn the technique of swapping two numbers using temporary variable in java programming language.  

Exchanging the values stored in two variables is known as swapping. There are multiple ways to swap the values of two variables. It can be swapped using a temporary variable or simple or simple mathematical operations such as addition and subtraction or multiplication and division or bitwise XOR can also be used to perform the swap operation.

In the below program, two numbers 2.60f and 4.44f which are to be swapped are stored in variables: first and second respectively.

The variable is printed before swapping using println() to see the results clearly after swapping is done. Below given steps are followed swapping process. After completing swapping process and the variables are printed on the screen.

Remember, the only use of temporary is hold the value of first before swapping. You can also swap the numbers without using temporary.

  • Program:

public class SwapNumbers {

    public static void main(String[] args) {

        float first = 2.60f, sec = 4.44f;

        System.out.println("--Before swapping the numbers--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + sec);

        // Value of first is assigned to temporary
        float temporary = first;

        // Value of sec is assigned to first
        first = sec;

        // Value of temporary (which contains the initial value of first) is assigned to sec 
        sec = temporary;

        System.out.println("--After swapping the numbers--");
        System.out.println("First number = " + first);
        System.out.println("Second number = " + sec);
    }
}

  • Output:

--Before swapping the numbers--
First number = 2.6
Second number = 4.44
--After swapping the numbers--
First number = 4.44
Second number = 2.6

  • Step:

Below are the simple steps we follow:
  1. Assign first to a temporary variable: temporary = first
  2. Assign second to first: first = second
  3. Assign temp to second: second = temporary

Let us understand with simple example,

first = 111, second = 222
After line 1: temporary = first
temporary = 111
After line 2: first = second
first = 222
After line 3: second = temporary
second = 222

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here
Java program to compute quotient and remainder Here

Write a java program to compute quotient and remainder

In this program, the quotient and remainder find in java programming language.

The different parts of division are dividend, divisor, quotient and remainder. Quotient can be defined as the resultant of the division. Formula for quotient is Quotient = Dividend/Divisor. In division, a number is divided by another number to get the output in the form of another number. The dividend is the number that is getting divided, and the divisor is the number that divides the given number. In fraction, the numerator is dividend, denominator is divisor, and when numerator is divided by the denominator, the result will be quotient. For, instance, suppose we have to divide 20(dividend) by 4(divisor); we will get 5, which is the quotient.

Remainder is defined as a value that is left after division. As an instance, if we divide 25 with 4, we will get 1 as a remainder. If the number divides another number completely, the remainder will be 0.
Remainder is always smaller than the divisor, if it is large, that means the division is not completed. But remainder can be less or greater than the quotient; for instance, if we divide 41 by 7, we will get 5 as a quotient, and 6 as a remainder which is less than divisor greater than the quotient. we know the method of Division = Divisor x Quotient + Remainder. so, the formula for remainder will be Remainder = Dividend - (Divisor x Quotient)

Here, we have created two variables dividend and divisor. we are calculating the quotient and remainder by dividing 55 by 6.
To find the quotient, we have used the / operator, we have divided dividend (55) by divisor (4). Since both dividend and divisor are integers, the result will also be integer.
55 / 6 // result 8(integer)
Likewise, to find the remainder we use the % operator. Here, the dividend is divided by the divisor and the remainder is returned by the % oprtator.
55 % 6 // result 1
finally, quotient and remainder are printed on the screen using println() function.

  • Program:

public class QuotientRemainder {

  public static void main(String[] args) {

    int dividend = 55, divisor = 6;

    int quotient = dividend / divisor;
    int remainder = dividend % divisor;

    System.out.println("Quotient = " + quotient);
    System.out.println("Remainder = " + remainder);
  }
}

  • Output:

Quotient = 9
Remainder = 1

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Java program to find ASCII value of a character Here

Write a java program to find ASCII value of a character

In this program, find and display the ASCII value of a character in java using typecasting and normal variable assignment operations.

Here, character n is stored in a char variable ch. Like, single quotes (' ') are used to declare characters, we use double quotes (" ") are used to declare strings.

Now, to find the ASCII value of ch, we just assign ch to an int variable ASCII. Internally, Java converts the characters value to an ASCII value.

We can also cast the character ch to an integer using int. In simple terms, casting is converting variable from one type to another, here char variable ch is converted to an int variable castAscii

Last, we print the ascii value using the println() function.

  • Program:


public class AsciiValue {

    public static void main(String[] args) {

        char ch = 'n';
        int ASCII = ch;
        // You can also cast char to int
        int castAscii = (int) ch;

        System.out.println("The ASCII value of " + ch + " is: " + ASCII);
        System.out.println("The ASCII value of " + ch + " is: " + castAscii);
    }
}

  • Output:


The ASCII value of a is: 110
The ASCII value of a is: 110

  • Visit:

Java program to print an Integer Here
Java program to add two integers Here
Newer Posts Older Posts Home

ABOUT

I could look back at my life and get a good story out of it. It's a picture of somebody trying to figure things out.

SUBSCRIBE & FOLLOW

POPULAR POSTS

  • Java program to check whether a given number is even or odd
  • Java program to find factorial of a number
  • Write a java program to multiply two floating point numbers
  • Java program to find the largest among three numbers
  • java program to calculate the sum of natural numbers

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • January 20231
  • December 20223
  • November 202215
  • August 20222

Report Abuse

Copyright

  • Home
  • About US
  • Privacy Policy
  • Disclaimer

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes