Found 338 Articles for Java Programming

LCM of an array of numbers in Java

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

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

734 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

138 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

206 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

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

1K+ Views

The java.io.ByteArrayOutputStream.toString() method converts the stream's contents using the platform's default character set. The malformed-input and unmappable-character sequences are replaced by the default replacement string for the platform's default character set.Example Live Demoimport java.io.ByteArrayOutputStream; import java.io.IOException; public class ByteArrayOutputStreamDemo {    public static void main(String[] args) throws IOException {       String str = "";       byte[] bs = {65, 66, 67, 68, 69};       ByteArrayOutputStream baos = null;       try {          // create new ByteArrayOutputStream          baos = new ByteArrayOutputStream();          // write ... Read More

Java Program to Compare Strings

Samual Sam
Updated on 13-Mar-2020 13:00:09

5K+ Views

You can compare two Strings in Java using the compareTo() method, equals() method or == operator.The compareTo() method compares two strings. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string.The result is a negative integer if this String object lexicographically precedes the argument string.The result is a positive integer if this String object lexicographically follows the argument string.The result is zero if the strings are equal, compareTo returns 0 exactly when the equals(Object) method would ... Read More

Java Program to Convert contents of a file to byte array and Vice-Versa

Ankith Reddy
Updated on 13-Mar-2020 12:56:11

192 Views

The FileInputStream class contains a method read(), this method accepts a byte array as a parameter and it reads the data of the file input stream to given byte array. Assume the file myData contains the following data −Exampleimport java.io.File; import java.io.FileInputStream; public class FileToByteArray {    public static void main(String args[]) throws Exception{       File file = new File("myData");       FileInputStream fis = new FileInputStream(file);       byte[] bytesArray = new byte[(int)file.length()];       fis.read(bytesArray);       String s = new String(bytesArray);       System.out.println(s);    } }OutputHi how are you welcome to Tutorialspoint

Java program to calculate the GCD of a given number using recursion

karthikeya Boyini
Updated on 13-Mar-2020 12:54:09

3K+ Views

You can calculate the GCD of given two numbers, using recursion as shown in the following program.Exampleimport java.util.Scanner; public class GCDUsingRecursion {    public static void main(String[] args) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter first number :: ");       int firstNum = sc.nextInt();       System.out.println("Enter second number :: ");       int secondNum = sc.nextInt();       System.out.println("GCD of given two numbers is ::"+gcd(firstNum, secondNum));    }    public static int gcd(int num1, int num2) {       if (num2 != 0){          return gcd(num2, num1 % num2);       } else{          return num1;       }    } }OutputEnter first number :: 625 Enter second number :: 125 GCD of given two numbers is ::125

Java program to multiply given floating point numbers

George John
Updated on 13-Mar-2020 12:52:52

890 Views

ExampleFollowing is a program to multiply given floating point numbers.import java.util.Scanner; public class MultiplyFloatingNumbers {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter first floating point number.");       float flt1 = sc.nextFloat();       System.out.println("Enter second floating point number.");       float flt2 = sc.nextFloat();       float product = flt1*flt2;       System.out.println("Product of given floating point numbers ::"+product);    } }OutputEnter first floating point number. 2.2 Enter second floating point number. 6.3 Product of given floating point numbers ::13.860001

Java program to convert a Set to an array

Lakshmi Srinivas
Updated on 21-Jun-2024 11:15:54

13K+ Views

The Set object provides a method known as toArray(). This method accepts an empty array as argument, converts the current Set to an array and places in the given array. To convert a Set object to an array − Create a Set object. Add elements to it. Create an empty array with size of the created Set. Convert the Set to an array using the toArray() method, bypassing the above-created array as an argument to it. Print the contents of the array.ExampleLive Demoimport java.util.HashSet; import java.util.Set; public class SetToArray {    public static void main(String args[]){       Set set = new HashSet();   ... Read More

Advertisements