w3resource

C Exercises: Reads in two integers and check whether the first integer is a multiple of the second integer


Check if the first integer is a multiple of the second

Write a C program that reads two integers and checks whether the first integer is a multiple of the second integer.

Sample Input: 9 3

Sample Solution:

C Code:

#include<stdio.h>
// Function to check if n1 is a multiple of n2
int is_Multiple(int n1, int n2)
{
    return n1 % n2 == 0;
}

int main()
{
    int n1, n2;
   
    // Prompt for user input
    printf("Input the first integer : ");
    scanf("%d", &n1);
    printf("Input the second integer: ");
    scanf("%d", &n2);

    // Check if n1 is a multiple of n2 and print result
    if(is_Multiple(n1, n2))
        printf("\n%d is a multiple of %d.\n", n1, n2);
    else
        printf("\n%d is not a multiple of %d.\n", n1, n2);

    return 0;
}

Sample Output:

Input the first integer : Input the second integer: 
9 is a multiple of 3.

Pictorial Presentation:

C Programming: Reads in two integers and check whether the first integer is a multiple of the second integer.


Flowchart:

C Programming Flowchart: Reads in two integers and check whether the first integer is a multiple of the second integer.


For more Practice: Solve these Related Problems:

  • Write a C program to check if one integer is a multiple of another using the modulus operator.
  • Write a C program to determine if the first number is an exact divisor of the second, handling zero appropriately.
  • Write a C program to verify multiplicity for two numbers using a custom function and recursion.
  • Write a C program to check if the first integer is a multiple of the second and output the quotient if true.

Go to:


PREV : Remove a negative sign from a number.
NEXT : Display integer equivalents for all letters (a–z, A–Z).

C programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.