C++ tutorial, C++ programs, C++ Exercises, C++ Example Programs

Tuesday, June 10, 2014

C Plus Plus Program Conversion of Hours into Weeks Days Hours

Today, we are going to discuss a simple program logic that uses only input, output and assignment statements along with variable declarations.

Points of Discussion are:
Easyway How to write c++ program convert hours into weeks days hours
Easyway How to write c++ program convert hours into weeks days hours

  • C++ Program: Input Hours Convert into Weeks, Days and Hours.
  • Algorithm of the Hours Conversion Program
  • C++ Code of the Hours Conversion Program.
  • Understanding Logic of The Hours into Week, Day and Hours Program
C Plus Plus to convert hours into weeks days and hours
C Plus Plus to convert hours into weeks days and hours

ALGORITHM : Hours Conversion

This algorithm is used to input hours and convert into weeks, days and hours.
  1. Start
  2. Input Hours
  3. Calculate Weeks = Hours / 168
  4. Calculate Hours = Hours Mod 168
  5. Calculate Days = Hours / 24
  6. Calculate Hours = Hours Mod 24
  7. Display "Weeks=", Weeks
  8. Display "Days=", Days
  9. Display "Hours =", Hours
  10. End

C++ Code: Program Hours Conversion into Weeks, Days and Hours.

/* This program is used to input hours from the user. Then this algorithm "Hours Conversion" will convert hours into weeks, days and hours.*/
#include<iostream.h>
#include<conio.h>

void main()
{
      clrscr();
      int h, w, d, hrs;
      cout<<"Enter Hours=";
      cin>>hrs;
      h = hrs;
      w = hrs / 168;
      hrs = hrs % 168;
      d = hrs / 24;
      hrs = hrs % 24;
      cout<<"Total Hours input = "<<h<<endl;
      cout<<"Weeks ="<<w<<endl;
      cout<<"Days ="<<d<<endl;
      cout<<"Hours ="<<hrs;
      getch();
}

OUTPUT of Sample Run of The Program Hours Conversion


Enter Hours= 200
Total Hours input =200
Weeks = 1
Days =1
Hours =8


Logic of The Program Hours Conversion


We know that:
1 day = 24 hours
so 1 week = 24 x 7 = 168 hours

Therefore we will divide total hours by 168 to get weeks.
weeks = 200 / 168 will give 1 week.
Now to calculate remaining hours we will get remainder of the division 200 % 168
This will give remaining hours = 32

To get days, we will divide remaining hours by 24
so 32 / 24 will give 1 day.

Now we will calculate remaining hours by remainder 32 % 24 that will give the result 8 hours.

So the output 1 week, 1 day and 8 hours.

If you enjoyed the C++ Learning Through Example program Logic Understanding, Please share this blog on Social media. Thanks!
Share:

EasyCPPprogramming.blogspotcom

Labels