## Check if given number is perfect square – O(√N) Solution

Objective: Given a number, write a program to check if given number is perfect sqaure. Example: N = 16 Output: True N = 32 Output: False Approach: Naive Approach: If N = 1 return true. Iterate through 1 to N/2 and check for each number whether square of each number is equal to N, if … Read more Check if given number is perfect square – O(√N) Solution

## Find the roots of Quadratic Equation – Java Program

Quadratic equation is in the form = ax2 + bx + c Discriminant (d) = b2 – 4ac Then roots are calculates using the equations below Root_1 =  (-b + √d)/2a Root_2 =  (-b – √d)/2a See the image below Run This Code Java Code: Output: Quadratic Equation: +3x^2 -5x -8 = 0, Roots are:(2.6666666666666665, … Read more Find the roots of Quadratic Equation – Java Program

## Calculate Logn base r – Java Implementation

Objective: Given a number n and r, write a program to calculate Logrn Example: N = 32, r =2 Log232= 5 N = 64, r = 4 Log464= 3 Approach: Without using built-In function- Initialize result = 0. Keep dividing the given number by r till number is greater than 0 and add one to … Read more Calculate Logn base r – Java Implementation

## Find the Area and Perimeter of Rectangle – Java Program

A rectangle is a quadrilateral with four right angles. Say rectangle is given with side’s length A and B Area of Rectangle = A * B Perimeter of rectangle = 2(A+B) Code: Run This Code Output: Area of Rectangle: 12.0 Perimeter of Rectangle: 14.0

## Find the Area of Triangle using base and height – Java Program

Objective: Given the base and height of a triangle, using math formula for calculating the area of triangle, write an java program to calculate the triangle area. Area of a Triangle: (Base * Height)/2 Code:Run This Code Output: Area of Triangle is: 10.0

## Find the Circumference of a Circle – Java Program

Area of a circumference = 2πR Where π = 3.14 and R = radius of circle Example: Radius = 4.0 Area = 2*π*radius = 2*3.14*4 = 25.12 Java Code:Run This Code Output: Area of circumference with radius=4.0 is: 25.12

## Floyd’s Triangle – Java Implementation

Floyd’s Triangle: Floyd’s triangle is a right angled triangular array of natural numbers. It named after Robert Floyd. Rows of the triangle filled by consecutive numbers. First row will have single number which is 1. Second row will have two numbers, which are 2 and 3 Third row will have three numbers, which are 4, … Read more Floyd’s Triangle – Java Implementation

## Java Program to determine if Given Year is Leap Year

Objective: Given a Year, write a java program to find whether year is leap year or not. Leap Year: a year, usually occurring once every four years, that has 366 days including February 29 as an intercalary day, called Leap Year. Example: Year: 2004, 2008, 2012 are Leap years. Year: 1993, 2001, 2003 are not … Read more Java Program to determine if Given Year is Leap Year

## Find number of Distinct Permutations of a String

Objective – Given a string, find the number of distinct permutations or anagrams of that string. Example: String x = “abc” Output: 6 (abc, acb, bac, bca, cab, cba) String x = “aab” Output: 3 (aab, aba, baa) Approach: Number of ways to arrange n distinct items are = n!. If we have duplicate items, … Read more Find number of Distinct Permutations of a String

## Find the Area of a Circle – Java Program

Area of a circle = πR2 Where π = 3.14 and R = radius of circle Example: Radius = 4.0 Area = π*radius*radius = 3.14*4*4 = 50.24 Code:Run This Code Output: Area of circle with radius=4.0 is: 50.24

## Minimum number of guesses needed to find a specific number

Objective– Given the numbers 1 to 1000, what is the minimum number of guesses needed to find a specific number if you are given the hint “higher” or “lower” for each guesses you make. Naive Approach: Linear search Start guessing from 1 and then 2 then 3  …till we do not find the answer. Time … Read more Minimum number of guesses needed to find a specific number

## Find the Area of a Triangle Using coordinates

Objective – Given three vertices coordinates or (X, Y) coordinates, write a program to find the area of a triangle. Example: Approach: Given the coordinates of the three vertices of any triangle are (X1, Y1), (X2, Y2) and (X3, Y3) Java Code:Run On IDE Run On IDE Output: Area of Triangle: 7.0

## Java Program to find if Triangle can be formed using given 3 sides

Algorithms – Java Program to find if Triangle can be formed using given 3 sides Objective– Given 3 side lengths, write a program to find out if using these 3 sides, a triangle can be formed. Example: Triangle can be formed using side 2.5, 3.5, 5.0 Triangle cannot be formed using side 1.0, 3.0, 5.0 … Read more Java Program to find if Triangle can be formed using given 3 sides