Posted on by Kalkicode
Code Probability

# Find angle between hour and minute hand

To find the angle between the hour and minute hand on an analog clock, you can use a simple mathematical formula. Here are the steps to write a program that calculates this angle:

1. Take input from the user for the current time in the format of hours and minutes.
2. Calculate the position of the hour hand. Each hour on the clock represents 30 degrees, so you can multiply the current hour by 30 to get the position of the hour hand. However, if the hour is greater than 12, subtract 12 before multiplying by 30.
3. Calculate the position of the minute hand. Each minute on the clock represents 6 degrees, so you can multiply the current minute by 6 to get the position of the minute hand.
4. Calculate the angle between the hour and minute hand. To do this, you can subtract the position of the hour hand from the position of the minute hand. However, if the result is greater than 180 degrees, subtract it from 360 degrees instead.
5. Output the result to the user.

## Code Solution

``````/*
Java program for
Find angle between hour and minute hand
*/
public class Angle
{
// Calculate angle by given position
public int angleValue(int h, int m)
{
int x = h - m;
if (x < 0)
{
x = -x;
}
if (x > 180)
{
x = 360 - x;
}
return x;
}
public void findAngle(int hour, int minute)
{
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
{
return;
}
// Calculate position of hour
int h = (hour * 360) / 12 + (minute * 360) / (12 * 60);
// Calculate position of minute
int m = (minute * 360) / (60);
int result = angleValue(h, m);
// Display given hour and minute
System.out.println(" Given hour   : " + hour);
System.out.println(" Given minute : " + minute);
// Display calculate angle
System.out.println(" Angle : " + result);
}
public static void main(String[] args)
{
/*
⑫
⑪         ➀

➓               ❷

➈        ✧        ➂

➇               ➃

➆        ➄
➅

hour : 10 , minute : 10
*/
/*
⑫
⑪         ➀

➉               ➁

➈        ✧        ➂

➇               ❹

➐         ➄
➅

hour : 7 , minute : 20
*/
}
}``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program for
Find angle between hour and minute hand
*/
class Angle
{
public:
// Calculate angle by given position
int angleValue(int h, int m)
{
int x = h - m;
if (x < 0)
{
x = -x;
}
if (x > 180)
{
x = 360 - x;
}
return x;
}
void findAngle(int hour, int minute)
{
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
{
return;
}
// Calculate position of hour
int h = (hour *360) / 12 + (minute *360) / (12 *60);
// Calculate position of minute
int m = (minute *360) / (60);
int result = this->angleValue(h, m);
// Display given hour and minute
cout << " Given hour   : " << hour << endl;
cout << " Given minute : " << minute << endl;
// Display calculate angle
cout << " Angle : " << result << endl;
}
};
int main()
{
/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10
*/
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20
*/
return 0;
}``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````
``````// Include namespace system
using System;
/*
Csharp program for
Find angle between hour and minute hand
*/
public class Angle
{
// Calculate angle by given position
public int angleValue(int h, int m)
{
int x = h - m;
if (x < 0)
{
x = -x;
}
if (x > 180)
{
x = 360 - x;
}
return x;
}
public void findAngle(int hour, int minute)
{
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
{
return;
}
// Calculate position of hour
int h = (hour * 360) / 12 + (minute * 360) / (12 * 60);
// Calculate position of minute
int m = (minute * 360) / (60);
int result = this.angleValue(h, m);
// Display given hour and minute
Console.WriteLine(" Given hour   : " + hour);
Console.WriteLine(" Given minute : " + minute);
// Display calculate angle
Console.WriteLine(" Angle : " + result);
}
public static void Main(String[] args)
{
/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10
*/
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20
*/
}
}``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````
``````package main
import "fmt"
/*
Go program for
Find angle between hour and minute hand
*/

