Found 7442 Articles for Java

Rename multiple files using Java

AmitDiwan
Updated on 04-Jul-2020 10:46:58

1K+ Views

Following is the code to rename multiple files using Java −Exampleimport java.io.File; import java.io.IOException; public class Demo{    public static void main(String[] argv) throws IOException{       String path_to_folder = "path\to\folder\where\multiple\files\are\present";       File my_folder = new File(path_to_folder);       File[] array_file = my_folder.listFiles();       for (int i = 0; i < array_file.length; i++){          if (array_file[i].isFile()){             File my_file = new File(path_to_folder + "\" + array_file[i].getName());             String long_file_name = array_file[i].getName();             String[] my_token = long_file_name.split("\s"); ... Read More

How to clear screen using Java?

AmitDiwan
Updated on 04-Jul-2020 10:44:46

3K+ Views

Following is the code to clear screen using Java −Example Live Demopublic class Demo{    public static void main(String[] args){       System.out.print("\033[H\033[2J");       System.out.flush();    } }OutputThe screen would be clearedA class named Demo contains the main function. Here, the ANSI escape code is written, that clears the screen. The flush function resets the cursor to the top of the window screen.

Java Program for check if a given number is Fibonacci number?

Shriansh Kumar
Updated on 01-Aug-2024 11:57:13

2K+ Views

For a given input number, write a Java program to check if it is a Fibonacci number. Any number that belongs to Fibonacci series is called as Fibonacci number. The Fibonacci series is a sequence of numbers formed by the sum of its two previous integers. The first two terms of this series are 0 and 1 and further terms go like 1, 2, 3, 5, 8 and so on. This series was named after a famous Italian mathematician, Leonardo Fibonacci. Example Scenario 1 Input: num1 = 8; Output: 8 is a Fibonacci number 8 comes into ... Read More

Java Program for GCD of more than two (or array) numbers

Alshifa Hasnain
Updated on 23-Dec-2024 18:08:05

628 Views

In this article, we will learn to calculate the GCD of more than two numbers in Java. We will discuss two approaches, the recursive method and an optimized approach using the Euclidean algorithm. The Greatest Common Divisor (GCD) of two or more integers is the largest integer that divides all the numbers without leaving a remainder. When it comes to an array of numbers, finding the GCD means determining the GCD of all the elements in the array. Problem Statement Given an array of integers, find the GCD of all the elements.Example −Input {7, 49, 177, 105, 119, 42} Output ... Read More

Java Program for focal length of a spherical mirror

AmitDiwan
Updated on 23-Dec-2024 11:17:56

286 Views

In this article, we will learn two different approaches to computing the focal length of a spherical mirror using Java. While the second method makes use of Object-Oriented Programming (OOP) concepts for a more generic and flexible approach the first method is a simple functional implementation. Finding Focal Length Spherical mirrors play a vital role in optical systems, from telescopes and microscopes to vehicle rearview mirrors. These mirrors, either concave or convex, focus or diverge light, respectively, based on their geometry. The focal length, a key characteristic of spherical mirrors, determines how the mirror bends light.The focal length of a ... Read More

Java Program to find the vertex, focus and directrix of a parabola

AmitDiwan
Updated on 04-Jul-2020 10:03:43

195 Views

Following is the Java program to find the vertex, focus and directrix of a parabola −Example Live Demopublic class Demo{    public static void find_values(float val_1, float val_2, float val_3){       System.out.println("The value of vertex is (" + (-val_2 / (2 * val_1)) + ", "+ (((4 * val_1 * val_3) - (val_2 * val_2)) / (4 * val_1)) + ")");       System.out.println("The value of focus is (" + (-val_2 / (2 * val_1)) + ", " + (((4 * val_1 * val_3) - (val_2 * val_2) + 1) / (4 * val_1)) + ")");     ... Read More

Java Program to find the perimeter of a cylinder

AmitDiwan
Updated on 04-Jul-2020 10:01:17

273 Views

Following is the Java code to find the perimeter of a cylinder −Example Live Demoimport java.io.*; public class Demo{    static int find_peri(int dia, int ht){       return 2*(dia + ht);    }    public static void main(String[] args){       int dia = 7;       int ht = 15;       System.out.println("The perimeter of the cylinder is " + find_peri(dia, ht) + " units");    } }OutputThe perimeter of the cylinder is 44 unitsA class named Demo defines a static function that takes in two values, diameter and height. This function computes the sum ... Read More

Java Program to find largest prime factor of a number

AmitDiwan
Updated on 04-Jul-2020 09:59:52

3K+ Views

Following is the Java code to find the largest prime factor of a number −Example Live Demoimport java.io.*; import java.util.*; public class Demo{    static long maxPrimeFactors( long val){       long max_prime = -1;       while (val % 2 == 0) {          max_prime = 2;          val >>= 1;       }       for (int i = 3; i 2)       max_prime = val;       return max_prime;    }    public static void main(String[] args){       int val = 148592; ... Read More

String Formatting in Java using %

AmitDiwan
Updated on 04-Jul-2020 09:57:44

184 Views

Followimg is the code to implement String formatting in Java using % −Example Live Demopublic class Demo {    public static void main(String args[]){       String my_str = " sample.";       String concat_Str = String.format("This is a" + "%s", my_str);       String format_str_1 = String.format("The value is %.4f", 78.92367);       System.out.println(concat_Str);       System.out.println(format_str_1);    } }OutputThis is a sample. The value is 78.9237A class named Demo contains the main function. Here a string value is defined, which is used to format the string, by concatenating it to another variable. Similarly, a ... Read More

Java program to cube sum of first n natural numbers

Alshifa Hasnain
Updated on 19-Feb-2025 17:50:35

554 Views

In this article, we will learn to write a Java program to calculate the sum of cubes of the first n natural numbers. Understanding the Cube Sum Formula The sum of cubes of the first n natural numbers follows a mathematical formula − S = 13 + 23 + 33 + ... + n3 = { ( n ( n + 1 ) ) / 2 }2 Different Approaches Following are the two different approaches to printing the cube sum of first n natural numbers − Using a Loop Using a ... Read More

Advertisements