Search code examples
cformatlcm

LCM and then representing it in a matrix form


I have to find LCM and then represent them in a table. I found the LCM needed but I'm not sure how to format it on the table.

|.... 20.... 21.... 22.... 23 ....

---+--------------------------------

10 |20... 210...110... 230...

11 |220...231...22...253...

.... | etc.


#include <stdio.h>

int main(){
    int u, v, x, y, lcm, gcd, temp;

    for(x = 10; x < 20; x++){
        for(y = 20; y < 30; y++){
            if(x > y){
                v = x;
                u = y;
            }
            else{
                v = y;
                u = x;
            }

            temp = v % u;

            // GCD
            while(temp != 0){
                v = u;
                u = temp;
                temp = v % u;
            }

            gcd = u;

            // LCM
            lcm = (x * y) / gcd;

            printf("The LCM is %d\n", lcm);
        }
    }
}


Solution

  • Firstly, you print first line from 20 to 30. I using \t instead of ... because, i think it's more beautiful. You can use ... instead if you want.

        printf("|   ");
        for(y = 20; y < 30; y++) {
            printf("%d\t", y);
        }
        printf("\n---+--------------------------------\n");
        for(x = 10; x < 20; x++){
            // your code here
        }
    

    Then in double for loop:

        for(x = 10; x < 20; x++){
            printf("%d|", x);
            for(y = 20; y < 30; y++){
                if(x > y){
                    v = x;
                    u = y;
                }
               ... // your code
                // LCM
                lcm = (x * y) / gcd;
    
                printf("%d\t", lcm);
            }
            printf("\n");
        }
    

    The result of test:

    |   20  21      22      23      24      25      26      27      28      29                                              
    ---+--------------------------------                                                                                    
    10|20   210     110     230     120     50      130     270     140     290                                             
    11|220  231     22      253     264     275     286     297     308     319                                             
    12|60   84      132     276     24      300     156     108     84      348                                             
    13|260  273     286     299     312     325     26      351     364     377                                             
    14|140  42      154     322     168     350     182     378     28      406                                             
    15|60   105     330     345     120     75      390     135     420     435                                             
    16|80   336     176     368     48      400     208     432     112     464                                             
    17|340  357     374     391     408     425     442     459     476     493                                             
    18|180  126     198     414     72      450     234     54      252     522                                             
    19|380  399     418     437     456     475     494     513     532     551