Found 267 Articles for Java8

Java program to print Fibonacci series of a given number.

karthikeya Boyini
Updated on 13-Mar-2020 07:25:42

480 Views

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.ExampleFollowing is an example to find Fibonacci series of a given number using a recursive functionpublic class FibonacciSeriesUsingRecursion {    public static long fibonacci(long number) {       if ((number == 0) || (number == 1)) return number;          else return fibonacci(number - 1) + fibonacci(number - 2);       }       public static void main(String[] args) {          for (int counter = 0; counter

Java program to print Pascal's triangle

Chandu yadav
Updated on 13-Mar-2020 07:24:47

8K+ Views

Pascal's triangle is one of the classic example taught to engineering students. It has many interpretations. One of the famous one is its use with binomial equations.All values outside the triangle are considered zero (0). The first row is 0 1 0 whereas only 1 acquire a space in Pascal’s triangle, 0s are invisible. Second row is acquired by adding (0+1) and (1+0). The output is sandwiched between two zeroes. The process continues till the required level is achieved.AlgorithmTake a number of rows to be printed, n.Make outer iteration I for n times to print rows.Make inner iteration for J ... Read More

Java program to generate and print Floyd’s triangle

Lakshmi Srinivas
Updated on 13-Mar-2020 07:22:18

4K+ Views

Floyd's triangle, named after Robert Floyd, is a right-angled triangle, which is made using natural numbers. It starts at 1 and consecutively selects the next greater number in the sequence.AlgorithmTake a number of rows to be printed, n.Make outer iteration I for n times to print rowsMake inner iteration for J to IPrint KIncrement KPrint NEWLINE character after each inner iterationExampleimport java.util.Scanner; public class FloyidsTriangle {    public static void main(String args[]){       int n, i, j, k = 1;       System.out.println("Enter the number of lines you need in the FloyidsTriangle");       Scanner sc ... Read More

Java program to delete duplicate characters from a given String

karthikeya Boyini
Updated on 13-Mar-2020 07:18:07

2K+ Views

The interface Set does not allow duplicate elements, therefore, create a set object and try to add each element to it using the add() method in case of repetition of elements this method returns false −If you try to add all the elements of the array to a Set, it accepts only unique elements so, to find duplicate characters in a given stringConvert it into a character array.Try to insert elements of the above-created array into a hash set using add method.If the addition is successful this method returns true.Since Set doesn't allow duplicate elements this method returns 0 when ... Read More

Java program to read numbers from users

George John
Updated on 13-Mar-2020 07:08:41

511 Views

The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.1. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.2. A scanning operation may block waiting for input.3. A Scanner is not safe for multi-threaded use without external synchronization.The nextInt() method of the Scanner class is used to read an integer value from the source.Exampleimport java.util.Scanner; public class ReadingNumbersFromUser {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number ::");       int num ... Read More

Java program to calculate the product of two numbers

Samual Sam
Updated on 21-Jun-2024 12:47:09

8K+ Views

The * operator in Java is used to multiply two numbers. Read required numbers from the user using Scanner class and multiply these two integers using the * operator.Exampleimport java.util.Scanner; public class MultiplicationOfTwoNumbers {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter the value of the first number ::");       int a = sc.nextInt();       System.out.println("Enter the value of the first number ::");       int b = sc.nextInt();       int result = a*b;       System.out.println("Product of the given two numbers ... Read More

Java program to find if the given number is a leap year?

Chandu yadav
Updated on 07-Nov-2023 02:58:02

64K+ Views

Finding a year is a leap or not is a bit tricky. We generally assume that if a year number is evenly divisible by 4 is a leap year. But it is not the only case. A year is a leap year if −1. It is evenly divisible by 1002. If it is divisible by 100, then it should also be divisible by 4003. Except this, all other years evenly divisible by 4 are leap years.Algorithm1. Take integer variable year2. Assign a value to the variable3. Check if the year is divisible by 4 but not 100, DISPLAY "leap year"4. ... Read More

Java program to reverse a string using recursion

Ankith Reddy
Updated on 13-Mar-2020 07:00:25

841 Views

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function. You can reverse a string using the recursive function as shown in the following program.ExampleLive Demopublic class StringReverse {    public String reverseString(String str){           if(str.isEmpty()){          return str;       } else {          return reverseString(str.substring(1))+str.charAt(0);       }    }    public static void main(String[] args) {       StringReverse obj = new StringReverse();       String result = obj.reverseString("Tutorialspoint");       System.out.println(result);    } }OutputtniopslairotuT

Java program to print prime numbers below 100

Samual Sam
Updated on 13-Mar-2020 06:59:36

734 Views

Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number. Other than these two number it has no positive divisor. For example −7 = 1 × 7Few prime numbers are − 1, 2, 3, 5, 7, 11 etc.Algorithm1. Take integer variable A2. Divide the variable A with (A-1 to 2)3. If A is not divisible by any value (A-1 to 2), except itself it is a prime number.4. Repeat this for all the numbers starting from 2 to the required limit.ExampleLive Demopublic class First100Primes {    public static void main(String args[]){       for(int i = 2; i

Java program to find the average of given numbers using arrays

Lakshmi Srinivas
Updated on 13-Mar-2020 06:56:23

3K+ Views

You can read data from the user using scanner class.Using the nextInt() method of this class get the number of elements from the user.Create an empty array.Store the elements entered by the user in the array created above.Finally, Add all the elements in the array and divide the sub by the number of elements.Exampleimport java.util.Scanner; public class AverageUsingArrays {    public static void main(String args[]){       //Reading total no.of elements       Scanner sc = new Scanner(System.in);       System.out.println("Enter the number of elements/numbers");       int num = sc.nextInt();       ... Read More

Advertisements