Calculate the IST : Indian Standard Time

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++
// C++ implementation of the approach#include <cmath>#include <iostream>using namespace std;Â
// Function to calculate Indian Standard Timevoid cal_IST(int h, float r){Â Â Â Â float IST = (h * r * 1.0) / 360;Â
    // Separate integer part    int int_IST = (int)IST;Â
    // Separate float part and return ceil value    int float_IST = ceil((IST - int_IST) * 60);Â
    cout << int_IST << ":" << float_IST;}Â
// Driver codeint main(){Â
    // Number of hours (1 - 24)    int h = 20;Â
    // Rotations in degrees    float r = 150;Â
    cal_IST(h, r);Â
    return 0;} |
Java
// Java implementation of the approach import java.math.*;Â
class GFG{    // Function to calculate Indian Standard Time     public static void cal_IST(int h, double r)     {         double IST = (h * r * 1.0) / 360; Â
        // Separate integer part         int int_IST = (int)IST; Â
        // Separate float part and return ceil value         int float_IST = (int)Math.ceil((int)((IST - int_IST) * 60)); Â
        System.out.println(int_IST + ":" + float_IST);     } Â
    // Driver code     public static void main(String[] args)     {              // Number of hours (1 - 24)         int h = 20;              // Rotations in degrees         double r = 150;              cal_IST(h, r);     }} Â
// This code is contributed by Naman_Garg |
Python3
# Python3 implementation of the approach from math import ceilÂ
# Function to calculate Indian Standard Time def cal_IST(h, r) : Â
    IST = round((h * r * 1.0) / 360, 3); Â
    # Separate integer part     int_IST = int(IST); Â
    # Separate float part and return ceil value     float_IST = ceil((IST - int_IST) * 60); Â
    print(int_IST, ":", float_IST); Â
# Driver code if __name__ == "__main__" : Â
    # Number of hours (1 - 24)     h = 20; Â
    # Rotations in degrees     r = 150; Â
    cal_IST(h, r); Â
# This code is contributed by AnkitRai01 |
C#
// C# implementation of the approachusing System;Â
class GFG{    // Function to calculate Indian Standard Time     public static void cal_IST(int h, double r)     {         double IST = (h * r * 1.0) / 360; Â
        // Separate integer part         int int_IST = (int)IST; Â
        // Separate float part and return ceil value         int float_IST = (int)Math.Floor((                         double)(IST - int_IST) * 60); Â
        Console.WriteLine(int_IST + ":" + float_IST);     } Â
    // Driver code     public static void Main(String[] args)     {              // Number of hours (1 - 24)         int h = 20;              // Rotations in degrees         double r = 150;              cal_IST(h, r);     }} Â
// This code is contributed by PrinciRaj1992 |
Javascript
<script>// Javascript implementation of the approachÂ
// Function to calculate Indian Standard Timefunction cal_IST(h, r){Â Â Â Â let IST = (h * r * 1.0) / 360;Â
    // Separate integer part    let int_IST = parseInt(IST);Â
    // Separate float part and return ceil value    let float_IST = Math.ceil(parseInt((IST - int_IST) * 60));Â
    document.write(int_IST + ":" + float_IST);}Â
// Driver codeÂ
    // Number of hours (1 - 24)    let h = 20;Â
    // Rotations in degrees    let r = 150;Â
    cal_IST(h, r);Â
// This code is contributed by subhammahato348.</script> |
8:20
Â
Time Complexity: O(1)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



