Add two numbers without using operators

Given two numbers, find sum of them without using arithmetic operators.

Explanation:

Lets assume we have two numbers "a" & "b". In while loop, repeatedly increment the number "a" and subtract number "b" until it becomes zero.

Java program to find sum of two numbers without using arithmetic operators:



Output:
Enter two numbers : 4   5
Sum is : 9

Enter two numbers : 6   8
Sum is : 14

Smallest of three integers without comparison operators

Given three numbers, find Smallest of three integers without comparison operators. Do not make use of mathematical operators in this problem.

Explanation:

Take a count variable and initialize it with 0. In while loop, repeatedly subtract the three numbers with 1. The number which becomes 0 first, then it is the smallest number.


Java Program to find smallest of three integers without comparison operators


Output:
Enter 3 numbers : 1     2      3
Smallest Number : 1

Enter 3 numbers : 4    3    6
Smallest Number : 3

Find angle between hour and minute hand (Clock Angle Problem)

Clock Angle Problem: Given time in HH:MM format, calculate the smallest angle between Hour hand and Minute hand.

Few examples shown in the table
Time1:002:307:0010:3011:203:405:158:45
Angle30°105°150°135°140°130°67½°7½°

In the above picture, small angle between hour and minute hand is 30° when the time is 1'o clock.

Explanation:

  • In 12 hours, hour hand rotates 360° , then in 1 hour it rotates 360°/12 = 30°
                                12 hours ---> 360° 
                                1 hour    ---> 360°/12 = 30°
  • In 60 minutes, minute hand rotates 360° , then in 1 minute it rotates 360°/60 = 6°
                                60 minutes ---> 360°
                                1 minute    ---> 360°/60 = 6°
  • Difference between these two angles is the angle between Hour hand & Minute hand.
  • If the angle is greater than 180° , then subtract it from 360° as we need small angle.

Java program to find angle between Hour and Minute hands :



Output :
Enter Time: 1:00
Small Angle b/w Hour and Min Hands : 30.0

Enter Time: 2:30
Small Angle b/w Hour and Min Hands : 105.0

Converting Decimal to Roman Numeral

In this post we will write a java program to convert a decimal number to equivalent Roman numeral.  Before we step in to the program, we will see what are the Roman symbols and its decimal values.


Roman Symbols and its values:
Roman SymbolDecimal value
I1
IV4
V5
IX9
X10
XL40
L50
XC90
C100
CD400
D500
CM900
M1000


Decimal Forming Rules:

1. When a symbol appears after a larger or equal symbol, then it is added
  • VI = V + I = 5 + 1 = 6
  • XXI = X + X + I = 10 + 10 + 1 = 21
2. But if the symbol appears before a larger symbol, the it is subtracted
  • IV = V - I = 5 - 1 = 4
  • IX = X - I = 10 - 1 = 9

Program :
With the below program, we can covert up to the maximum value 3000. If you want to covert more than 3000, just add more roman symbols in the thousands array.


Output:

Enter a decimal : 9
Roman is : IX

Enter a decimal : 30

Roman is : XXX

Enter a decimal : 2001

Roman is : MMI

Converting Roman Numerals To Decimal

In this post we will write a java program to convert Roman Number to Decimal. Before start our program, lets see what are the roman numbers and its decimal values below.

Roman Numbers and its decimal values:
Roman Symbol
Decimal value
I
1
IV
4
V
5
IX
9
X
10
XL
40
L
50
XC
90
C
100
CD
400
D
500
CM
900
M
1000

Decimal Forming Rules:

1. When a symbol appears after a larger or equal symbol, then it is added

  • VI = V + I = 5 + 1 = 6
  • XXI = X + X + I = 10 + 10 + 1 = 21

2. But if the symbol appears before a larger symbol, the it is subtracted

  • IV = V - I = 5 - 1 = 4
  • IX = X - I = 10 - 1 = 9
Java Program:


Output:
Enter roman value : VI
It's Decimal value : 6

Enter roman value : XC
It's Decimal value : 90

Enter roman value : MCMLXXXIV
It's Decimal value : 1984