c++ for loop converted to a while loop

后端 未结 3 1018
一生所求
一生所求 2021-01-29 09:11

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++)
{
           


        
3条回答
  •  借酒劲吻你
    2021-01-29 09:43

    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<<" ";
    }
    

提交回复
热议问题