// Calculate angle by given position
func angleValue(h, m int) int {
var x int = h - m
if x < 0 {
x = -x
}
if x > 180 {
x = 360 - x
}
return x
}
func findAngle(hour, minute int) {
if hour < 0 || hour > 24 || minute < 0 || minute > 60 {
return
}
// Calculate position of hour
var h int = (hour * 360) / 12 + (minute * 360) / (12 * 60)
// Calculate position of minute
var m int = (minute * 360) / (60)
var result int = angleValue(h, m)
// Display given hour and minute
fmt.Println(" Given hour   : ", hour)
fmt.Println(" Given minute : ", minute)
// Display calculate angle
fmt.Println(" Angle : ", result)
}
func main() {

/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10

*/
findAngle(10, 10)
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20

*/
findAngle(7, 20)
}``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````
``````/*
Node JS program for
Find angle between hour and minute hand
*/
class Angle
{
// Calculate angle by given position
angleValue(h, m)
{
var x = h - m;
if (x < 0)
{
x = -x;
}
if (x > 180)
{
x = 360 - x;
}
return x;
}
findAngle(hour, minute)
{
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
{
return;
}
// Calculate position of hour
var h = parseInt((hour * 360) / 12) +
parseInt((minute * 360) / (12 * 60));
// Calculate position of minute
var m = parseInt((minute * 360) / (60));
var result = this.angleValue(h, m);
// Display given hour and minute
console.log(" Given hour   : " + hour);
console.log(" Given minute : " + minute);
// Display calculate angle
console.log(" Angle : " + result);
}
}

function main()
{
/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10
*/
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20
*/
}
main();``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````
``````<?php
/*
Php program for
Find angle between hour and minute hand
*/
class Angle
{
// Calculate angle by given position
public	function angleValue(\$h, \$m)
{
\$x = \$h - \$m;
if (\$x < 0)
{
\$x = -\$x;
}
if (\$x > 180)
{
\$x = 360 - \$x;
}
return \$x;
}
public	function findAngle(\$hour, \$minute)
{
if (\$hour < 0 || \$hour > 24 || \$minute < 0 || \$minute > 60)
{
return;
}
// Calculate position of hour
\$h = (int)((\$hour * 360) / 12) +
(int)((\$minute * 360) / (12 * 60));
// Calculate position of minute
\$m = (int)((\$minute * 360) / (60));
\$result = \$this->angleValue(\$h, \$m);
// Display given hour and minute
echo(" Given hour   : ".\$hour.
"\n");
echo(" Given minute : ".\$minute.
"\n");
// Display calculate angle
echo(" Angle : ".\$result.
"\n");
}
}

function main()
{
/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10
*/
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20
*/
}
main();``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````
``````#    Python 3 program for
#    Find angle between hour and minute hand
class Angle :
#  Calculate angle by given position
def angleValue(self, h, m) :
x = h - m
if (x < 0) :
x = -x

if (x > 180) :
x = 360 - x

return x

def findAngle(self, hour, minute) :
if (hour < 0 or hour > 24 or minute < 0 or minute > 60) :
return

#  Calculate position of hour
h = int((hour * 360) / 12) + int((minute * 360) / (12 * 60))
#  Calculate position of minute
m = int((minute * 360) / (60))
result = self.angleValue(h, m)
#  Display given hour and minute
print(" Given hour   : ", hour)
print(" Given minute : ", minute)
#  Display calculate angle
print(" Angle : ", result)

def main() :
#               ⑫
#         ⑪         ➀
#       ➓              ❷
#      ➈        ✧       ➂
#      ➇               ➃
#          ➆        ➄
#               ➅
#        hour : 10 , minute : 10
#               ⑫
#         ⑪         ➀
#       ➉              ➁
#      ➈        ✧       ➂
#      ➇               ❹
#         ➐         ➄
#               ➅
#        hour : 7 , minute : 20

if __name__ == "__main__": main()``````

#### Output

`````` Given hour   :  10
Given minute :  10
Angle :  115
Given hour   :  7
Given minute :  20
Angle :  100``````
``````#    Ruby program for
#    Find angle between hour and minute hand
class Angle
#  Calculate angle by given position
def angleValue(h, m)
x = h - m
if (x < 0)
x = -x
end

if (x > 180)
x = 360 - x
end

return x
end

def findAngle(hour, minute)
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
return
end

