Found 7442 Articles for Java

Java program to find the sum of elements of an array

Lakshmi Srinivas
Updated on 14-Jun-2024 14:12:56

47K+ Views

To find the sum of elements of an array.create an empty variable. (sum)Initialize it with 0 in a loop.Traverse through each element (or get each element from the user) add each element to sum.Print sum.Exampleimport java.util.Arrays; import java.util.Scanner; public class SumOfElementsOfAnArray {    public static void main(String args[]){       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       int sum = 0;       System.out.println("Enter the elements of the array one by one ");       for(int i=0; i

Java program to find the sum of elements of an array

Lakshmi Srinivas
Updated on 14-Jun-2024 14:12:56

47K+ Views

To find the sum of elements of an array.create an empty variable. (sum)Initialize it with 0 in a loop.Traverse through each element (or get each element from the user) add each element to sum.Print sum.Exampleimport java.util.Arrays; import java.util.Scanner; public class SumOfElementsOfAnArray {    public static void main(String args[]){       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       int sum = 0;       System.out.println("Enter the elements of the array one by one ");       for(int i=0; i

Java program to print Fibonacci series of a given number.

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

661 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 Fibonacci series of a given number.

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

661 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 16-Sep-2024 23:25:11

10K+ Views

In this article we will learn to print Pascal's triangle in Java. Pascal's triangle is one of the classic examples taught to engineering students. It has many interpretations. One of the famous ones 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 acquires 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. Problem Statement Write a program in Java to print ... Read More

Java program to print Pascal\'s triangle

Chandu yadav
Updated on 16-Sep-2024 23:25:11

10K+ Views

In this article we will learn to print Pascal's triangle in Java. Pascal's triangle is one of the classic examples taught to engineering students. It has many interpretations. One of the famous ones 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 acquires 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. Problem Statement Write a program in Java to print ... 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 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 31-Jul-2024 17:49:38

3K+ Views

The Set interface 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 string. Problem Statement Given a string, write a program in Java to delete duplicate characters from a given string − Input TUTORIALSPOINT Output Indices of the duplicate characters in the given string :: Index :: ... Read More

Java program to delete duplicate characters from a given String

karthikeya Boyini
Updated on 31-Jul-2024 17:49:38

3K+ Views

The Set interface 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 string. Problem Statement Given a string, write a program in Java to delete duplicate characters from a given string − Input TUTORIALSPOINT Output Indices of the duplicate characters in the given string :: Index :: ... Read More

Advertisements