Given two integer H and R where H is the time in hours at a place X and R is the distance in degrees from place X to India, the task is to find the current time in IST.
UTC (Coordinated Universal Time) is a 24-hour time standard that is used to synchronize world clocks.
Examples:
Input: H = 24, R = 82.50
Output: IST = 5:30
IST = (24/360)* 82.50
= 5.5
= 0.5*60 (i.e 60 minute = 360 degree rotation and 1 minute = 6 degree so, 0.5 hour * 60 = 30)
IST = 5:30
Input: H = 20, R = 150
Output: IST = 8:20
Approach:
- It is known that 1 hour = 60 minute = 360 degree rotation.
- 1 degree rotation = (1 / 360) hour.
- 2 degree rotation = (1 / 360) * 2 hour.
- So, Generalised formula will be:
IST = UTC + (H / 360) * R (UTC = 0 for IST)
IST = ( H / 360 ) * R
The answer will be converted in 0:00 form so int part (hour) and float part in IST are separated and the float part is multiplied by 60 to convert it into minutes.
Below is the implementation of the above approach:
C++
#include <cmath>
#include <iostream>
using namespace std;
void cal_IST( int h, float r)
{
float IST = (h * r * 1.0) / 360;
int int_IST = ( int )IST;
int float_IST = ceil ((IST - int_IST) * 60);
cout << int_IST << ":" << float_IST;
}
int main()
{
int h = 20;
float r = 150;
cal_IST(h, r);
return 0;
}
|
Java
import java.math.*;
class GFG
{
public static void cal_IST( int h, double r)
{
double IST = (h * r * 1.0 ) / 360 ;
int int_IST = ( int )IST;
int float_IST = ( int )Math.ceil(( int )((IST - int_IST) * 60 ));
System.out.println(int_IST + ":" + float_IST);
}
public static void main(String[] args)
{
int h = 20 ;
double r = 150 ;
cal_IST(h, r);
}
}
|
Python3
from math import ceil
def cal_IST(h, r) :
IST = round ((h * r * 1.0 ) / 360 , 3 );
int_IST = int (IST);
float_IST = ceil((IST - int_IST) * 60 );
print (int_IST, ":" , float_IST);
if __name__ = = "__main__" :
h = 20 ;
r = 150 ;
cal_IST(h, r);
|
C#
using System;
class GFG
{
public static void cal_IST( int h, double r)
{
double IST = (h * r * 1.0) / 360;
int int_IST = ( int )IST;
int float_IST = ( int )Math.Floor((
double )(IST - int_IST) * 60);
Console.WriteLine(int_IST + ":" + float_IST);
}
public static void Main(String[] args)
{
int h = 20;
double r = 150;
cal_IST(h, r);
}
}
|
Javascript
<script>
function cal_IST(h, r)
{
let IST = (h * r * 1.0) / 360;
let int_IST = parseInt(IST);
let float_IST = Math.ceil(parseInt((IST - int_IST) * 60));
document.write(int_IST + ":" + float_IST);
}
let h = 20;
let r = 150;
cal_IST(h, r);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Mar, 2022
Like Article
Save Article