Found 9321 Articles for Object Oriented 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

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 print ASCII value of a particular character

Lakshmi Srinivas
Updated on 13-Mar-2020 07:06:25

468 Views

ASCII stands for American Standard Code for Information Interchange. There are 128 standard ASCII codes, each of which can be represented by a 7-digit binary number: 0000000 through 1111111.If you try to store a character into an integer value it stores the ASCII value of the respective character.Exampleimport java.util.Scanner; public class ASCIIValue {    public static void main(String args[]){       System.out.println("Enter a character ::");       Scanner sc = new Scanner(System.in);       char ch = sc.next().charAt(0);       int asciiValue = ch;       System.out.println("ASCII value of the given character is ::"+asciiValue);   ... Read More

Java program to find if the given number is a leap year?

Chandu yadav
Updated on 07-Nov-2023 02:58:02

65K+ Views

Finding a year is a leap or not is a bit tricky. We generally assume that if a year number is evenly divisible by 4 is a leap year. But it is not the only case. A year is a leap year if −1. It is evenly divisible by 1002. If it is divisible by 100, then it should also be divisible by 4003. Except this, all other years evenly divisible by 4 are leap years.Algorithm1. Take integer variable year2. Assign a value to the variable3. Check if the year is divisible by 4 but not 100, DISPLAY "leap year"4. ... Read More

Java program to print prime numbers below 100

Samual Sam
Updated on 13-Mar-2020 06:59:36

736 Views

Any whole number which is greater than 1 and has only two factors that is 1 and the number itself, is called a prime number. Other than these two number it has no positive divisor. For example −7 = 1 × 7Few prime numbers are − 1, 2, 3, 5, 7, 11 etc.Algorithm1. Take integer variable A2. Divide the variable A with (A-1 to 2)3. If A is not divisible by any value (A-1 to 2), except itself it is a prime number.4. Repeat this for all the numbers starting from 2 to the required limit.ExampleLive Demopublic class First100Primes {    public static void main(String args[]){       for(int i = 2; i

Java program to print the transpose of a matrix

karthikeya Boyini
Updated on 13-Mar-2020 06:16:50

2K+ Views

The transpose of a matrix is the one whose rows are columns of the original matrix, i.e. if A and B are two matrices such that the rows of the matrix B are the columns of the matrix A then Matrix B is said to be the transpose of Matrix A.To print the transpose of the given matrix −Create an empty matrix.Copy the contents of the original matrix to the new matrix such that elements in the [j][i] position of the original matrix should be copied to the [i][j] position of the new matrix.Print the new matrix.ExampleLive Demopublic class TransposeSample{ ... Read More

Java program to Print Odd and Even Number from an Array

Ankith Reddy
Updated on 13-Mar-2020 06:18:02

1K+ Views

In the loop check, the result of I %2 operation on each element, if 0 the element is even else the element is odd.ExampleLive Demopublic class OddNumbersInAnArray {    public static void main(String args[]){       int[] myArray = {23, 93, 56, 92, 39};       System.out.println("Even numbers in the given array are:: ");       for (int i=0; i

How to write Java program to add two matrices

Samual Sam
Updated on 13-Mar-2020 06:15:21

5K+ Views

To add two matrices −Create an empty matrixAt each position in the new matrix, assign the sum of the values in the same position from the given two matrices i.e. if A[i][j] and B[i][j] are the two given matrices then, the value of c[i][j] should be A[i][j] + B[i][j]ExampleLive Demopublic class AddingTwoMatrices{    public static void main(String args[]){       int a[][]={{1,2,3},{4,5,6},{7,8,9}};       int b[][]={{1,1,1},{1,1,1},{1,1,1}};       int c[][]=new int[3][3];       for(int i = 0;i

Advertisements