Found 477 Questions for Java8

Nth Catalan numbers in java

Ankith Reddy
Updated on 25-Jun-2020 12:32:38
The nth Catalan number in terms of binomial coefficients is calculated by the formula(n + k )/k where k varies from 2 to n and n ≥ 0. i.e.Cn = (2n)!/((n+1)!n!)Programpublic class NthCatalanNumber {    public static long fact(int i) {       if(i

Catalan numbers in java

Arjun Thakur
Updated on 25-Jun-2020 12:25:10
The nth Catalan number in terms of binomial coefficients is calculated by the formula(n + k )/k where k varies from 2 to n and n ≥ 0. i.e.Cn = (2n)!/((n+1)!n!)Programpublic class CatalanNumbers {    public static long fact(int i) {       if(i

Smith Numbers in java

Chandu yadav
Updated on 25-Jun-2020 12:19:59
A composite number whose sum of digits equal to the sum of the digits of its prime factors.Ex: 58 = 2 x 29 (5 + 8 = 12) (2+ 2 + 9 = 12)Programpublic class SmithNumbers {    public static boolean isPrime(int number) {       int loop;       int prime = 1;       for(loop = 2; loop < number; loop++) {          if((number % loop) == 0) {             prime = 0;          }       }       if (prime ... Read More

k-th prime factor of a given number in java

George John
Updated on 25-Jun-2020 12:18:39
Following is the Java program which prints the kth prime factor of a number n, when k and n are given.Programimport java.util.Scanner; public class KthPrimeFactor {    public static void main(String args[]) {       int number, k, factor = 0;       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number :");             number = sc.nextInt();       System.out.println("Enter the k value :");       k = sc.nextInt();       int temp = k-1;             for(int i = 2; i< number; ... Read More

Find politeness of a number in java

Ankith Reddy
Updated on 25-Jun-2020 12:17:45
The numbers which can be expressed as the sum of positive consecutive integers are known as polite numbers.Ex: 5 = 2+3The number of ways a number can be expressed as the sum of positive integers will be the Politeness of that number.Ex: 9 = 4+5 || 2+3+4AlgorithmGet the prime factors of a number.Get the powers of prime factors greater than 2.Add 1 to all of them.Multiply them, subtract 1 from the result.Programimport java.util.Scanner; public class PolitenessOfANumber {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");   ... Read More

Sum of all proper divisors of a natural number in java

Arjun Thakur
Updated on 30-Jul-2019 22:30:22
Following is the Java program which prints all the sum of all the divisors of a given number.

Find all divisors of a natural number in java

Ankith Reddy
Updated on 25-Jun-2020 12:16:02
Following is the Java program which prints all the divisors of a given number.Programimport java.util.Scanner; public class DivisorsOfNaturalNumber {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter required number :");       int num = sc.nextInt();             for(int i = 1; i

Prime factors in java

Arjun Thakur
Updated on 25-Jun-2020 12:13:57
Factors are the numbers we multiply to get another number.factors of 14 are 2 and 7, because 2 × 7 = 14.Some numbers can be factored in more than one way.16 can be factored as 1 × 16, 2 × 8, or 4 × 4.A number that can only be factored as 1 times itself is called a prime number.The first few primes are 2, 3, 5, 7, 11, and 13.The list of all the prime-number factors of a given number is the prime factors of a number. The factorization of a number into its prime factors and expression of ... Read More

Deleting a File in java

George John
Updated on 25-Jun-2020 12:12:22
You can delete a file using the delete() method of the File class.ProgramLive Demoimport java.io.File; public class DeleteFileExample {    public static void main(String[] args) {       try {          File file = new File("myFile");          if(file.delete()) {             System.out.println(file.getName() + " is deleted!");          } else {             System.out.println("Delete operation is failed.");          }          catch(Exception e) {             e.printStackTrace();       }    } }   OutputDelete operation is failed.

Copying a File in java

Chandu yadav
Updated on 25-Jun-2020 12:11:19
Following Java program copies a file to another.Programimport java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class CopyExample {    public static void main(String[] args) {       FileInputStream ins = null;       FileOutputStream outs = null;       try {          File infile = new File("C:\Users\TutorialsPoint7\Desktop\abc.txt");          File outfile = new File("C:\Users\TutorialsPoint7\Desktop\bbc.txt");          ins = new FileInputStream(infile);          outs = new FileOutputStream(outfile);          byte[] buffer = new byte[1024];          int length;           ... Read More
Advertisements