Found 7442 Articles for Java

Java program to accept an integer from user and print it

Ankith Reddy
Updated on 18-Oct-2024 11:56:22

2K+ Views

In this article, we will prompt the user to input an integer, and the entered value will be displayed back to them. To achieve this, we will use the Scanner class from java.util package, which allows us to read user input. Specifically, the nextInt() method of the Scanner class will be used to read the integer input from the user.Problem Statement Write a program in Java to accept an integer from the user and print itΒ βˆ’ Input Enter an integer: 2 Output Given integer is :: 2 Steps to accept an integer from the user and print it Following are ... Read More

Java program to accept an integer from user and print it

Ankith Reddy
Updated on 18-Oct-2024 11:56:22

2K+ Views

In this article, we will prompt the user to input an integer, and the entered value will be displayed back to them. To achieve this, we will use the Scanner class from java.util package, which allows us to read user input. Specifically, the nextInt() method of the Scanner class will be used to read the integer input from the user.Problem Statement Write a program in Java to accept an integer from the user and print itΒ βˆ’ Input Enter an integer: 2 Output Given integer is :: 2 Steps to accept an integer from the user and print it Following are ... Read More

Java program to find the roots of a quadratic equation

Lakshmi Srinivas
Updated on 21-Oct-2024 17:40:26

14K+ Views

In this article, we will learn to find the roots of a quadratic equation using Java. The roots of a quadratic equation are determined by the following formula: $$x = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}$$ Here we will take input values for the coefficients π‘Ž, 𝑏, and 𝑐 of the quadratic equation π‘Ž π‘₯ 2 + 𝑏 π‘₯ + ... Read More

Java program to find the roots of a quadratic equation

Lakshmi Srinivas
Updated on 21-Oct-2024 17:40:26

14K+ Views

In this article, we will learn to find the roots of a quadratic equation using Java. The roots of a quadratic equation are determined by the following formula: $$x = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}$$ Here we will take input values for the coefficients π‘Ž, 𝑏, and 𝑐 of the quadratic equation π‘Ž π‘₯ 2 + 𝑏 π‘₯ + ... Read More

Java program to round a number

George John
Updated on 13-Mar-2020 10:43:07

686 Views

The java.lang.Math.round(float a) returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. Special cases βˆ’If the argument is NaN, the result is 0.If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.Exampleimport java.util.Scanner; public class RoundingDecimalPlaces ... Read More

Java program to round a number

George John
Updated on 13-Mar-2020 10:43:07

686 Views

The java.lang.Math.round(float a) returns the closest int to the argument. The result is rounded to an integer by adding 1/2, taking the floor of the result, and casting the result to type int. Special cases βˆ’If the argument is NaN, the result is 0.If the argument is negative infinity or any value less than or equal to the value of Integer.MIN_VALUE, the result is equal to the value of Integer.MIN_VALUE.If the argument is positive infinity or any value greater than or equal to the value of Integer.MAX_VALUE, the result is equal to the value of Integer.MAX_VALUE.Exampleimport java.util.Scanner; public class RoundingDecimalPlaces ... Read More

Java program to swap two integers

Chandu yadav
Updated on 16-Aug-2024 23:24:32

3K+ Views

In this article, we will learn to swap two numbers using Java. We will use the Scanner class to get the user input and perform the swapping temporary variable. Read integers from users using the nextInt() method of the Scanner class. Steps to swap two integers Following are the steps to swap two numbers in Javaβˆ’ Import the Scanner class. Create a variable (temp), and initialize it with 0. Assign 1st number to temp. Assign the 2nd number to the 1st number. ... Read More

Java program to swap two integers

Chandu yadav
Updated on 16-Aug-2024 23:24:32

3K+ Views

In this article, we will learn to swap two numbers using Java. We will use the Scanner class to get the user input and perform the swapping temporary variable. Read integers from users using the nextInt() method of the Scanner class. Steps to swap two integers Following are the steps to swap two numbers in Javaβˆ’ Import the Scanner class. Create a variable (temp), and initialize it with 0. Assign 1st number to temp. Assign the 2nd number to the 1st number. ... Read More

Java program to calculate the power of a number

Lakshmi Srinivas
Updated on 02-Aug-2024 18:16:16

3K+ Views

Read the base and exponent values from the user. Multiply the base number by itself and multiply the resultant with base (again) repeat this n times where n is the exponent value. 2 ^ 5 = 2 X 2 X 2 X 2 X 2 (5 times) Problem Statement Given a number, write a program in Java to calculate the power of the number βˆ’ Input Enter the base number :: 12 Enter the exponent number :: 2 Output Result of 12 power 2 is 144 Steps to calculate the power of a number Import ... Read More

Java program to calculate the power of a number

Lakshmi Srinivas
Updated on 02-Aug-2024 18:16:16

3K+ Views

Read the base and exponent values from the user. Multiply the base number by itself and multiply the resultant with base (again) repeat this n times where n is the exponent value. 2 ^ 5 = 2 X 2 X 2 X 2 X 2 (5 times) Problem Statement Given a number, write a program in Java to calculate the power of the number βˆ’ Input Enter the base number :: 12 Enter the exponent number :: 2 Output Result of 12 power 2 is 144 Steps to calculate the power of a number Import ... Read More

Advertisements