0
please help me out with this
how to rewrite the following program using while loop include <iostream.h> using namespace std; int main() { int k=6; while (k>=0) { cout<<k<<endl; if (k>4) cout<<2*k<<endl; cout<<k<<endl; k--; } return 0; }
14 Answers
+ 2
How did you expect the code to work?
If you see a need for reuse of code (or blocks of code), then you write a function based on the loop workflow. You can afterwards use the function wherever you need.
+ 2
I don't know how to help you this way unfortunately
Good luck! 👍
+ 1
You're already using while loop there! look at line 2 in function main ...
+ 1
Share the actual instruction here. There should be a criteria on rewriting the code. Like "rewrite this using blah so that it does blah etc etc"
0
I know but is there any way to reuse it differently?
0
Try "for" loop
0
include <iostream.h>
using namespace std;
int main()
{
int k=6;
for (; k >= 0; k--)
{
cout<<k<<endl;
if (k>4)
cout<<2*k<<endl;
cout<<k<<endl;
}
return 0;
}
0
it's a quiz and it asks to rewrite this code using while loop
how I'm going to put 'for' instead 😭💀
0
But you already have "while" loop😳😳
0
I've never write any code using C++ lang so I have no idea how I'm going to rewrite it
0
include <iostream.h>
using namespace std;
int main()
{
int k=0
while (k<=6)
{
cout<<k<<endl;
if (k>4)
cout<<2*k<<endl;
cout<<k<<endl;
k++;
}
return 0;
}
0
Try this😳😳
0
Try this out
while(k--) {
cout<<k<<endl;
If(k>4)cout<<2*k<<endl:
cout<<k<<endl;
}
0
This is going to be finite loop!! Can u post the question so it'll be easy to solve btw coming to your point u can use any of the loop either for or while that depends on you!! using for loop :
for ( , k>=0 , k--)
{
Cout<<k;
if (k>4)
{
Cout<<k*2 <<endl;
}
}
Using while :
While(k>0)
{
Cout<<k<<endl;
If(k>4) {
Cout<<k*2;
K--;
}
}