Category Archives: Java Programs

Java Programs

Java Program to Compare Two Arrays

Java Program to Compare Two Arrays

In this tutorial, we will learn to create a Java program that will Compare Two Arrays using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operators in Java Programming.

What is Compare?

In comparing, we compare two or more things point to point and find the common things or the differences in them.

Compare Two Arrays.

In this program, First, we will declare arrays one and two for the program. Then we will compare them from each other. Then, if they satisfy the conditional statements, they will print that the arrays are the same.  If not then satisfy the conditional statements then will print that the arrays are not the same. With the help of this program, we can Compare Two Arrays in an Array.

Program Code:-

 

Output:-

In the above program, we have first initialized the required variable.

  • arr1  = it will hold the integer array.
  • arr2  = it will hold the integer array.
  • arr3  = it will hold the integer array.
  • arr4  = it will hold the integer array.

Condition to check for comparison.

Printing if the arrays are the same.

Printing if the arrays are not the same.

Java Program to find Sum of Two Arrays Elements

Java Program to find Sum of Two Arrays Elements

In this tutorial, we will learn to create a Java program that will Find the Sum of Two Arrays Elements using Java programming.

Prerequisites:-

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

Program description to Find Sum of two array Elements.

In this program, we will first take the input array size of both arrays then we will check the size of both arrays. Now we will take the elements of both the array. Then we will add all the elements of both the array using the for a loop. At last, we will print that sum of both the arrays element using the print function.

With the help of this program, we can Find the Sum of Two Arrays Elements.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • arry1[] = it will hold the integer value of the input elements.
  • arry2[] = it will hold the integer value of the input elements.
  • i = it will hold the integer value.
  • arry3[] = it will hold the integer value sum of the array.

Input size of the array 1 and 2 from the user.

Taking the elements of the array from the user.

Program Code to find the sum of two array elements.

Printing output sum of the two array elements.

Java Program to Find the Sum of Array

Java Program to Find the Sum of Array

In this tutorial, we will learn to create a Java program that will Find the Sum of an Array using Java programming.

Prerequisites:-

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Arithmetic operations in Java Programming.

What is An array?

The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single and multidimensional.

Program description to Find Sum of all Elements in an Array.

In this program, we will first take the input array size and the elements from the user. Then we will add all the elements of the array using the for loop. At last, we will print that sum of elements of the array using the print function.

With the help of this program, we can Find the Sum of all Elements in an Array.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • sum = it will hold the integer value for the sum of numbers.
  • arry[] = it will hold the integer value of the input elements.
  • i = it will hold the integer value.
  • sz = it will hold the integer value of the input.

Input the number of elements and the elements of the array from the user.

Program Logic Code to find the sum of an array.

Printing output sum of the array elements.

Java Program to Find Length of Array

Java Program to Find Length of Array

In this tutorial, we will learn to create a Java program that will Find the Length of an Array in Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Conditional Statements in Java programming.
  • Array in Java programming.

What is An array?

The array is a collection of similar data types. An array can store multiple values with different indexes in memory by using a single variable. The array can be both single and multidimensional.

Algorithm:-

Find Length or Size of an Array:-

As we all know, the array is a collection of similar data type elements. In an array, only one variable is declared which can store multiple values. First, will take the number of elements of an array from the user. Then will Find the Length or Size of an Array. And at last, we will print the Length or Size of an Array.

With the help of this program, we can print the Length or Size of an Array.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • i = it will hold the integer value.
  • arr[] = it will hold the integer value.

Finding the length of the array.

Printing the length of the array.

Printing the elements of the array.

Java Program to print Words in Sentence

In this tutorial, we will learn to create a Java Program to print Words in Sentence using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.
  • in-built functions.

Words in Sentence

with the help of string’s split() function breaks the given string around matches cases with given regular expression.

Example: we take a string as shown below  and then break sentence in to word using split function

string s = “hello to Java Programming”.

Output:
hello
to
java
Programming

Java Program to print Words in Sentence

In this program, we will break sentence into word using if-else and for a loop. We would first declare and initialized the required variables. Next, we will break sentence into words. Let’s have a look at the code.

 

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • s= it will hold string.

After declaring variables we initiate values in  an array[]

After splitting the sentence into word  with regular expression, this method returns a charters of arrays and finally we will print words.

Java Program to Reverse Sentence

Java Program to Reverse Sentence

In this tutorial, we will learn to create a Java program that will reverse a String in Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Usage of Conditional statements in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java Programming.

What is reversing a sentence?

A sentence is a collection of words, numbers, and symbols. The sentence can be any string of the English Language. Reversing a sentence means writing the sentence in reverse order, from last to first.

Algorithm:-

Program Description for Reversing the sentence:-

In this program, We will first take the input sentence for the program in string format. Then we will call the reverse function to reverse the sentence in the program. Storing the reversed sentence in a variable. Printing the reversed Sentence to the user.

With the help of the below, code, we will reverse the sentence.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • sent = string type variable that will hold the value of the input sentence.
  • reversed = it will hold the integer value of a reversed sentence. 

A user-defined function to reverse a string.

Calling the reverse function in the program.

Printing the reversed sentence to the user.

Java Program to Check if a word is present in sentence

Java Program to Check if a word is present in the sentence

In this tutorial, we will learn to create a Java program that will Check if a word is present in a sentence in Java programming

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Conditional Statements in Java programming.

