Program opened below. You can scroll through the algorithm, Java code and output.

Sum of Two Times

Download .java

Question

Accept two times in hours and minutes and display their sum in normalised form.

Step 1: Start the program and accept the hours and minutes of the first time. Step 2: Accept the hours and minutes of the second time. Step 3: Add the two minute values. Step 4: Divide the total minutes by 60 to determine the number of carried hours. Step 5: Use the remainder after division by 60 as the final minutes. Step 6: Add the hours of both times and the carried hours. Step 7: Store the normalised total hours and minutes. Step 8: Display the sum in hours and minutes on a separate output line. Step 9: Stop the program. Step 10: Display the calculated result clearly and stop the program.
Java
import java.util.*;
class SumOfTimes
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter first time (hours minutes): ");
        int h1 = sc.nextInt(), m1 = sc.nextInt();
        System.out.print("Enter second time (hours minutes): ");
        int h2 = sc.nextInt(), m2 = sc.nextInt();
        int minutes = m1 + m2;
        int hours = h1 + h2 + minutes / 60;
        minutes = minutes % 60;
        System.out.println("Sum = " + hours + " hours " + minutes + " minutes");
    }
}
Input: 6 35; 3 45 Output: Sum = 10 hours 20 minutes
← Previous Program 46 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.