Found 70 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

214 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

708 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

Shriansh Kumar
Updated on 11-Sep-2024 11:05:11

8K+ Views

Given a square whose length of all its sides are l, write a Java program to find its area. A square is a rectangle whose length and breadth are same. Therefore, area of this type of rectangle is the square of its length. To calculate the area of square in Java, you simply need to multiply the given length of square with the length itself and store the result in another variable. Java Program to Find The Area of a Square A Java program that illustrates how to find the area of the square is shown below − public class ... 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

959 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 14-Jun-2024 14:12:56

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

617 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

9K+ 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 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 read numbers from users

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

575 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 ... 7 Next
Advertisements