195k views
4 votes
CS 133U: C Programming ❏ Write a program that takes three integers as input: low, high, and x. The program then outputs the number of multiples of x between low and high inclusive.

The rest of the program works fine but this part with the following:
// Beginning of the for loop to count the number of multiples
// while setting i to low value
for(i=low;i // Checks if the i has a remainder (modulus result to find the
// the multiples
if(i%x==0) {
// Increments the count of multiples by 1
printf("%d %d", i, x);
count+=1;
}
}
// Ending of the for loop to count the number of multiples
// while setting i to low value
// Prints out a message stating the number of multiples
printf("Number of multiple of %d between low and high: %d", x, low, high, count);
printf("\\");
// Resets the number of multiples to 0
count = 0;

When I enter
13 45 9
I get 13 as the result. I assume it has somthing to do with the for and if statement setup. Please help.

User Rmac
by
3.7k points

1 Answer

3 votes

Answer:

#include <stdio.h>

int main() {

int low, high, x;

int count=0;

printf("Enter low : ");

scanf("%d", &low);

printf("Enter high : ");

scanf("%d", &high);

printf("Enter x : ");

scanf("%d", &x);

for (int i = low; i <= high; i++) {

if (i % x == 0) {

count++;

}

}

printf("Number of multiple of %d between low and high: %d\\", x, count);

return 0;

}

Step-by-step explanation:

In your printf statement, make sure that every %d corresponds to one argument. The first %d displays the first argument, and so on, that explains why you saw 13.

User Grafthez
by
4.3k points