Found 2616 Articles for Java

Java Program to Round a Number to n Decimal Places

AmitDiwan
Updated on 21-Feb-2022 12:39:49

251 Views

In this article, we will understand how to round a number to n decimal places. Rounding of decimal values are done using the CEIL or FLOOR functions.Below is a demonstration of the same −InputSuppose our input is −Input : 3.1415OutputThe desired output would be −Output : 3.2 AlgorithmStep 1 - START Step 2 - Declare a float variable values namely my_input. Step 3 - Read the required values from the user/ define the values Step 4 – Use the CEIL function to round the number to the required decimal places. In this example we are rounding up to 2 decimal ... Read More

Java program to check the birthday and print happy birthday message

AmitDiwan
Updated on 19-Jul-2024 14:23:10

2K+ Views

In this article, we will understand how to check the birthday and print Happy Birthday message. Checking the birthday is accomplished by comparing the present day and the given birthday. Problem Statement Write a program that checks if today's date matches a predefined birthday date. If the dates match, the program should print a "Happy Birthday" message otherwise, it should indicate that today is not the birthday. Below is a demonstration of the same − Input Birthday Date: 15 July Output Today’s Date is 20-12-2021 Today is not my birthday Check the Birthday Using LocalDate ClassBelow are the steps to check ... Read More

Java Program to Iterate over enum

AmitDiwan
Updated on 21-Feb-2022 12:29:45

182 Views

In this article, we will understand how to iterate over enum objects. Enum is a datatype that represents a small collection of objects.Below is a demonstration of the same −InputSuppose our input is −Enum objects are defined as : red, blue, green, yellow, orangeOutputThe desired output would be −Printing the Objects: red blue green yellow orangeAlgorithmStep 1 – START Step 2 - Declare the objects of Enum function namely red, blue, green, yellow, orange Step 3 – Using a for loop, iterate over the objects of the enum function and print each object. Step 4- StopExample 1enum Enum { ... Read More

Java Program to Check if two of three Boolean variables are true

AmitDiwan
Updated on 21-Feb-2022 12:30:36

337 Views

In this article, we will understand how to check if two of three Boolean variables are true. Boolean variables are datatypes that can contain only true or false values.Below is a demonstration of the same −InputSuppose our input is −Input : true, true, falseOutputThe desired output would be −Result : Two of the three variables are trueAlgorithmStep 1 - START Step 2 - Declare 4 boolean values namely my_input_1, my_input_2, my_input_3 and my_result Step 3 - Read the required values from the user/ define the values Step 4 - Using an if-else condition, compare two of the three values each ... Read More

Java Program to Implement Multiple Inheritance

AmitDiwan
Updated on 21-Jun-2024 12:33:10

14K+ Views

In this article, we will understand how to implement multiple inheritance. Java does not support multiple inheritance. This means that a class cannot extend more than one class, but we can still achieve the result using the keyword 'extends'.Algorithm to Implement Multiple InheritanceStep 1 – START Step 2 – Declare three classes namely Server, connection and my_test Step 3 – Relate the classes with each other using 'extends' keyword Step-4 – Call the objects of each class from a main function. Step 5 – STOPExample 1class Server{    void my_frontend(){       System.out.println("Connection to frontend established successfully");}    } ... Read More

Java Program to Count the Number of Vowels and Consonants in a Sentence

AmitDiwan
Updated on 21-Feb-2022 11:34:59

2K+ Views

In this article, we will understand how to count the vowels and consonants in Java. Alphabets that include 'a' 'e' 'i' 'o' 'u' are called Vowels and all other alphabets are called Consonants.Below is a demonstration of the same −InputSuppose our input is −Hello, my name is CharlieOutputThe desired output would be −The number of vowels in the statement is: 8 The number of vowels in the Consonants is: 12AlgorithmStep1- Start Step 2- Declare two integers: vowels_count, consonants_count and a string my_str Step 3- Prompt the user to enter a string value/ define the string Step 4- Read the values ... Read More

Java Program to Compute Quotient and Remainder

AmitDiwan
Updated on 21-Feb-2022 11:12:33

758 Views

In this article, we will understand how to compute the quotient and reminder in Java. Quotient and Reminder is calculated using two simple formula "Quotient = Dividend / Divisor" and "Remainder = Dividend % Divisor".Given an integer a and a non-zero integer d, it can be shown that there exist unique integers q and r, such that a = qd + r and 0 ≤ r < |d|. The number q is called the quotient, while r is called the remainder.Below is a demonstration of the same −InputSuppose our input is −Dividend value: 50 Divisor: 3OutputThe desired output would be ... Read More

Java Program to Print the ASCII values

AmitDiwan
Updated on 21-Feb-2022 11:07:46

935 Views

In this article, we will understand how to print ascii values of characters. This is done by assigning the character to an integer value and printing those integer values.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. Extended ASCII adds an additional 128 characters that vary between computers, programs and fonts.Below is a demonstration of the same −InputSuppose our input is −Enter a character: sOutputThe desired output would be −Ascii value of s is 115 AlgorithmStep1- Start Step 2- Declare ... Read More

Java Program to convert Decimal to Octal

AmitDiwan
Updated on 21-Feb-2022 10:59:16

433 Views

In this article, we will understand how to convert decimal to octal. a Decimal number is a number whose whole number part and the fractional part is separated by a decimal point. Octal Number has a base of eight and uses the number from 0 to 7.Below is a demonstration of the same −InputSuppose our input is −Enter the decimal number : 8OutputThe desired output would be −The octal value is 10AlgorithmStep 1 - START Step 2 - Declare three integer value namely my_input, I and j and an integer array my_octal Step 3 - Read the required values from ... Read More

Java Program to Find the Perimeter of a Rectangle

AmitDiwan
Updated on 21-Feb-2022 10:45:19

794 Views

In this article, we will understand how to find the perimeter of a rectangle. The Perimeter of a rectangle is calculated by adding the lengths of all the sides of the rectangle.Below is a demonstration of a rectangle. The perimeter of a rectangle is the total length of the two lengths and two widths of the rectangle −InputSuppose our input is −The length of the sides of a rectangle are : 5, 8, 5, 8OutputThe desired output would be −Perimeter : 26 AlgorithmStep 1 – START Step 2 – Declare 5 floating point variables a, b, c, d and perimeter ... Read More

Advertisements