Found 74 Articles for Java Technologies

Different Ways to Print First K Characters of the String in Java

Shriansh Kumar
Updated on 20-Jul-2023 21:09:15

67 Views

A string is a class in Java that stores a series of characters enclosed within double quotes.Those characters are actually String-type objects. The string class is available in the‘java.lang’ package. Suppose we have given a string and a positive integer ‘k’. Now, thejob is to print that string's first 'k' characters in Java. Also, check whether the length ofgiven string is less than or not, if so print the original string. Java Program to Print First K Characters of the String Let’s understand the given problem with a few examples − Instance String st1 = “TutorialsPoint”; String st2 = “Tutorial”; ... Read More

Blank final in Java

karthikeya Boyini
Updated on 18-Jun-2020 12:40:21

580 Views

In Java, a final variable can a be assigned only once. It can be assigned during declaration or at a later stage. A final variable if not assigned any value is treated as a blank final variable. Following are the rules governing initialization of a blank final variable.A blank instance level final variable cannot be left uninitialized.The blank Instance level final variable must be initialized in each constructor.The blank Instance level final variable cannot be initialized in class methods.A blank static final variable cannot be left uninitialized.The static final variable must be initialized in a static block.A static final variable cannot ... Read More

Java program to find the area of a square

Samual Sam
Updated on 13-Mar-2020 07:33:41

5K+ Views

A square is a rectangle whose length and breadth are same. Therefore, Area of a rectangle is the square of its length. so, to calculate the area of a squareGet the length of the square form the user.Calculate its square.Print the square.Exampleimport java.util.Scanner; public class AreaOfSquare {    public static void main(String args[]){       int length, area;       Scanner sc = new Scanner(System.in);       System.out.println("Enter the length of the square ::");       length = sc.nextInt();       area = length* length;       System.out.println("Area of the square is ::"+area);   ... Read More

Write an example to find whether a given string is palindrome using recursion

Arjun Thakur
Updated on 13-Mar-2020 07:31:14

772 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.Following is an example to find palindrome of a given number using recursive function.Examplepublic class PalindromeRecursion {    public static boolean isPalindrome(String str){       if(str.length() == 0 ||str.length()==1){          return true;       }       if(str.charAt(0) == str.charAt(str.length()-1)){          return isPalindrome(str.substring(1, str.length()-1));       }       return false; ... Read More

Java program to find the sum of elements of an array

Lakshmi Srinivas
Updated on 02-Sep-2023 12:57:24

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

428 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

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

482 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

1 2 3 4 5 ... 8 Next
Advertisements