...

View Full Version : increase by 20 for every 3 to 4 hours



cally0611
03-26-2013, 06:06 AM
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..

KULP
03-26-2013, 06:26 AM
If currentHour is a dynamic number and this method is called every hour, something along the lines of...



currentHour
number

if(currentHour%3==0)
number+=20;


If you have currentHour and want number to be determined...



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

cally0611
03-27-2013, 08:51 AM
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++



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum