Java Program to Print an Array In this program, you’ll learn different techniques to print the elements of a given array in Java. Example 1: Print an Array using For loop public class Array{ public static void main(String[] args){ int[] array = {1, 2, 3, 4, 5}; for (int element: array) { System.out.println(element); } …
Day: April 25, 2021
Java Program to Remove All Whitespaces from a String In this program, you’ll learn to remove all whitespaces in a given string using regular expressions in Java. Example 1: Program to Remove All Whitespaces public class Whitespaces{ public static void main(String[] args){ String sentence = “T his is b ett er.”; System.out.println(“Original sentence: ” …
Java Code To Create Pyramid and Pattern In this program, you’ll learn to create pyramid, half pyramid, inverted pyramid, Pascal’s triangle and Floyd’s triangle sing control statements in Java. Programs to print triangles using *, numbers and characters Example 1: Program to print half pyramid using * * * * * * * * …
Java Program to Calculate Difference Between Two Time Periods In this program, you’ll learn to calculate the difference between two time periods in Java. Example: Calculate Difference Between Two Time Periods public class Time{ int seconds; int minutes; int hours; public Time(int hours, int minutes, int seconds){ this.hours = hours; this.minutes = minutes; this.seconds …
Java Program to Add Two Complex Numbers by Passing Class to a Function In this program, you’ll learn to add two complex numbers in Java by creating a class named Complex and passing it into a function add(). Example: Add Two Complex Numbers public class Complex{ double real; double imag; public Complex(double real, double …
Java Program to Sort Elements in Lexicographical Order (Dictionary Order) In this program, you’ll learn to sort the element words in lexicographical order using a for loop and if else in Java. Example: Program to Sort Strings in Dictionary Order public class Sort{ public static void main(String[] args){ String[] words = { “Ruby”, “C”, …
Java Program to Count the Number of Vowels and Consonants in a Sentence In this program, you’ll learn to count the number of vowels, consonants, digits and spaces in a given sentence using if else in Java. Example: Program to count vowels, consonants, digits, and spaces public class Count{ public static void main(String[] args){ …
Java Program to Find the Frequency of Character in a String In this program, you’ll learn to find the occurence (frequency) of a character in a given string. Example: Find Frequency of Character public class Frequency{ public static void main(String[] args){ String str = “This website is awesome.”; char ch = ‘e’; int frequency …
Java Program to Find Transpose of a Matrix In this program, you’ll learn to find and print the transpose of a given matrix in Java. Transpose of a matrix is the process of swapping the rows to columns. For 2×3 matrix, Matrix a11 a12 a13 a21 a22 a23 Transposed Matrix a11 a21 a12 a22 a13 a23 …
Java Program to Multiply two Matrices by Passing Matrix to a Function In this program, you’ll learn to multiply two matrices using a function in Java. For matrix multiplication to take place, the number of columns of the first matrix must be equal to the number of rows of the second matrix. In our example, …