Thread: Need help.

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    21

    Need help.

    So basicly I have some homework to do, useing for and then while do I need to write this
    1 x 1 = 1

    2 x 1 = 2

    3 x 1 = 3

    ...
    9 x1 = 9

    10 x1 = 10
    1 x 2 = 2
    2 x 2 = 4

    ...
    9 x 10 = 90
    10 x 10 = 100

    I managed to do it useing for but I just cant get how to do it useing while and do.

    Code:
      int main()  {
          
    int a; 
    int b;
    
    for(a = 1;a < 11; ++a ) {
       for(b = 1;b < 11; ++b) {
           int c = a*b; 
           printf(" %4d x %4d = %4d \n", a, b, c);
           
     }
     }
     return 0;
     }


    This is what I tried with while do


    Code:
    int main() {
    
    int a = 0;
    int b = 1;
    
    do
    { 
    ++a;
    int c = a*b;
    printf("%4d   X %4d = %4d \n",a, b, c);
    } 
    while(a<10); 
        
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Two loops is still two loops, so two for-loops will become two do-while loops. (Or at least, that's the most natural way to do it. If you have to, you can unroll it out to a single loop (either do/while or for), but there's no reason to do that unless specifically requested.)

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    21
    yea its said that we need to use 2 loops but tbh im super new to programming so idk what i need to change there so i can make it with while do

    edit: just got a friend to send me his result i get it now how to do it
    will update this thread later and wirte how to do it
    but thanks
    Last edited by JohnnyC; 10-24-2018 at 01:29 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, if a for loop is
    Code:
    for (initializing; checking; increment) {
        loop-body
    }
    then a do-while loop is
    Code:
    initializing
    do {
        loop-body
        increment
    } while (checking)
    with the only exception being that a do-while loop will always run at least once, while a for-loop might not. In this case we can see by inspection that the for-loops also run at least once, so that's not a problem.

Popular pages Recent additions subscribe to a feed

Tags for this Thread