Found 338 Articles for Java Programming

Java program to find a cube of a given number

Samual Sam
Updated on 18-Jun-2024 15:48:41

13K+ Views

Cube of a value is simply three times multiplication of the value with self.For example, cube of 2 is (2*2*2) = 8.AlgorithmSteps to find a cube of a given number in Java programming:Take integer variable A.Multiply A three times.Display result as Cube.Exampleimport java.util.Scanner; public class FindingCube {    public static void main(String args[]){       int n = 5;       System.out.println("Enter a number ::");       Scanner sc = new Scanner(System.in);       int num = sc.nextInt();       System.out.println("Cube of the given number is "+(num*num*num));    } }OutputEnter a number :: 5 Cube of the given number is 125

Java program to calculate mode in Java

Chandu yadav
Updated on 13-Mar-2020 10:54:18

12K+ Views

In statistics math, a mode is a value that occurs the highest numbers of time. For Example, assume a set of values 3, 5, 2, 7, 3. The mode of this value set is 3 as it appears more than any other number.Algorithm1.Take an integer set A of n values. 2.Count the occurrence of each integer value in A. 3.Display the value with the highest occurrence.ExampleLive Demopublic class Mode {    static int mode(int a[], int n) {       int maxValue = 0, maxCount = 0, i, j;       for (i = 0; i < ... Read More

Java program to find the permutation when the values n and r are given

Lakshmi Srinivas
Updated on 13-Mar-2020 10:52:12

219 Views

Permutation refers a number of ways in which set members can be arranged or ordered in some fashion. The formula of permutation of arranging k elements out of n elements is −nPk = n! / (n - k)!Algorithm1. Define values for n and r. 2. Calculate factorial of n and (n-r). 3. Divide factorial(n) by factorial(n-r). 4. Display result as a permutation.Exampleimport java.util.Scanner; public class Permutation {    static int factorial(int n) {       int f;       for(f = 1; n > 1; n--){          f *= n;       ... Read More

Java program to find the square root of a given number

Arjun Thakur
Updated on 13-Mar-2020 10:49:49

1K+ Views

The process of finding the square root of a number can be divided into two steps. One step is to find integer part and the second one is for fraction part.AlgorithmDefine value n to find the square root of.Define variable i and set it to 1. (For integer part)Define variable p and set it to 0.00001. (For fraction part)While i*i is less than n, increment i.Step 4 should produce the integer part so far.While i*i is less than n, add p to i.Now i have the square root value of n.ExampleLive Demopublic class SquareRoot {    public static void main(String ... Read More

Java program to find the Frequency of a character in a given String

karthikeya Boyini
Updated on 24-Jun-2024 15:42:49

5K+ Views

To find the Frequency of a character in a given String Read a string from the user. Read the character. Create an integer variable initialize it with 0. Compare each character in the given string with the entered character increment the above created integer variable each time a match occurs.Exampleimport java.util.Scanner; public class FrequencyOfACharacter {    public static void main(String args[]){       System.out.println("Enter a string value ::");       Scanner sc = new Scanner(System.in);       String str = sc.nextLine();       System.out.println("Enter a particular character ::");       char character = sc.nextLine().charAt(0);       int count = 0;       for (int i=0; i

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

Advertisements