What is a string?

As we all know, the String is a collection of characters and words. The word is a collection of alphabets. For string, only one variable is declared which can store multiple values. The String can consist of all the type able data, it means Digits, Alphabets, Symbols, etc.

Algorithm:-

Check if a word is present in the sentence

In this program, first, we will declare the input string in the program. Then we will declare the word to be searched in the sentence. Then will find the occurrence of that word in that string. Printing the result if that string is present in the sentence or not.

Let us take the example program from the below code to search for the occurrence of that word.

Program:-

Output:-

In the above program, we have first initialized the required variable.

  • txt = It will hold the string value.
  • str1 = It will hold the string value. 

Searching the first string in the sentence using a conditional statement.

Searching the second word in the Sentence.

Printing output words are present in the sentence or not.

Java program to count number of words in sentence

Java program to count the number of words in a sentence

In this tutorial, we will learn to create a Java program that will count the number of words in a sentence in Java programming

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Conditional Statements in Java programming.

What is a Sentence?

As we all know, a sentence is a collection of characters and words. The word is a collection of alphabets. For string, only one variable is declared which can store multiple values. The String can consist of all the typeable data, it means Digits, Alphabets, Symbols, etc.

Algorithm:-

Count the number of words in a sentence

In this program, first, we will declare the input string in the program. Then we will declare the word to be searched in the sentence. Then will find the occurrence of that word in that string. Printing the result if that string is present in the sentence or not.

Let us take the example program from the below code to search for the occurrence of that word.

Program:-

Output:-

In the above program, we have first initialized the required variable.

  • txt = It will hold the string value.
  • str1 = It will hold the string value. 

Count the words in the sentence using a conditional statement.

Logic code of the user-defined functions

Printing output number of words in the sentence.

Square Number Series Program in Java

In this tutorial, we will learn to create a Square Number Series Program in Java  using java programming.

Prerequisites

Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:

  • Java Operators.
  • Basic Input and Output function in Java.
  • Class and Object in Java.
  • Basic Java programming.
  • If-else statements in Java.
  • For loop in Java.

Square of a number ?

Square a number is when we simply multiply number by itself.

Example: “Square of 2” = 2 ×2 = 4

Square Number Series Program in Java

In this program we will square of number till the size of N  using for loop. We would first declared and initialized the required variables. Next, we will find square of numbers .Lets have a look at the code.

Output

In the above program, we have first declared and initialized a set variables required in the program.

  • number= it will hold given number.
  • i = for iteration
  • sqr = it will hold square of values.

After declaring variables we initiate values taking value from user.

User entered a number till the number we looped using for loop and  within the loop we will find square of numbers.

as you can loop will till number and within the loop we will find squares of number

Now we use for loop to get the desired output as shown in  image.

And finally we print the result that is square of number till the number inputted by user.

Odd Number Series Program in Java

Odd Number Series Program in Java

In this tutorial, we will learn to create a Java program that will Print Odd Number Series using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • While loop in Java programming.
  • Conditional Statements in Java programming.
  • Arithmetic operations in Java Programming.

What is an Odd number?

A number is said to be an Odd number when that number is divided by two into two equal whole numbers. And there should be a remainder of zero when it is being divided by 2.

Program to Print Odd Numbers from 1 to N.

In this program, we will first take the input point from the user. Then we will find the Odd numbers up to that range. At last, we will print that series of Odd numbers up to that number.

With the help of this program, we can print a series of Odd numbers up to N number.

Algorithm:-

Program to Print Odd Numbers series:-

 

Output:-

In the above program, we have first initialized the required variable.

  • no = it will hold the integer value of the input.
  • i = it will hold the integer value for the loop.

Input message for the user to take an endpoint of the series.

Program Logic Code to generate the series of odd numbers.

Printing output odd digits

Even Number Series Program in Java

Even Number Series Program in Java

In this tutorial, we will learn to create a Java program that will Print Even Number Series using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following C programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • For loop in Java programming.
  • Conditional Statements in Java programming.
  • Arithmetic operations in Java Programming.

What is an Even number?

A number is said to be an Even number when that number is divided by two into two equal whole numbers. And there should be a remainder of zero when it is being divided by 2.

Program to Print Even Numbers from 1 to N.

In this program, we will first take the input point from the user. Then we will find the Even numbers up to that range. At last, we will print that series of Even numbers up to that number.

With the help of this program, we can print a series of Even numbers up to N number.

Algorithm:-

Program to Print Even Numbers series:-

 

Output:-

In the above program, we have first initialized the required variable.

  • no = it will hold the integer value of the input.
  • i = it will hold the integer value for the loop.

Input message for the user to take an endpoint of the series.

Program Logic Code to generate the series of Even numbers.

Printing output Even digits

Java Program to Print Name Of Month

Java Program to Print Name Of Month

In this tutorial, we will learn to create a Java program that will Print Name Of Month using Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • Conditional Statements in Java programming.
  • Arithmetic operations in Java Programming.

Algorithm:-

Find the Month Name of a year:-

In today’s program, we will take the input month in a range between 1 to 12 from the user. Then we will pass that variable to the conditional statements to find the name of the Month. At last, we will print the resulting name of the Month to the user.

With the help of the below program, we can find the name of the Month in a year.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • mth = it will hold the integer value for the input.

Taking the input month from the user.

Finding the name of the month in a year using conditional statements.

Printing the output of the program.