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

Check if Given Number is power of 2.

Objective: Given a number, write a program to find if number is power of two. Example: N = 5 Output: false. N = 8 Output: true (23) N = 512 Output: true (29) Approach: This problem can be solved in multiple ways; we will discuss three solutions here. Log2 Method Check the Remainder Convert number … Read more Check if Given Number is power of 2.

Get the Sum of Digits in a number till it become a single digit

Objective – Given a number, Write a program to get the sum of digits in a number till it become a single digit. Example: N = 999 -> 9+9+9 = 27-> 2+7 = 9 N = 789 -> 7+8+9= 24-> 2+4 = 6 Approach: Recursion– Find the sum of all digits. Make a recursive call … Read more Get the Sum of Digits in a number till it become a single digit

Print First N Prime Numbers – Java Code

Objective: Given a number N, write a program to print first N prime numbers. What is Prime number:?? A prime number (or a prime) is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. A number is either divisible by 1 or by number by itself. Example: N … Read more Print First N Prime Numbers – Java Code

Check if two Strings are equal without using built-in function – Java

Objective– Given two strings, find out if they are equal or not without using any built-in function (without using equals() function in java). Example: String x=’tutorial’ and String y=’tutorial’ are equal – true String x=’tutorial’ and String y=’tutorial ‘ are equal – false String x=’tutorial’ and String y=’ ‘ are equal – false Approach: If … Read more Check if two Strings are equal without using built-in function – Java

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

Swap two numbers using Bitwise XOR Operator

Bit Manipulation

Objective– Given two numbers, swap both the numbers using XOR operators. Example: X = 4, Y = 8 Output: X = 8, Y= 4 Approach: XOR operator There are many ways to swap two numbers but here we will discuss a solution to swap numbers using XOR(^) operator. Say numbers are x and y. Do … Read more Swap two numbers using Bitwise XOR Operator

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

Fizz Buzz Challenge – Java Implementation

Objective: Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz” Output: 1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, … Read more Fizz Buzz Challenge – Java Implementation