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 8723637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:40:18+00:00 2026-06-13T07:40:18+00:00

I am writing a C program to take in a value representing a time

  • 0

I am writing a C program to take in a value representing a time of the day in 24 hour format, and a time duration in similar format, positive or negative. This program should calculate the 24 hour time after the duration (that is, beginning + duration = end). For example, 1345 and 345 would output 1730.

However, I have no idea how to handle improper inputs. For example, values like 2372 or 2520 shouldn’t be accepted. As well, if I have (for example) 100 and -200, I should get 2300 as output instead of -100. In short, I know that the first two numbers should be mod 24, and the last two numbers should be mod 60, but I don’t know how to apply these operations to the input. Can anyone offer any tips on how I can do this?

  • 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-13T07:40:19+00:00Added an answer on June 13, 2026 at 7:40 am

    Read the input as an int, separate the minutes from the hours and check both of them:

    int tm, h, m, is_negative = 0;
    scanf("%d", &tm);
    
    if (tm < 0) {
        is_negative = 1;
        tm = -tm;
    }
    
    m = tm % 100; // separates the two last digits
    h = tm / 100; // excludes the two last digits
    
    if (m > 59) {
        handle_error();
    }
    
    if (h > 23) {
        handle_error();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im writing a program that should read input via stdin, so I have the
So basically, I'm writing a program to take in a set of data and
I'm writing a program that is supposed to take in a list of names
I am writing a program that needs to take an array of size n
I'm writing a program for an ATMega328P that will take readings from several ADC
I just started learning Python... I am writing a simple program that will take
The program that I am writing needs to do this: read every line of
So in this program I'm writing, I actually grab a SQL query from the
I am writing a C-program where I need 2D-arrays (dynamically allocated) with negative indices
I am writing a program that can take in either 3 ints, or 3

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.