Home > Net >  printing exclusive multiplication of two numbers under 100
printing exclusive multiplication of two numbers under 100

Time:09-20

3-9. Print the exclusive multiple of two numbers

After inputting two numbers, print out all the numbers from 1 to 100 that are multiples of only one of the two numbers. That is, a number that is a common multiple of the two numbers should not be output.

For example, if 15 and 20 are input, "15, 20, 30, 40, 45, 75, 80, 90, 100" is output. (60 is not in the output)

For loops, use the 'for' statement, and use variables as follows.

int num1, num2; // two numbers entered
int i; // variable for iteration

Example of execution)

Enter 2 numbers 15 20
15
20
30
40
45
75
80
90
100

I recently started to learn c programming and this is the question I have tried to solve the question but my code does not work properly can somebody help me with this question? thank you so much

this is the code I have tried

enter code here
#include <stdio.h>

int main(void) {
  int num1,num2;
  int i=1;
  int result1;
  int result2;
  printf("Enter 2 numbers \n");
  scanf("%d %d",&num1,&num2);
  for(i=1;i>0;i  ){
    if(num1*i<=100&&num2*i<=100){
      if((num1*i%num2!=0)&&(num2*i%num1!=0)){
        printf("%d\n",num1*i);
        printf("%d\n",num2*i);
        }
      else if((num2*i%num1==0)&&(num1*i%num2!=0))
        printf("%d\n",num1*i);
      else if((num1*i%num2==0)&&(num2*i%num1!=0))
        printf("%d\n",num2*i);
      
      }
    else if((num1*i>100&&num2*i<=100)&& (num2*i%num1!=0&&num1*i%num2==0))
        printf("%d\n",num2*i);
    else if((num2*i>100&&num1*i<=100)&&(num1*i%num2!=0&&num2*i%num1==0))
        printf("%d\n",num1*i);
      

CodePudding user response:

Another way: Capriciously using an array of 32 bit int's for no more than 'flags'. Merely set these flags for the 'harmonics' of one number, then sweep up the 'harmonics' of the other checking and flushing flags from the former and suppressing common values.

This consumes memory, but might be faster than quite a few base10 modulo calculations. The memory issue could be addressed with bit fields (perhaps 25 bits for 32 bit systems and 50 bits on 64 bit systems).

#include <stdio.h>
#include <string.h>

int main() {
    for( ;; ) {
        int i, ilag, n1, n2, arr[ 1   100 ]; // base 1 numbering

        printf( "Enter 2 numbers \n" );
        if( scanf( "%d %d", &n1, &n2 ) != 2
        ||  n1 == n2
        ||  !(1 < n1 && n1 <= 100)
        ||  !(1 < n2 && n2 <= 100)
            )
            exit(1);

        memset( arr, 0, sizeof arr );
        for( i = n1; i <= 100; i  = n1 ) arr[ i ] = i;
        ilag = 1;
        for( i = n2; i <= 100; i  = n2 ) {
            for( ; ilag < i; ilag   )
                if( arr[ ilag ] )
                    printf( "%d ", ilag );
            if( i != arr[ ilag   ] )
                printf( "%d ", i );
        }
        puts( "" );
    }

    return 0;
}

Output

Enter 2 numbers
15 20
15 20 30 40 45 75 80 90 100
Enter 2 numbers
6 9
6 9 12 24 27 30 42 45 48 60 63 66 78 81 84 96 99
Enter 2 numbers

CodePudding user response:

There are multiple problems in your code:

  • the end of the main function is missing
  • the loop for(i=1;i>0;i ) is an infinite loop. You should instead loop from 1 to 100 and check if the number is a multiple of num1, num2, but not both.
  • using white space after keywords, commas and semicolons and on both sides of binary operators makes the code much more readable.

Here is a modified version:

#include <stdio.h>

int main(void) {
    int i, num1, num2;
    printf("Enter 2 numbers: ");
    if (scanf("%d %d", &num1, &num2) != 2) {
        printf("invalid input\n");
        return 1;
    }
    if (num1 <= 0 || num2 <= 0) {
        printf("invalid numbers\n");
        return 1;
    }
    for (i = 1; i <= 100; i  ) {
        if (i % num1 == 0 || i % num2 == 0) {
            /* i is a multiple of num1 or num2 */
            if (!(i % num1 == 0 && i % num2 == 0)) {
                /* i is a not a multiple of both num1 and num2 */
                printf("%d\n", i);
            }
        }
    }
    return 0;
}

Here is a more compact version suggested by Gerhardh:

#include <stdio.h>

int main(void) {
    int i, num1, num2;
    printf("Enter 2 numbers: ");
    if (scanf("%d %d", &num1, &num2) != 2) {
        printf("invalid input\n");
        return 1;
    }
    if (num1 <= 0 || num2 <= 0) {
        printf("invalid numbers\n");
        return 1;
    }
    for (i = 1; i <= 100; i  ) {
        if ((i % num1 == 0) != (i % num2 == 0)) {
            /* i is a multiple of num1 or num2 but not both */
            printf("%d\n", i);
        }
    }
    return 0;
}

CodePudding user response:

chqrlie's answer shows an optimal implementation of the algorithm hinted by the problem statement.

For loops, use the 'for' statement, and use variables as follows.

int num1, num2; // two numbers entered
int i; // variable for iteration

The asker, though, seems to have tried a different approach, using i to directly calculate the multiples of num1 and num2 and somehow compare only those numbers to decide what to print.

I like the idea of considering only the multiples instead of all the numbers up to 100, but the posted (incomplete) implementation is rather confusing, without a clear ending and with an initial condition if( num1 * i <= 100 && num2 * i <= 100 ) which excludes all the results where only one of the multiples is less than the limit.

I would have introduced another variable (note, that seems not what the problem statement suggests) to simplify the code.

int const limit = 100;
for ( int i = num1, j = num2    // 'i' are the multiples of 'num1', 'j' of 'num2'.
    ; i <= limit || j <= limit  // The loop stops when BOTH are over the limit.
    ; )  // <- No increment here.
{
  if ( i < j ) {          // Note that this ensures it doesn't exceed the limit.
    printf("%d\n", i);         
    i  = num1;            // Next multiple.
  }
  else if ( j < i ) {
    printf("%d\n", j);
    j  = num2;
  }
  else {                // They may exceed the limit, here, but nothing is printed.
    i  = num1;
    j  = num2;
  }
}
  • Related