Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Showing posts with label PROGRAMS. Show all posts
Showing posts with label PROGRAMS. Show all posts

Monday, 19 July 2021

How Do You Find The First Non Repeated Character of a String in Java

Find First Non Repeating Character in a String in Java 

Now Here we are going print First non repeated character in String in java through a simple example or java program so that you can understand well.

So let's start a simple java program for find the first non repeated character of a String.

For example, we take a string in  java like String str = "akalsys";

So in above string there is k is a first non repeated character in String.

Java Example for First Non-Repeated Character in String.

class FirstNonRepeatedCharacterInString
{
public static void main(String args[])
{
String str = "akalsys";
for(char ch : str.toCharArray( ))
{
if(str.indexOf(ch) == str.lastIndexOf(ch))
{
System.out.println(ch);
break;
}
}
}
}

Output : k

Above example will you give the k as output.

Here we learned, how to find the first non repeated character in java string with a simple example.

Share:

Monday, 26 March 2018

Binary Search Program in Java

Binary Search in Java Program

Write a Java Program for Binary Search

Here we are gonna to see binary search program in java which is mostly asked in core java interviews.

Binary search is used to search specified element from the list of multiple elements. Binary search algorithm is a algorithm that find the position of a targate value within a sorted array. It compares the targate value to the middle element of the array.

Binary search is also known as half-interval search or binary chop or logarithmic search.

Let's see binary search java example.


Write a Java Program for Binary Search 

This is the simple implementation of binary search using java program. Here we are going to use Scanner class for taking input from the user e.g number of elements, values of elements, and key element for searching.

import java.util.Scanner;
class BinarySearchExample
{
public static void main(String args[])
{
int num, array[], counter, item, first, last, middle;

Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements");
num = sc.nextInt();

array = new int[num];//creating array and declare number of elements

System.out.println("Enter " +num+ " Elements ");

for(counter = 0; counter < num; counter++)
array[counter] = sc.nextInt();

System.out.println("Enter key element for searching ");
item = sc.nextInt();
first = 0;
last = num - 1;
middle = (first + last)/2;

while(first <= last)
{

if(array[middle] < item )
first = middle + 1;

else if(array[middle] == item)
{
System.out.println(item+ " found at location " + (middle + 1));
break;
}

else
{
last = middle - 1;
}

middle = (first + last)/2;
}

if(first > last)
System.out.println(item+ " not found");
}
}

Output: Enter number of elements
             5
             Enter 5 elements
             14
             65
             89
             91
             96
             Enter key element for searching
             89
             89 found at location 3

There is another way we can perform binary search operationg using java program.


Binary Search Example

This is another java program for binary search by using Arrays.binarySearch() method.

We have to import java.util.Arrays class.

import java.util.Arrays;
class BinarySearch2
{
public static void main(String args[])
{
int elements[] = {14, 65, 89, 91, 96};
int keyElement = 89;

int search = Arrays.binarySearch(elements, keyElement);
if(search < 0)
{
System.out.println(" Element not found ");
}
else
{
System.out.println(" Element is found at index " + search);
}
}
}

Output: Element is found at index 2

You can check other java programs, link given below

Linear Search Program in Java
Bubble Sort Program in Java 
Java Array Programming Interview Questions 

Here we discussed some most important binary search programs in java which will help you in java interviews.


Share:

Wednesday, 28 February 2018

Java Program to Check Leap Year or not

Leap Year Program in Java

Leap year program in Java

Now, Here we going to write a java program to check year is leap year or not. This leap year java program is the most important program because it is mostly asked in java programming interviews

Let's write a java program to check whether the entered year is leap year or not.


What is Leap Year?

Normally every year comes with 365 days but when any year comes with 366 days is known as leap year.

Let's understand with an example where we will take year as an input from the user by using Scanner class and then calculate the year is leap year or not.


Java Leap Year Program Example

This is simple java program to find leap year or not with line-by-line.

import java.util.Scanner;
class LeapYearExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the year");
int year = sc.nextInt();

boolean b = false;
if(year % 400 == 0)

{

b = true;

}

else if(year % 100 == 0)

{

b = false;

}

else if(year % 4 == 0)

{

b = true;

}

else

{

b = false;

}

if(b)
{
System.out.println(" year "+year+"  is a leap year ");
}
else
{
System.out.println(" year " +year+"  is not a leap year ");
}
}
}

Output : Enter the year
              2000
              year 2000 is a leap year

Note: If a year is divisible by 400 and 4, year is leap year. If year is divisible by 100, not a leap year.

Check some other basic java programs.

Learn how to convert decimal to binary in java and how to find area of triangle in java.

Here we learned how to calculate leap year in java through program.

Share:

Friday, 23 February 2018

Write a Java Program for Linear Search

Linear Search Program in Java

Linear Search Program in Java

Here, We will see how to write a java program for linear search algorithm step-by-step. Linear search algorithm is the most important topic and it is mostly asked in core java interviews.

Linear search is a very simple searching algorithm and it is also known as sequential search algorithm in java. By using linear search algorithm we can easily find particular elements or value in the list.

Linear search is used to search a target element from multiple elements and it is used to check if an element exists in the given list we compare it with every element in the list. If it is found then we print the location of an element at which it occurs.

Linear search algorithm is slower than binary search algorithm in java.

Let's understand, Java Linear search algorithm with a simple example.

Check other Java Programs for Practice.


Java Program for Linear Search

In this linear search program, We will take elements from the user and then we will search particular elements from multiple elements. Also take number of elements.

import java.util.Scanner;
class LinearSearchExample
{
public static void main(String args[])
{
int c, n, search, array[];

Scanner sc = new Scanner(System.in);
System.out.println("Enter number of elements");
n = sc.nextInt();
array = new int[n];

System.out.println("Enter " + n + " Integers");

for(c = 0; c < n; c++)
array[c] = sc.nextInt();

System.out.println("Enter value to find");
search = sc.nextInt();

for(c = 0; c < n; c++)
{
if(array[c] == search)
{
sop(search+ " is present at location " + (c + 1)+ " . ");

break;

}
}
if(c == n)
System.out.println(search+ "is not present in array");
}
}

Output: Enter number of elements
             5
             Enter 5 Integers
             47
             56
             30
             15
             89
             Enter value to find
             30
             30 is present at location 3


Java Linear Search Example 2

This is another java linear search example to find target value from the list.

package javatutorial95;
public class Demo2
{
public static int linearSearch(int[] arr, int key)
{
int size = arr.length;

for(int i = 0; i < size; i++)
{
if(arr[i] == key)
{
return i;
}
}
return -1;
}

public static void main(String args[])
{
int arr1[] = {50, 87, 66, 23};
int searchKey = 66;

System.out.println("key " + searchKey + " present at index " +linearSearch(arr1, searchKey));
}
}

Output: key 66 present at index 2

Read More:

Binary Search Program in Java.
Java Program to Check Leap Year or Not.
Java Program to Find Area of Square.
Some Java Basic & Important Programs.
Java String Conversion Examples

Here we saw linear search program in java by 2 simple examples.  
Share:

Wednesday, 6 December 2017

Java Program To Find Area of Square

Java Program for Area of Square

Java Program for Area of Square

Here we are gonna to see java program to find area of square and perimeter of square also with simple examples.

Before calculating area and perimeter of square, let's focus on some formulas we will use in coming examples.

Formula for area of a square is given below:

area = side * side


Formula for perimeter of a square is given below:

perimeter = 4 * side


Let's calculate the area of square in java with different-different examples.

1) Area of Square in Java Example 1

This is simple java program to calculate are of square where side are given.

class AreaOfSquare1
{
public static void main(String args[])
{
double side = 6.5;//given side
double area = side * side;//using formula for area of a square 
System.out.println("Area of Square is : "+area);
}
}

Output: Area of Square is : 42.25

Now moving to next example where we use Scanner class for reading the data or input from the keyboard by the user.


2) Area of Square in Java Example 2

This is another example of area of square where the value of side of square is given by the user form the console and then by the help of square formula we will calculate area of square.

import java.util.*;
class AreaOfSquare2
{
public static void main(String args[])
{
//Using Scanner class
Scanner sc = new Scanner(System.in);

System.out.println("Enter side of square");
double length = sc.nextDouble();
double area = length * length;

System.out.println("Area of Square is : "+area);
}
}

You can check for practice other java programs e.g How to calculate area and perimeter of rectangle and how to find area and circumference of cirlcle, etc.


3) Find the Perimeter of a Square in Java Example 3

This our final program in this article where we calculate both area and perimeter of square by using formulas.

import java.util.Scanner;
class PerimeterOfSquare3
{
public static void main(String args[])
{
double side, area, perimeter;

Scanner sc = new Scanner(System.in);

System.out.println("Enter side of Square");
side = nextDouble();
area = side * side;
perimeter = 4 * side;

System.out.println("Area of Square is : "+area);
System.out.println("Perimeter of Square is : "+perimeter);
}
}

Output: Enter side of Square
             6.5
             Area of Square is : 42.25
             Perimeter of Square is : 26.0


In this article, we have learned how to find the area of a square and how to find the perimeter of a square in java programming with simple examples.

Share:

Monday, 4 December 2017

Java Program To Calculate Area and Perimeter of Rectangle

Java Program for Area and Perimeter of Rectangle

calculate area and perimeter of rectangle in java

Now here we are gonna to discuss that how to write a simple java program to calculate area and perimeter of the rectangle. Here we will see some useful programs for calculating area and perimeter of rectangle which is quite useful in any java programming interviews.

Let's see java program to find area of rectangle first and then programs for perimeter of rectangle.

Here we will see some different-different java programs for area of rectangle in java.

Before learning java programs for area and perimeter of rectangle, let's discuss about some useful formula we will use for calculating area and perimeter.

Formula for Area of Rectangle

Area = length * width//This is area rectangle formula

Formula for Perimeter of a Rectangle

Perimeter = 2 * (length + width)//This is perimeter rectangle formula




1) Calculate Rectangle Area Using Java Example 1

