Help with C program

5.17 (Multiples) Write a function isMultiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true ) if the second is a multiple of the first, and 0 (false ) otherwise. Use this function in a program
that inputs a series of pairs of integers .

Example of what the output should be:

Enter·two·integers:45·5↵
·45·is·a·multiple·of·5↵
Enter·two·integers:59·8↵
·59·is·not·a·multiple·of·8↵
Enter·two·integers:12·54↵
·12·is·not·a·multiple·of·54↵


The issue seems to be the fact that my attempts only do one output rather than multiple and im not entirely sure how to fix that so it continues to repeat.


Attempt 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <stdio.h>

char remainderZeroOrNot(int a,int b);

int main(void) {

int a,b;

printf("Enter two integers: ");
scanf("%d %d",&a,&b);

remainderZeroOrNot(a,b);

}

char remainderZeroOrNot(int a,int b){

if (a%b==0)
return printf("%d is a multiple of %d",a,b);
else
return printf("%d is not a multiple of %d",a,b);

}



Attempt 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdio.h>

int remainderZeroOrNot(int ,int );

int main(void) {

int a,b;

printf("Enter two integers: ");
scanf("%d %d",&a,&b);

remainderZeroOrNot(a,b);

}

int remainderZeroOrNot(int a,int b){

if (b%a==0)
printf("%d is a multiple of %d",a,b);
else
printf("%d is not a multiple of %d",a,b);
return !(b%a);

}
Last edited on
The function should take two integer arguments and return 1 (true ) if the second is a multiple of the first, and 0 (false ) otherwise.

Where in that does it say that the function isMultiple should do anything else than to return 0 or 1?
int isMultiple( int first, int second );

Use this function in a program that inputs a series of pairs of integers

The main() should thus have a:
LOOP
  get a and b
  result = isMultiple( a, b );
  if ( 1 == result ) print "foo"
  else /* 0 == result */ print "bar"
Topic archived. No new replies allowed.