Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts

    increase by 20 for every 3 to 4 hours

    Hi,

    how do I write a code for something like this, increase by a value of 20 for every 3 hours,

    meaning..if its 3 hours = it should be 20, and if its 6 hours, it should be 40..and so on..
    I think it should be done by for loop..

  • #2
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    If currentHour is a dynamic number and this method is called every hour, something along the lines of...

    Code:
    currentHour
    number
    
    if(currentHour%3==0)
    number+=20;
    If you have currentHour and want number to be determined...

    Code:
    while(currentHour>0)
    {
         if(currentHour-3>0)
              number+=20;
         currentHour-3;
    }

  • Users who have thanked KULP for this post:

    cally0611 (03-27-2013)

  • #3
    New Coder
    Join Date
    Mar 2013
    Posts
    16
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Hey KULP,
    this is my version...with a for loop...just for beginners out there like me

    #include<iostream>
    using namespace std;

    int main()
    {
    int hours, value=0;

    cout<<"Enter the number of hours"<<"\n";
    cin>>hours;

    for(int i=1;i<=hours;i++)
    {
    if(i%3==0)
    {
    value=value+20;
    cout<<value;
    }
    }
    }


    in C++


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •