Arrays & Hashing

StringChallange

Estimated reading: 1 minute 50 views

String Challenge:
Write a function StringChallenge(num) that takes the num parameter (representing minutes) and returns a string representing the equivalent number of hours and minutes. The result should separate hours and minutes with a colon.

  • Example 1:
    Input: 126
    Output: 2:6

  • Example 2:
    Input: 45
    Output: 0:45

				
					public class StringChallenge {

    public static String stringChallenge(int num) {
        // Calculate hours
        int hours = num / 60;
        // Calculate remaining minutes
        int minutes = num % 60;
        // Return the result in the format of hours:minutes
        return hours + ":" + (minutes < 10 ? "0" + minutes : minutes);
    }

    public static void main(String[] args) {
        // Test cases
        System.out.println(stringChallenge(126)); // Expected Output: 2:06
        System.out.println(stringChallenge(45));  // Expected Output: 0:45
        System.out.println(stringChallenge(150)); // Expected Output: 2:30
        System.out.println(stringChallenge(0));   // Expected Output: 0:00
    }
}

				
			
Share this Doc

StringChallange

Or copy link

CONTENTS