w3resource logo


C Programming Exercises

C Exercises: Calculates the total number of minutes with hour and minutes

Secondary Nav

C Input Output statement: Exercise-5 with Solution

Write a C program that takes hours and minutes as input, and calculates the total number of minutes.

Expected Output :

Input hours: 5                                                                                                
Input minutes: 37                                                                                             
Total: 337 minutes.             

C Code:

#include <stdio.h>

int hrs;          /* given number of hours */
int mins;         /* given number of minutes */
int tot_mins;     /* total number of minutes (to be computed) */

const int MINaHOUR = 60;      /* number of minutes in an hour */

char line_text[50];      /* line of input from keyboard */

int main() {
	printf("Input hours: ");
	fgets(line_text, sizeof(line_text), stdin);
	sscanf(line_text, "%d", &hrs);

	printf("Input minutes: ");
	fgets(line_text, sizeof(line_text), stdin);
	sscanf(line_text, "%d", &mins);

	tot_mins = mins + (hrs * MINaHOUR);

	printf("Total: %d minutes.\n", tot_mins);

	return(0);
}

Solution

#include <stdio.h>

int hrs;          /* given number of hours */
int mins;         /* given number of minutes */
int tot_mins;     /* total number of minutes (to be computed) */

const int MINaHOUR = 60;      /* number of minutes in an hour */

char line_text[50];      /* line of input from keyboard */

int main() {
	printf("Input hours: ");
	fgets(line_text, sizeof(line_text), stdin);
	sscanf(line_text, "%d", &hrs);

	printf("Input minutes: ");
	fgets(line_text, sizeof(line_text), stdin);
	sscanf(line_text, "%d", &mins);

	tot_mins = mins + (hrs * MINaHOUR);

	printf("Total: %d minutes.\n", tot_mins);

	return(0);
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Java: How to convert a string to an integer in Java?

C#: Loops in c#

SQL: JOIN using more than 5 tables