Binary numbers are numbers consisting only of 2 digits: 0 and 1. They can be expressed in the base 2 numeral system. For example, Decimal numbers are numbers consisting of 10 digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. They can be expressed in the base 10 numeral system. Here, we will be writing a Java program that will convert a binary number into decimal and vice versa using built-in methods and custom… Continue reading Convert Binary Number to Decimal Number
Month: March 2022
Display Armstrong Number B/w Two Intervals
A positive integer is called an Armstrong number of order n if abcd… = an + bn + cn + dn + … In case of an Armstrong number of 3 digits, the sum of cubes of each digit is equal to the number itself. For example: This program is built on the concept of how to check… Continue reading Display Armstrong Number B/w Two Intervals
Display Prime Numbers Between Two Intervals
Display Prime Numbers Between two Intervals Output In this program, each number between low and high are tested for prime. The inner for loop checks whether the number is prime or not. The difference between checking a single prime number compared to an interval is, you need to reset the value of flag = false on each… Continue reading Display Prime Numbers Between Two Intervals
Concatenate Two Arrays
Concatenate Two Arrays using arraycopy Output In the above program, we’ve two integer arrays array1 and array2. In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length aLen + bLen. Now, in order to combine both, we copy each element in both arrays to result by… Continue reading Concatenate Two Arrays
Largest Element of an Array
Find the largest element in an array Output In the above program, we store the first element of the array in the variable largest. Then, largest is used to compare other elements in the array. If any number is greater than largest, largest is assigned the number. In this way, the largest number is stored in largest when it is printed.
Calculate Average Using Arrays
Output In the above program, the numArray stores the floating-point values whose average is to be found. Then, to calculate the average, we need to first calculate the sum of all elements in the array. This is done using a for-each loop in Java. Finally, we calculate the average by the formula: In this case, the total count is given… Continue reading Calculate Average Using Arrays
Calculate Standard Deviation
This program calculates the standard deviation of a individual series using arrays. To calculate the standard deviation, calculateSD() function is created. The array containing 10 elements is passed to the function and this function calculates the standard deviation and returns it to the main() function. Example: Program to Calculate Standard Deviation Output In the above program, we’ve used the… Continue reading Calculate Standard Deviation
Find LCM of two Numbers
LCM using while Loop and if Statement Output In this program, the two numbers whose LCM is to be found are stored in variables n1 and n2 respectively. Then, we initially set lcm to the largest of the two numbers. This is because, LCM cannot be less than the largest number. Inside the infinite while loop (while(true)), we check if lcm perfectly divides… Continue reading Find LCM of two Numbers
Check Leap Year
A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400. Java Program to Check a Leap Year Output In the above example, we are checking if the year 1900 is a leap year or not. Since 1900 is… Continue reading Check Leap Year
Roots of a Quadratic Equation
The standard form of a quadratic equation is: Here, a, b, and c are real numbers and a can’t be equal to 0. We can calculate the root of a quadratic by using the formula: The ± sign indicates that there will be two roots: The term b2-4ac is known as the determinant of a quadratic equation. It specifies the nature of roots. That is, if determinant >… Continue reading Roots of a Quadratic Equation