Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9241061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:18:03+00:00 2026-06-18T08:18:03+00:00

I know that maybe its a silly question but I have some troubles with

  • 0

I know that maybe its a silly question but I have some troubles with it, I’m ashamed but I really don’t know, how to make it. I want to add and subtract two ‘hours’ given as integers.

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
    // should be: 21:59:2
    int rH, rM, rS;

    int h1 = 12, m1 = 34, s1 = 56;
    int h2 = 9, m2 = 24, s2 = 6;

    if(h1 + h2 >= 24)
    {
        rH = abs(h2 + h1);
        m1 += 60;

        if(m1 + m2 >= 60)
        {
            rH = (m2 - m1);
        }

    }
    else
    {
        rH = h1 + h2;
    }

    cout << rH << " " << rM << " " << rS << " " << "\n";

    return 0;
}

Tried the above but I don’t know how to do it further. I cant use any date-time objects.

EDIT

subtraction, I don’t know where the error is but it calculates wrong:

#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;

int main()
{
    int rH, rM, rS;
    int h1 = 12, m1 = 25, s1 = 29, h2 = 11, m2 = 12, s2 = 1;
    int sum1, sum2, sum;

    sum1 = (h1)*3600 + (m1)*60 + s1;
    sum2 = (h2)*3600 + (m2)*60 + s2;

    if(sum1 > sum2)
        sum = sum1-sum2;
    else
        sum = sum2-sum1;

    rS = sum %60;
    sum = sum/60;
    rM = sum % 60;
    rH = sum/60;

    std::cout << rH << " " << rM << " " << rS << " " << "\n";

    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T08:18:04+00:00Added an answer on June 18, 2026 at 8:18 am
    // addition
    int rH=0, rM=0, rS=0; 
    
    rS = s1+s2;
    if (rS>60)
    {
        rM++;
        rS %= 60;
    }
    
    rM += m1+m2;
    if (rM>60)
    {
        rH++;
        rM %= 60;
    }
    
    rH += h1+h2;
    if (rH>24)
    {
        // 1 day more, who cares
        rH %= 24;
    }
    
    cout << rH << " " << rM << " " << rS << " " << "\n";
    

    // substraction

    int rH = 0, rM = 0, rS = 0;
    int h1 = 12, m1 = 25, s1 = 29, h2 = 11, m2 = 12, s2 = 1;

    rS = s1 - s2;
    if (rS<0)
    {
        rM--;
        rS += 60;
    }
    
    rM += m1 - m2;
    if (rM<0)
    {
        rH--;
        rM += 60;
    }
    
    rH += h1 - h2;
    if (rH<0)
    {
        // Error occurred ...
    }
    
    cout << rH << " " << rM << " " << rS << " " << "\n";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that Java have its own garbage collection, but sometimes I want to
It's maybe silly question. But I'm new in Linux developing. And I don't know
I know that this may be an amateur question but for some reason I
Forgive me if this is a silly question but I'm afraid that I don't
It's a strange and silly question I know, but I'm curious because I don't
I know that maybe this question has been asked before, but I can't seem
Maybe a silly question but here goes anyway. Example: Let's say I have one
Does anyone know if Python's shelve module uses memory-mapped IO? Maybe that question is
I know that this sort of question has been asked here before, but still
This may be a silly question but... Say you have a sentence like: The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.