Found 267 Articles for Java8

Java program to accept an integer from user and print it

Ankith Reddy
Updated on 13-Mar-2020 10:46:39

750 Views

Scanner class of the util package class is used to read data from user The nextInt() method of this class reads an integer from the user.Programimport java.util.Scanner; public class PrintInteger {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter an integer:");       int num = sc.nextInt();       System.out.print("Given integer is :: "+num);    } }OutputEnter an integer: 123 Given integer is :: 123

Java program to find the roots of a quadratic equation

Lakshmi Srinivas
Updated on 21-Jun-2024 13:20:38

11K+ Views

Roots of a quadratic equation are determined by the following formula:$$x = \frac{-b\pm\sqrt[]{b^2-4ac}}{2a}$$Algorithm To calculate the rootsCalculate the determinant value (b*b)-(4*a*c).If determinant is greater than 0 roots are [-b +squareroot(determinant)]/2*a and [-b -squareroot(determinant)]/2*a.If determinant is equal to 0 root value is (-b+Math.sqrt(d))/(2*a)Example to find the roots of a quadratic equationBelow is an example to find the roots of a quadratic equation in Java programming.import java.util.Scanner; public class RootsOfQuadraticEquation {    public static void main(String args[]){       double secondRoot = 0, firstRoot = 0;       Scanner sc = new Scanner(System.in);       System.out.println("Enter the value of ... Read More

Java program to round a number

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

519 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 13-Mar-2020 10:34:31

2K+ Views

Read integers from users using the nextInt() method of the Scanner class.To swap them −Create a variable (temp), initialize it with 0.Assign 1st number to temp.Assign 2nd number to 1st number.Assign temp to second number.Exampleimport java.util.Scanner; public class SwapTwoNumbers {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter first number :: ");       int num1 = sc.nextInt();       System.out.println("Enter second number :: ");       int num2 = sc.nextInt();       int temp = 0;       temp = num1;     ... Read More

Java program to calculate the power of a number

Lakshmi Srinivas
Updated on 13-Mar-2020 10:31:34

2K+ 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)Exampleimport java.util.Scanner; public class PowerOfNumber {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter the base number ::");       int base = sc.nextInt();       int temp = base;       System.out.println("Enter the exponent number ::");       int exp = sc.nextInt();       for (int i=1; i

Java program to print a multiplication table for any number

Arjun Thakur
Updated on 30-Jul-2019 22:30:22

1K+ Views

Following is a Java program which accepts an integer variable from user and prints the multiplication table of that particular integer.Exampleimport java.util.Scanner; public class MultiplicationTable { public static void main(String args[]) { System.out.println("Enter an integer variable :: "); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); for(int i=1; i

Java program to find the LCM of two numbers

karthikeya Boyini
Updated on 13-Mar-2020 10:29:51

7K+ Views

L.C.M. or Least Common Multiple of two values is the smallest positive value which 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.AlgorithmInitialize A and B with positive integers. Store maximum of A & B to the max. Check if max is divisible by A and B. If divisible, Display max as LCM. If not divisible then step increase max, go to step 3.Examplepublic class LCMOfTwoNumbers {    public static void ... Read More

Java program to find the GCD or HCF of two numbers

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

28K+ 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

289 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

George John
Updated on 18-Jun-2024 15:07:07

16K+ Views

Read a number from user. Create an integer (count) initialize it with 0. Divide the number with 10. till the number is 0 and for each turn increment the count.Exampleimport java.util.Scanner; public class CountingDigitsInInteger {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       int count = 0;       System.out.println("Enter a number ::");       int num = sc.nextInt();       while(num!=0){          num = num/10;          count++;       }       System.out.println("Number of digits in the entered integer are :: "+count);    } }OutputEnter a number :: 1254566 Number of digits in the entered integer are :: 7

Advertisements