Sometimes we need to print the numbers with its sign irrespective of number is positive (+) or negative(-). Example: X = 5, print = +5 Y = -8, print = -8 Say we need to print a  equation : 2x – 3y – 6 = 0 and if you will use System.out.println(“Equation: “+ a + … Read more

## 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

## 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

## Print First n numbers in Fibonacci Series

Objective: Write a program to print first n numbers in Fibonacci series. Fibonacci number: First two Fibonacci numbers are defined as 0 and 1 and every number after the first two is the sum of the two preceding ones Fibonacci Sequence- 0 1 1 2 3 5 8 13 21 34 55 89 …….. Code:Run … Read more

## 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 public class AreaOfTriangleBaseHeight { public static void area(double base, double height){ double area = (base*height)/2; System.out.println("Area of Triangle … Read more

## 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

## 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

## 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 public class CircumferenceOfCircle { public static void circumference(double radius){ double pi = 3.14; double circumference = 2*pi*radius; System.out.println("Area of circumference with radius="+radius + " … Read more

## 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

## 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

## Print Numbers from 1 to N without using loop

Objective: Given a number N, write an program to print from number 1 to N without using loop. Example: N = 20 Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 Approach: Use Recursion Make recursive to N = N-1. In tail recursion, print … Read more

## Swap two numbers using Bitwise XOR Operator

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

## 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

## 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