Add Long Integers and Check for Overflow in Java

karthikeya Boyini
Updated on 16-Oct-2024 16:29:42

1K+ Views

In this article, we will add two long integers in Java and check if the sum causes an overflow, which happens when the result exceeds the maximum value that a long data type can hold,  defined by the Long class as Long.MAX_VALUE. If the sum goes beyond this limit, an exception will be thrown to handle the overflow. Otherwise, the sum will be displayed as the result. Steps to add long integers and check for overflow Following are the steps to add long integers and check for overflow − First, we will define two long integers ... Read More

Java Program Without Making Class

Arjun Thakur
Updated on 16-Oct-2024 16:29:26

1K+ Views

Is it possible to create and run a Java program without any class? The answer is Yes. The trick is to use an enum instead of a Class.Enums are similar to classes, but instead of using the class keyword, we use enum to define them. An enum is used to represent a public static constant. It can have a static main method as well.  Steps to write Java program without making classFollowing are the steps to write a Java program without making class −We will start by defining an enum instead of a class. An enum typically represents constants, but it can also ... Read More

Rotate Matrix Elements in Java

AmitDiwan
Updated on 16-Oct-2024 16:27:40

1K+ Views

In this article, we will understand how to rotate matrix elements using Java. A matrix is a representation of the elements in rows and columns. Matrix rotation is shifting the position of each element of the matrix by 1 position towards its right or left. We will be using two approaches: one where the matrix is rotated based on user input, and another where the matrix is defined within the code. Problem Statement Write a Java program to rotate matrix elements. Below is a demonstration of the same − Input The matrix is defined as 1 2 3 4 5 6 ... Read More

Compilation and Execution of a C# Program

Chandu yadav
Updated on 15-Oct-2024 17:48:01

21K+ Views

To compile and execute a program in C#, you just need to click the Run button or press F5 key to execute the project in Microsoft Visual Studio IDE. Compile a C# program by using the command-line instead of the Microsoft Visual Studio IDE − Open a text editor and add the above-mentioned code. Save the file as helloworld.cs Open the command prompt tool and go to the directory where you saved the file. Type csc helloworld.cs and press enter to compile your code. If there are no errors in your code, the command prompt takes you to the next line and ... Read More

Print Mirror Lower Star Triangle Pattern in Java

AmitDiwan
Updated on 15-Oct-2024 11:56:29

760 Views

In this article, we will understand how to print a mirror lower star triangle pattern in Java. The program will use loops and print statements to get the output that we want, allowing us to get the pattern based on the number of rows specified by the user or predefined in the code. Problem Statement Write a Java program to print a mirror lower star triangle pattern. Below is a demonstration of the same − Input Enter the number of rows : 8 Output The mirror lower star triangle pattern : * * * * * * * * ... Read More

Reverse an Array Up to a Given Position in Java

Samual Sam
Updated on 15-Oct-2024 11:54:38

905 Views

In this article, we will reverse the elements of an array up to a specified position using Java. You will learn how the program takes an array, reverses the first part of it up to a given index, and prints the modified array. An array can be reversed upto the given position pos and the remaining array is unchanged.Problem Statement Write a Java program to reverse an array upto a given position.Input Array = 1 2 3 4 5 6 7 8 9 10 Position = 6 Output Modified array is: 6 5 4 3 2 1 7 8 9 ... Read More

Check if String Contains Any Character in Given Set in Java

karthikeya Boyini
Updated on 15-Oct-2024 11:53:07

995 Views

In this article, we will learn to check if the string contains any character in the given set of characters in Java. We will iterate over the string and compare each character to a set of characters that we want to search for. If any match is found, the program will print the character and confirm that it is present in the string. Problem Statement Write a program in Java to check if the string contains any character in the given set of characters − Input str = abcde chSearch = 'b', 'c' Output Character b found in string abcde ... Read More

Print Common Characters of Two Strings in Alphabetical Order

AmitDiwan
Updated on 15-Oct-2024 11:52:18

2K+ Views

In this article, we will learn to print common characters of two strings in alphabetical order using Java. The program uses arrays to count how often each letter appears in both strings and then compares these counts to identify the common characters. Problem Statement Write a Java program to print common characters of two strings in alphabetical order − Input my_str_1 = "itsasample" my_str_2 = "thisisasample" Output The common characters between the two strings in alphabetical order is :aaeilmpsst Steps to print common characters of two strings in alphabetical order Following are the steps to print common characters of two strings ... Read More

Check Occurrence of Each Vowel in String in Java

Chandu yadav
Updated on 15-Oct-2024 11:51:21

4K+ Views

To count occurrences of vowels in a string again use the Map utility of Java as used in calculating the occurrence of each character in the string. Put each vowel as a key in Map and put the initial value as 1 for each key. Now compare each character with the key of the map if a character matches with a key then increase its corresponding value by 1. Steps to check occurrence of each vowel in String Following are the steps to check occurrence of each vowel in String − First, we will define ... Read More

Print X Star Pattern in Java

AmitDiwan
Updated on 15-Oct-2024 10:42:14

12K+ Views

In this article, we will understand how to print X star pattern using Java. The pattern is formed by using multiple for-loops and print statements. The pattern forms the shape of the letter "X" by printing stars ('X') and spaces in specific positions. The user can provide a number as input, which will determine the size of the pattern. The program uses nested loops to create the required star pattern, where stars are placed at the diagonal positions of a grid. Problem Statement Write a program in Java to print X star pattern. Below is a demonstration of the same: ... Read More

Advertisements