How can this while loop be modified into a for loop In C int
How can this while loop be modified into a for loop? (In C++)
int pushup;
int energy;
pushup=0;
cout<<\"Please enter your level of energy (in number units): \";
cin>>energy;
while ( energy>=2)
{
pushup++;
energy-=2;
}
cout<<\"Pushup=\"<<pushup<<endl;
Solution
Solution.cpp
#include <iostream>//header file for input output function
using namespace std;//it tells the compiler to link std namespace
int main()
{//main fucntion
int pushup;//variable declaration
int energy;
pushup=0;
cout<<\"Please enter your level of energy (in number units): \";
cin>>energy;//key board inputting
for(;energy>=2;energy=energy-2)
{//for loop
pushup++;
}
cout<<\"Pushup=\"<<pushup<<endl;
return 0;
}
output
Please enter your level of energy (in number units): 9
Pushup=4
Please enter your level of energy (in number units): 0
Pushup=0