#  Calculate position of hour
h = (hour * 360) / 12 + (minute * 360) / (12 * 60)
#  Calculate position of minute
m = (minute * 360) / (60)
result = self.angleValue(h, m)
#  Display given hour and minute
print(" Given hour   : ", hour, "\n")
print(" Given minute : ", minute, "\n")
#  Display calculate angle
print(" Angle : ", result, "\n")
end

end

def main()
#               ⑫
#         ⑪         ➀
#       ➓               ❷
#      ➈        ✧        ➂
#      ➇               ➃
#          ➆        ➄
#               ➅
#        hour : 10 , minute : 10
#               ⑫
#         ⑪         ➀
#       ➉               ➁
#      ➈        ✧        ➂
#      ➇               ❹
#         ➐         ➄
#               ➅
#        hour : 7 , minute : 20
end

main()``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100
``````
``````/*
Scala program for
Find angle between hour and minute hand
*/
class Angle()
{
// Calculate angle by given position
def angleValue(h: Int, m: Int): Int = {
var x: Int = h - m;
if (x < 0)
{
x = -x;
}
if (x > 180)
{
x = 360 - x;
}
return x;
}
def findAngle(hour: Int, minute: Int): Unit = {
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
{
return;
}
// Calculate position of hour
var h: Int = (hour * 360) / 12 + (minute * 360) / (12 * 60);
// Calculate position of minute
var m: Int = (minute * 360) / (60);
var result: Int = angleValue(h, m);
// Display given hour and minute
println(" Given hour   : " + hour);
println(" Given minute : " + minute);
// Display calculate angle
println(" Angle : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Angle = new Angle();
/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10

*/
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20

*/
}
}``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````
``````/*
Swift 4 program for
Find angle between hour and minute hand
*/
class Angle
{
// Calculate angle by given position
func angleValue(_ h: Int, _ m: Int) -> Int
{
var x: Int = h - m;
if (x < 0)
{
x = -x;
}
if (x > 180)
{
x = 360 - x;
}
return x;
}
func findAngle(_ hour: Int, _ minute: Int)
{
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
{
return;
}
// Calculate position of hour
let h: Int = (hour * 360) / 12 + (minute * 360) / (12 * 60);
// Calculate position of minute
let m: Int = (minute * 360) / (60);
let result: Int = self.angleValue(h, m);
// Display given hour and minute
print(" Given hour   : ", hour);
print(" Given minute : ", minute);
// Display calculate angle
print(" Angle : ", result);
}
}
func main()
{
/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10

*/
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20

*/
}
main();``````

#### Output

`````` Given hour   :  10
Given minute :  10
Angle :  115
Given hour   :  7
Given minute :  20
Angle :  100``````
``````/*
Kotlin program for
Find angle between hour and minute hand
*/
class Angle
{
// Calculate angle by given position
fun angleValue(h: Int, m: Int): Int
{
var x: Int = h - m;
if (x < 0)
{
x = -x;
}
if (x > 180)
{
x = 360 - x;
}
return x;
}
fun findAngle(hour: Int, minute: Int): Unit
{
if (hour < 0 || hour > 24 || minute < 0 || minute > 60)
{
return;
}
// Calculate position of hour
val h: Int = (hour * 360) / 12 + (minute * 360) / (12 * 60);
// Calculate position of minute
val m: Int = (minute * 360) / (60);
val result: Int = this.angleValue(h, m);
// Display given hour and minute
println(" Given hour   : " + hour);
println(" Given minute : " + minute);
// Display calculate angle
println(" Angle : " + result);
}
}
fun main(args: Array < String > ): Unit
{
/*
⑫
⑪         ➀
➓               ❷

➈        ✧        ➂

➇               ➃
➆        ➄
➅
hour : 10 , minute : 10

*/
/*
⑫
⑪         ➀
➉               ➁

➈        ✧        ➂

➇               ❹
➐         ➄
➅
hour : 7 , minute : 20

*/
}``````

#### Output

`````` Given hour   : 10
Given minute : 10
Angle : 115
Given hour   : 7
Given minute : 20
Angle : 100``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post