-4
#include <stdio.h>
int main()
{
    int n, range, i;
    printf("Enter an integer to find multiplication table: ");
    scanf("%d",&n);
    printf("Enter range of multiplication table:");
    scanf("%d",&range);
    for(i=1;i<=range;++i)
{
    printf("%d * %d = %d\n", n, i, n*i);
}
return 0;

}

I need to display odd next to multiples that are odd and even next to even multiples.

Muroo
  • 1
  • 3

3 Answers3

1

Use the modulo

if ((n*i)%2 == 0)
  printf("EVEN : %d * %d = %d\n", n, i, n*i);
else
  printf("ODD : %d * %d = %d\n", n, i, n*i);
Yann
  • 318
  • 1
  • 14
1

Modify your printf() statement to:

printf ( "%d * %d = %d : %s\n", n, i, n*i, (n*i % 2 == 0) ? "EVEN" : "ODD" );

The modulus operator gives the remainder of n divided by 2.

Mahesh Bansod
  • 1,493
  • 2
  • 24
  • 42
  • (n*i) is the one that needs to be checked, I was thinking of replacing the "i" in the code with "(n*i)" , will test it when I go home and see if it works. – Muroo Jun 16 '14 at 10:20
  • I have editted the code and replaced 'i' to 'n*i'. I hope it works. – Mahesh Bansod Jun 16 '14 at 14:36
1

The best way to check if a number is even or odd is by using bitwise operators.

if (i & 1) { // It's odd }

else { //it is even }

PG1
  • 1,220
  • 2
  • 12
  • 27