Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

printing exclusive multiplication of two numbers under 100

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)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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);
      

>Solution :

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;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading