Found 338 Articles for Java Programming

Java program to find the area of a triangle

George John
Updated on 13-Mar-2020 08:19:28

2K+ Views

Area of a triangle is half the product of its base and height. Therefore, to calculate the area of a triangle.Get the height of the triangle form the user.Get the base of the triangle form the user. Calculate their product and divide the result with 2.Print the final result.Exampleimport java.util.Scanner; public class AreaOfTriangle {    public static void main(String args[]){       int height, base, area;       Scanner sc = new Scanner(System.in);       System.out.println("Enter the height of the triangle ::");       height = sc.nextInt();       System.out.println("Enter the base of the triangle ::"); ... Read More

Java program to find the area of a rectangle

Samual Sam
Updated on 18-Jun-2024 16:59:45

10K+ Views

Area of a rectangle is the product of its length and breadth. Therefore, to calculate the area of a rectangleGet the length of the rectangle form the user.Get the breadth of the rectangle form the user.Calculate their product.Print the product.ExampleBelow is an example to find the area of a rectangle in Java using Scanner class.import java.util.Scanner; public class AreaOfRectangle {    public static void main(String args[]){       int length, breadth, area;       Scanner sc = new Scanner(System.in);       System.out.println("Enter the length of the rectangle ::");       length = sc.nextInt();       ... Read More

Java program to find the area of a square

Samual Sam
Updated on 13-Mar-2020 07:33:41

6K+ Views

A square is a rectangle whose length and breadth are same. Therefore, Area of a rectangle is the square of its length. so, to calculate the area of a squareGet the length of the square form the user.Calculate its square.Print the square.Exampleimport java.util.Scanner; public class AreaOfSquare {    public static void main(String args[]){       int length, area;       Scanner sc = new Scanner(System.in);       System.out.println("Enter the length of the square ::");       length = sc.nextInt();       area = length* length;       System.out.println("Area of the square is ::"+area);   ... Read More

Write an example to find whether a given string is palindrome using recursion

Arjun Thakur
Updated on 13-Mar-2020 07:31:14

812 Views

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.Following is an example to find palindrome of a given number using recursive function.Examplepublic class PalindromeRecursion {    public static boolean isPalindrome(String str){       if(str.length() == 0 ||str.length()==1){          return true;       }       if(str.charAt(0) == str.charAt(str.length()-1)){          return isPalindrome(str.substring(1, str.length()-1));       }       return false; ... Read More

Java program to find the sum of elements of an array

Lakshmi Srinivas
Updated on 14-Jun-2024 14:12:56

44K+ Views

To find the sum of elements of an array.create an empty variable. (sum)Initialize it with 0 in a loop.Traverse through each element (or get each element from the user) add each element to sum.Print sum.Exampleimport java.util.Arrays; import java.util.Scanner; public class SumOfElementsOfAnArray {    public static void main(String args[]){       System.out.println("Enter the required size of the array :: ");       Scanner s = new Scanner(System.in);       int size = s.nextInt();       int myArray[] = new int [size];       int sum = 0;       System.out.println("Enter the elements of the array one by one ");       for(int i=0; i

Java program to print Fibonacci series of a given number.

karthikeya Boyini
Updated on 13-Mar-2020 07:25:42

485 Views

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.ExampleFollowing is an example to find Fibonacci series of a given number using a recursive functionpublic class FibonacciSeriesUsingRecursion {    public static long fibonacci(long number) {       if ((number == 0) || (number == 1)) return number;          else return fibonacci(number - 1) + fibonacci(number - 2);       }       public static void main(String[] args) {          for (int counter = 0; counter

Java program to print Pascal's triangle

Chandu yadav
Updated on 13-Mar-2020 07:24:47

8K+ Views

Pascal's triangle is one of the classic example taught to engineering students. It has many interpretations. One of the famous one is its use with binomial equations.All values outside the triangle are considered zero (0). The first row is 0 1 0 whereas only 1 acquire a space in Pascal’s triangle, 0s are invisible. Second row is acquired by adding (0+1) and (1+0). The output is sandwiched between two zeroes. The process continues till the required level is achieved.AlgorithmTake a number of rows to be printed, n.Make outer iteration I for n times to print rows.Make inner iteration for J ... Read More

Java program to generate and print Floyd’s triangle

Lakshmi Srinivas
Updated on 13-Mar-2020 07:22:18

4K+ Views

Floyd's triangle, named after Robert Floyd, is a right-angled triangle, which is made using natural numbers. It starts at 1 and consecutively selects the next greater number in the sequence.AlgorithmTake a number of rows to be printed, n.Make outer iteration I for n times to print rowsMake inner iteration for J to IPrint KIncrement KPrint NEWLINE character after each inner iterationExampleimport java.util.Scanner; public class FloyidsTriangle {    public static void main(String args[]){       int n, i, j, k = 1;       System.out.println("Enter the number of lines you need in the FloyidsTriangle");       Scanner sc ... Read More

Java program to delete duplicate characters from a given String

karthikeya Boyini
Updated on 13-Mar-2020 07:18:07

2K+ Views

The interface Set does not allow duplicate elements, therefore, create a set object and try to add each element to it using the add() method in case of repetition of elements this method returns false −If you try to add all the elements of the array to a Set, it accepts only unique elements so, to find duplicate characters in a given stringConvert it into a character array.Try to insert elements of the above-created array into a hash set using add method.If the addition is successful this method returns true.Since Set doesn't allow duplicate elements this method returns 0 when ... Read More

Java program to read numbers from users

George John
Updated on 13-Mar-2020 07:08:41

511 Views

The java.util.Scanner class is a simple text scanner which can parse primitive types and strings using regular expressions.1. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.2. A scanning operation may block waiting for input.3. A Scanner is not safe for multi-threaded use without external synchronization.The nextInt() method of the Scanner class is used to read an integer value from the source.Exampleimport java.util.Scanner; public class ReadingNumbersFromUser {    public static void main(String args[]){       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number ::");       int num ... Read More

Advertisements