Found 7442 Articles for Java

GCD of an array of numbers in java

Ankith Reddy
Updated on 25-Jun-2020 12:00:32

2K+ Views

ProgramFollowing is the example to calculate the GCD of the numbers of an array.Live Demopublic class GCDOfArrayofNumbers{    public static int gcd(int a,int b){       int res = 0;       while (b > 0){          int temp = b;          b = a % b;          a = temp;          res = a;       }       return res;    }    public static void main(String arg[]){       int[] myArray = {3, 6, 8};       int result = gcd(myArray[0],myArray[1]);       for(int i = 2; i < myArray.length; i++){          result = gcd(result, myArray[i]);       }       System.out.println("Gcd of n numbers is: "+result);    } }OutputGCD of n numbers is: 1

LCM of an array of numbers in Java

George John
Updated on 25-Jun-2020 11:59:39

4K+ 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.ProgramFollowing example computes the LCM of array of numbers.Live Demopublic class LCMofArrayOfNumbers {    public static void main(String args[]) {       int[] myArray = {25, 50, 125, 625};       int min, max, x, lcm = 0;             for(int i = 0; i

LCM of an array of numbers in Java

George John
Updated on 25-Jun-2020 11:59:39

4K+ 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.ProgramFollowing example computes the LCM of array of numbers.Live Demopublic class LCMofArrayOfNumbers {    public static void main(String args[]) {       int[] myArray = {25, 50, 125, 625};       int min, max, x, lcm = 0;             for(int i = 0; i

GCD and LCM of two numbers in Java

Chandu yadav
Updated on 25-Jun-2020 11:57:26

960 Views

Following is an example which computes find LCM and GCD of two given numbers.Programimport java.util.Scanner; public class LCM_GCD {    public static void lcm(int a, int b){       int max, step, lcm = 0;       if(a > b){          max = step = a;       } else{          max = step = b;       }       while(a!= 0) {          if(max%a == 0 && max%b == 0) {             lcm = max;             break;          }          max += step;       }       System.out.println("LCM of given numbers is :: "+lcm);    }    public static void gcd(int a,int b){       int i, hcf = 0;          for(i = 1; i

GCD and LCM of two numbers in Java

Chandu yadav
Updated on 25-Jun-2020 11:57:26

960 Views

Following is an example which computes find LCM and GCD of two given numbers.Programimport java.util.Scanner; public class LCM_GCD {    public static void lcm(int a, int b){       int max, step, lcm = 0;       if(a > b){          max = step = a;       } else{          max = step = b;       }       while(a!= 0) {          if(max%a == 0 && max%b == 0) {             lcm = max;             break;          }          max += step;       }       System.out.println("LCM of given numbers is :: "+lcm);    }    public static void gcd(int a,int b){       int i, hcf = 0;          for(i = 1; i

Java Program to Convert a Stack Trace to a String

karthikeya Boyini
Updated on 13-Mar-2020 13:01:05

277 Views

The Java.io.StringWriter class is a character stream that collects its output in a string buffer, which can then be used to construct a string. Closing a StringWriter has no effect.The Java.io.PrintWriter class prints formatted representations of objects to a text-output stream.Using these two classes you can convert a Stack Trace to a String.ExampleLive Demoimport java.io.PrintWriter; import java.io.StringWriter; public class StackTraceToString {    public static void main(String args[]) {       try {          int a[] = new int[2];          System.out.println("Access element three :" + a[3]);       } catch (ArrayIndexOutOfBoundsException e) ... Read More

Java Program to Convert a Stack Trace to a String

karthikeya Boyini
Updated on 13-Mar-2020 13:01:05

277 Views

The Java.io.StringWriter class is a character stream that collects its output in a string buffer, which can then be used to construct a string. Closing a StringWriter has no effect.The Java.io.PrintWriter class prints formatted representations of objects to a text-output stream.Using these two classes you can convert a Stack Trace to a String.ExampleLive Demoimport java.io.PrintWriter; import java.io.StringWriter; public class StackTraceToString {    public static void main(String args[]) {       try {          int a[] = new int[2];          System.out.println("Access element three :" + a[3]);       } catch (ArrayIndexOutOfBoundsException e) ... Read More

Java Program to Check if a String is Numeric

Chandu yadav
Updated on 13-Mar-2020 13:01:54

337 Views

ExampleYou can check if a given string is Numeric as shown in the following program.Live Demoimport java.util.Scanner; public class StringNumeric {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string ::");       String str = sc.next();       boolean number = str.matches("-?\d+(\.\d+)?");       if(number) {          System.out.println("Given string is a number");       } else {          System.out.println("Given string is not a number");       }    } }OutputEnter a string :: 4245 Given string is a number

Java Program to Check if a String is Numeric

Chandu yadav
Updated on 13-Mar-2020 13:01:54

337 Views

ExampleYou can check if a given string is Numeric as shown in the following program.Live Demoimport java.util.Scanner; public class StringNumeric {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a string ::");       String str = sc.next();       boolean number = str.matches("-?\d+(\.\d+)?");       if(number) {          System.out.println("Given string is a number");       } else {          System.out.println("Given string is not a number");       }    } }OutputEnter a string :: 4245 Given string is a number

Java Program to Convert OutputStream to String

Shriansh Kumar
Updated on 05-Aug-2024 18:10:08

2K+ Views

The process of converting an OutputStream to a String is used during unit testing when it is necessary to check the content of the OutputStream before displaying it. In this article, we will explain Java programs to convert OutputStream into String. Here, String is a class in Java which represents sequence of characters and OutputStream class of java.io package is the superclass of those classes that represents an output stream of bytes. Using toString() Method The toString() method of the java.io.ByteArrayOutputStream class converts the specified stream's contents into String using the platform's default character set. Example The following ... Read More

Advertisements