Author: misamaliraza94

  • 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…

  • 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…

  • 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…

  • 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…

  • 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…

  • 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…

  • 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 >…

  • Check Vowel or Consonant

    Check vowel or consonant using if..else statement Output In the above program, ‘i’ is stored in a char variable ch. In Java, you use double quotes (” “) for strings and single quotes (‘ ‘) for characters. Now, to check whether ch is vowel or not, we check if ch is any of: (‘a’, ‘e’, ‘i’, ‘o’, ‘u’). This is done using a simple if..else statement.

  • Check Even or Odd

    Output In the above program, a Scanner object, reader is created to read a number from the user’s keyboard. The entered number is then stored in a variable num. Now, to check whether num is even or odd, we calculate its remainder using % operator and check if it is divisible by 2 or not. For this, we use if…else statement in Java. If num is divisible by 2, we print num is…