C Plus Plus Program Conversion of Hours into Weeks Days Hours - Download Turbo C Language and C++ IDE and Programs Free

Latest

Perfect Collection of C Language and C++ Programs

BANNER 728X90

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:
  • 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


ALGORITHM : Hours Conversion


  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.


#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!