0

I want to know how the modulo operator is used here

Input and output are as follows:

Input : 6

1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 0
6 7 8 9 0 1
#include <stdio.h>

int main(){
    
    int i, j, n;
    printf("Input n: ");
    scanf("%d", &n);
 
    for(i=0; i<n; i++)
    {
        for (j=1; j<=n; j++)
        {
            printf ("%d ", (i+j)%10);
        }
        printf("\n");
    }
return 0;
}
Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    It's used to keep the output between numbers 0-9 and that's about it. If you know how to calculate (i+j)/10 then you know how to calculate (i+j)%10 as well. – Lundin Dec 06 '22 at 09:35
  • OT: Strictly speaking `%` is **not** a modulo operator. Instead it calculates the remainder. See https://stackoverflow.com/questions/13683563/whats-the-difference-between-mod-and-remainder – Support Ukraine Dec 06 '22 at 10:02
  • I'm not really sure what your question is so I'm not sure this will help but... `printf ("%d ", (i+j)%10);` will generate the same as `int x = (i+j) - ((i+j) / 10) * 10; printf ("%d ", x);` In other words... `n % 10` will give a number in the range 0..9 (when n is positive). – Support Ukraine Dec 06 '22 at 10:20

1 Answers1

1

The expression (i+j)%10 calculates i+j modulo 10, which means taking the last digit of the sum of i and j.

Dominique
  • 16,450
  • 15
  • 56
  • 112