Hi I wrote this code to print out factors of an integer with a for looop
how do i write it with a while loop?
for(int i = 1; i < integer+1; i++)
{
Following code will print the same result as your for loop in printing.
int i = 1;
while(i < integer + 1)
{
if(integer % i == 0)
{ cout<< i<<" "; }
i++;
}
int i = 1;
while(i < integer + 1) {
// your current loop body goes here
i++;
}
See equivalent forms of for loop.
int i = 1;
while (i < integer+1)
{
if(integer % i == 0)
cout<< i<<" ";
i++;
}
Or even better:
int i = 0;
while (++i < integer+1)
{
if(integer % i == 0)
cout<< i<<" ";
}