Found 9150 Articles for Object Oriented Programming

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 round a number

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

689 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 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 print a multiplication table for any number

Shriansh Kumar
Updated on 01-Aug-2024 10:58:04

2K+ Views

For a given integer number, write a Java program to print its multiplication table. In mathematics, a multiplication table shows the product of two numbers. Printing Multiplication Table using for Loop In Java, a for loop is used when a specific block of code needs to be executed multiple times. Here, we will run this loop 10 times and increment the loop variable by one with each iteration. During this iteration, multiply the given integer value with the current value of the loop variable to get the multiplication table. Example Following is a Java program which prints the multiplication ... Read More

Java program to find the LCM of two numbers

karthikeya Boyini
Updated on 23-Oct-2024 17:35:36

8K+ Views

In this article, we will learn to find the LCM of two numbers using Java. L.C.M. or Least Common Multiple of two values is the smallest positive value which is the multiple of both values. For example, multiples of 3 and 4 are: 3 → 3, 6, 9, 12, 15 ... 4 → 4, 8, 12, 16, 20 ... The smallest multiple of both is 12, hence the LCM of 3 and 4 is 12.We will learn through two different methods to solve this: an incremental method and a formula-based approach using the Greatest Common Divisor (GCD).  Problem Statement Write ... Read More

Java program to find the GCD or HCF of two numbers

Chandu yadav
Updated on 14-Jun-2024 13:21:32

36K+ Views

An H.C.F or Highest Common Factor, is the largest common factor of two or more values.For example factors of 12 and 16 are −12 → 1, 2, 3, 4, 6, 12 16 → 1, 2, 4, 8, 16The common factors are 1, 2, 4 and the highest common factor is 4.AlgorithmDefine two variables - A, BSet loop from 1 to max of A, BCheck if both are completely divided by same loop number, if yes, store itDisplay the stored number is HCFExample: Using Java for loop import java.util.Scanner; public class GCDOfTwoNumbers {    public static void main(String args[]){   ... Read More

Java program to display English alphabets

Samual Sam
Updated on 13-Mar-2020 10:20:56

413 Views

Following is an example to display English alphabets A to Z.ExampleLive Demopublic class DisplayingAtoZ {    public static void main(String args[]){       char ch;       System.out.println("List of alphabets are ::");       for(ch = 'A'; ch

Java program to Count the number of digits in a given integer

Shriansh Kumar
Updated on 11-Sep-2024 10:36:31

22K+ Views

Suppose an integer number is given as an input, our task is to write a Java program to count the number of digits in that integer. For this problem, create a counter variable and initialize it with 0. Divide the given integer value with 10 till the integer becomes 0 and increment the counter variable for each turn. Using while Loop A while loop in Java is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. For the given problem, we use this loop to check whether the specified integer value is ... Read More

Advertisements