Programmator
Programmator
  • Home
  • Download
  • Social
    • YouTube
    • Instagram
    • Facebook
    • Telegram
  • Features
    • C/C++
    • Java
  • Contact Us

Java program to multiply two floating point numbers

In the below program, we have two floating - point numbers 3.4f and 1.5f stored in variables first and second respectively.

Notice, we have used f after the numbers. This ensures the numbers are float, otherwise they will be assigned typed double.

first and second are then multiplied using the * operator and the result is stored in a new float variable product.

Finally, the result product is printed on the screen using println() function.

  • Program:

public class MultiplyTwoNumbers {

    public static void main(String[] args) {

        float first = 3.4f;
        float second = 1.5f;

        float product = first * second;

        System.out.println("The product is: " + product);
    }
}

  • Output:

The product is: 5.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
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 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here
Java program to check leap year Here

Write a java program to check leap year

Leap year contains 366 days, which comes once every four years.
Every leap year corresponds to these facts:

  • A century year is a year ending with 00.  A century year is a leap year only if it is divisible by 400.
  • A leap year (except a century year) can be identified if it is exactly divisible by 4.
  • A century year should be divisible by 4 and 100 both.
  • A non-century year should be divisible only by 4.

Let's find out whether a year is a leap year or not

  • Program:

public class Main {

  public static void main(String[] args) {

    // year to be checked
    int year = 2011;
    boolean leap = false;

    // if the year is divided by 4
    if (year % 4 == 0) {

      // if the year is century
      if (year % 100 == 0) {

        // if year is divided by 400
        // then it is a leap year
        if (year % 400 == 0)
          leap = true;
        else
          leap = false;
      }
      
      // if the year is not century
      else
        leap = true;
    }
    
    else
      leap = false;

    if (leap)
      System.out.println(year + " is a leap year.");
    else
      System.out.println(year + " is not a leap year.");
  }
}

  • Output:

2011 is not a leap year

--- Another example ---

2012 is a leap year

  • 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 
Java program to find the largest among three numbers Here
Java program to find all roots of a quadratic equation Here

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 
Java program to find the largest among three numbers 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
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

  • Write a C program to enter a distance in to kilometer and convert it in to meter, feet, inches and centimeter
  • Write a C program to computer Fahrenheit from centigrade ( f=1.8*c+32 )
  • Write a C program to interchange two numbers
  • Write a C program to read and store the roll no and marks of 20 students using array
  • Write a C program to evaluate the series sum=1-x+x^2/2!-x^3/3!+x^4/4!......-x^9/9!

Advertisement

👆 Shopping 👆

Powered by Blogger

Archive

  • February 20241
  • January 20242
  • November 20234
  • October 20236
  • September 20236
  • August 20235
  • April 20231
  • March 20231
  • October 20221
  • August 20223
  • July 20222
  • May 20223
  • April 20222
  • March 20221
  • January 20221
  • December 20216
  • November 20214
  • October 20211
  • September 20216
  • August 202127
  • July 20216

Report Abuse

Copyright

  • Home
  • About us
  • Privacy Policy
  • Disclaimer
  • Contact Us

About Me

Nilesh Patel
View my complete profile

Copyright © Programmator. Designed by OddThemes