c++ for loop converted to a while loop

后端 未结 3 1016
一生所求
一生所求 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:27

    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++;
    
        }
    
    0 讨论(0)
  • 2021-01-29 09:37
    int i = 1;
    while(i < integer + 1) {
        // your current loop body goes here
        i++;
    }
    

    See equivalent forms of for loop.

    0 讨论(0)
  • 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<<" ";
    }
    
    0 讨论(0)
提交回复
热议问题