Category Archives: Java Basic Programs

Java Basic Programs

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.

Java Program to Print Name Of Day

Java Program to Print Name Of Day

In this tutorial, we will learn to create a Java program that will Print Name Of Day 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 Day Name of a Week:-

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

With the help of the below program, we can find the name of the day in a week.

Program Code:-

Output:-

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

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

Taking the input day from the user.

Finding the name of the day in a week using conditional statements.

Printing the output of the program.

Java program to find Voting Age Program in Java

In this tutorial, we will learn to create a Java Program to check Voting age  in Java using Java programming.

Prerequisites

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

  • 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.
  • Scanner class.
  • Basic inbuilt functions.

Voting

Voting is a technique for a group of people for finding a opinion on something or making a collective decision,such as a meeting or an electorate vote .

Java program to find Voting Age Program in Java

In this program we will find voting age  using if-else statement  in java programming. We would first declared and initialized the required variables. Next, we would prompt user to input the age value .Later we calculate the age is valid for vote or not let’s have a look at the code.

Output

Valid condition.

Not valid condition.

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

  • age= it will hold entered age by user
  • shrt= it will hold value person short by how much.

After declaring variables in the next statement user will be prompted to enter a value and  which will be assigned to variable ‘age’.

And after that we calculate the value given age by user is valid for voting or not firstly check condition for valid voting if the given age is grater than 18 or equal to 18 ( age>=18) then it valid age.

Here what we have considered that age of given by user is eligible to vote if he attains age of 18 and more .

If age of person is  not eligible to vote then we going to calculate the years after person can vote as shown in image above.

So in our program we calculate the age or person is eligible for vote or not.

Passing Division Program in Java

In this tutorial, we will learn to create a Java to Program Find Passing Division 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.

How to Calculate percentage?

Percent is one out of one hundred parts, i.e., A ratio of the parts out of 100. Symbol of a percent is “%”.

Example :->

 

Passing Division Program in Java

In this program we will find Passing Division number provided by user. first will take the input from the user and then calculate passing percentage of a user. Lets have look at the Code.

Output

Division:=>

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

  • m = it will hold entered math’s number.
  • e = it will hold entered English number.
  • s = it will hold entered science number.
  • art = it will hold entered art  number.
  • com = it will hold entered computer number.
  • total= it will hold total of all subject.
  • per =it will hold percentage  of a student.

After that we take 5 subjects values form user.

and then after that we add all values in variable total and

then after find percentage

and finally we will print the percentage according to inputted values.

Java Program to check Equal Number in Java

In this tutorial, we will learn to create a Java Program to check Equal Numbers 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.

How do you equal a number in Java?

How to compare integer values are equal or not in Java, we can use either == (operator) or we can simply subtract these two values and values becomes zero means given number are equal. Both are used to compare two values. We can also use the equal() method to find numbers are equal or not.
Example How to check whether they are equal or not, use equal to (== )operator.
a == b

Example2 How to check whether they are equal or not, use the equal to ( )operator.
if(a -b==0)they are equal other wise they are not equal..

Java Program to check Equal Number in Java

In this program, we will take two values from the user and print numbers are an equal or not the smallest element in an array using a nested for loop. We would first declare and initialized the required variables. Next, we will find the smallest number in an array. Let’s have a look at the code.

Using relational operator equal to (==) in java program..

Output

Equal

Not Equal

using arithmetic operator equal to(-) in java program..

Output

Equal

Not Equal

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

  • a= it will hold value of first variable.
  • b= it will hold value of second variable.
  • i and j for iteration
  • temp = it will hold temporary value of an array.

After declaring variables we initiate values and take value from user at run time.

then with the help of relational and arithmetic operator we will find given integer are equal or not.

using arithmetic operator equal to(-) in java program..

Using relational operator equal to (==) in java program..

and finally we will print the results…