This is simple example, where we will use Scanner class to take values of length and width of rectangle from the user and then calculate the area of rectangle.

import java.util.Scanner;
class AreaOfRectangle1
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
double length = sc.nextDouble();

System.out.println("Enter width of rectangle");
double width = sc.nextDouble();

double area = length * width;
System.out.println("Area of rectangle is : "+area);
}
}

Output: Enter length of rectangle
             5
             Enter width of rectangle
             9
             Area of rectangle is : 45.0


2) Calculatea Rectangle Area Using Java Example 2

In this example, We will find the rectangle's area by using method.

import java.util.Scanner;
class AreaOfRectangle2
{
static double rectangleArea(double l, double w)
{
double area;
area = l * w;
return area;
}

public static void main(String args[])
{
double length;
double width;
double area;

Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
length = sc.nextDouble();

System.out.println("Enter width of rectangle");
width = sc.nextDouble();

area = rectangleArea(length , width);
System.out.println("Area of rectangle is : "+area);
}
}


You can check other calculating area and perimeter programs in java e.g How to find area and perimeter of circle and how to find area of triangle.

Let''s see our final program which is how do you find the perimeter of  a rectangle by using java program.


3) How to Find the Perimeter of a Rectangle

In this example, We will see how to find the perimeter of a rectangle by using rectangle perimeter formula.

import java.util.*;
class PerimeterOfRectangle
{
public static void main(String args[])
{
double length, width, area, perimeter;

Scanner sc = new Scanner(System.in);

System.out.println("Enter length of rectangle");
length = sc.nextDouble();

System.out.println("Enter width of rectangle");
width = sc.nextDouble();

area = length * width;
perimeter = 2 * (length + width);

System.out.println("Area of rectangle is : "+area);
System.out.println("Perimeter of rectangle is : "+perimeter);
}
}

Output: Enter length of rectangle
             5
             Enter width of rectangle
             9
             Area of rectangle is : 45.0
             Perimeter of rectangle is : 28.0

Calculate area and perimeter of rectangle is quite important for core java interviews because it is mostly asked in java interview.
  
Share:

Saturday, 2 December 2017

Java Program To Calculate Area and Circumference of Circle

Java Program To Calculate Area and Circumference or Perimeter of Circle

Java Program To Calculate Area and Circumference of Circle

In the last post, We saw some examples of how to calculate an area of the triangle in java but here we are going to see java program to calculate area and circumference of circle by the help of different - different java programs.

Let's see java program for area of circle with step-by-step.

We will use formula to calculate the area and perimeter or circumference of circle. The formula is given below.

1) Calculate Area of Circle Formula

PI*radius*radius 

2) Calculate Circumference of Circle Formula

2*pi*radius 


(1) Area of Circle in Java Program 1

This is simple java program to find area of circle where we will take radius value from the user and then calculate area of circle.

import java.util.Scanner;
class AreaOfCircle
{
public static void main(String args[])
{
//creating Scanner 
Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of circle");
double radius = sc.nextDouble();

double area = Math.PI*radius*radius;
System.out.println("Area of Circle is : "+area);
}
}

Output: Enter radius of circle
             5
            Area of Circle is : 78.53981633974483


(2) Area of Circle in Java Program 2

This is another java program to calculate area of circle using constructor where we create constructor for calculating the circle area.

import java.util.Scanner;
class AreaOfCircle2
{
double area;

AreaOfCircle2(double radius)
{
area = Math.PI*radius*radius;
System.out.println("Area of Circle is : "+area);
}
}

class Test
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter radius of Circle");

double r = sc.nextDouble();
AreaOfCircle2 a = new AreaOfCircle2(r);
}
}

Output: Enter radius of Circle
             5
             Area of Circle is : 78.53981633974483

Let's take another simple java program to find area of circle.


(3) Area of Circle in Java Program 3

This another simple java program to calculate area of circle using method.

import java.util.*;
class AreaOfCircle3
{
//creating static method to find area of circle in java

static double area(double radius)
{
return Math.PI*radius*radius;
}

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of circle");
double r = sc.nextDouble();
double d = area(r);

System.out.println("Area of Circle is : "+d);
}
}

Output: Enter radius of circle
             5
             Area of Circle is : 78.53981633974483


(4) Calculate Circle Perimeter Using Java Example

In this example we will calculate the circle perimeter or circumference by using 2*pi*radius formula.

import java.util.Scanner;
class CircleCircumferenceExample
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

System.out.println("Enter radius of Circle");
double radius = sc.nextDouble();
double circumference = Math.PI*2*radius;

System.out.println("Circumference of Circle : "+circumference);
}
}

Output: Enter radius of Circle
             5
            Circumference of Circle : 31.41592653589793

Read More:

How to Calculate Area of Square in Java.
How to Calculate Area and Perimeter of a Rectangle in Java.
Java Program to Check Leap Year on not.
Java Program for Binary Search.
Java Program for Linear Search.

Here we discussed that how to calculate area of circle through java program and how to find the perimeter of circle through java program with different - different examples. 

Share:

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Link

Translate