How to Write a C Program That Checks Whether Two Integers Are Multiples
In this program, we will show you how to write a simple C program that reads two integers from the user input and checks whether they are multiples or not.
What Are Multiples?
Multiples are numbers that can be divided by another number without leaving a remainder. For example, 12 is a multiple of 3 because 12 / 3 = 4 with no remainder. Similarly, 15 is a multiple of 5 because 15 / 5 = 3 with no remainder.
To check whether two integers are multiples, we can use the modulo operator (%) . The modulo operator returns the remainder of the division of two numbers. For example, 12 % 3 = 0 and 15 % 5 = 0. If the remainder is zero, it means that the two numbers are multiples. Otherwise, they are not.
C Program
To write the C program that checks whether two integers are multiples, we need to follow these steps:
- Include the stdio.h header file that provides input/output functions.
- Declare two integer variables to store the user input.
- Print a message to prompt the user to enter two integers.
- Use the scanf() function to read two integers from the user input and store them in the variables.
- Use an if-else statement to check if one integer is divisible by the other using the modulo operator (%).
- Print the result accordingly using the printf() function.
- Return 0 to indicate successful termination of the program.
Here is the complete code of the C program:
#include <stdio.h>
int main()
{
int a, b; // declare two integers
printf("Enter two integers: ");
scanf("%d %d", &a, &b); // read two integers from user input
if (a % b == 0 || b % a == 0) // check if one integer is divisible by the other
{
printf("%d and %d are multiples.\n", a, b); // print the result
}
else
{
printf("%d and %d are not multiples.\n", a, b); // print the result
}
return 0;
}
Output
To show the output of the above program, we need to run it with some input values.
For example, if we enter 12 and 4 as the input values, the output will be:
Enter two integers: 12 4
12 and 4 are multiples.
If we enter 15 and 7 as the input values, the output will be:
Enter two integers: 15 7
15 and 7 are not multiples.
Explanation
The above program is a simple C program that checks whether two integers are multiples or not. It consists of the following parts:
- The first line #include <stdio.h> is a preprocessor directive that tells the compiler to include the stdio.h header file that provides input/output functions such as printf() and scanf().
- The second line int main() is the main function of the program where the execution begins. It returns an integer value to indicate the status of the program.
- The third line int a, b; declares two integer variables named a and b to store the user input.
- The fourth line printf("Enter two integers: "); prints a message to prompt the user to enter two integers. The printf() function takes a string as an argument and displays it on the screen.
- The fifth line scanf("%d %d", &a, &b); reads two integers from the user input and stores them in the variables a and b. The scanf() function takes a format string and a list of pointers as arguments and assigns the input values to the corresponding variables. The %d is a format specifier that indicates an integer value. The & operator is used to get the address of the variables.
- The sixth line if (a % b == 0 || b % a == 0) is an if-else statement that checks if one integer is divisible by the other using the modulo operator (%). The modulo operator returns the remainder of the division of two numbers. For example, 12 % 3 = 0 and 15 % 5 = 0. If the remainder is zero, it means that the two numbers are multiples. Otherwise, they are not. The || operator is a logical OR operator that evaluates to true if either of the operands is true. For example, true || false = true and false || false = false.
- The seventh line printf("%d and %d are multiples.\n", a, b); prints the result if the condition is true. It displays the values of a and b and a message that they are multiples. The \n is an escape sequence that indicates a new line character.
- The eighth line else is executed if the condition is false.
- The ninth line printf("%d and %d are not multiples.\n", a, b); prints the result if the condition is false. It displays the values of a and b and a message that they are not multiples.
- The tenth line return 0; returns 0 to indicate successful termination of the program.
That's how the above program works.
Conclusion
In this program, we have learned how to write a C program that checks whether two integers are multiples or not. We have also learned how to use the modulo operator (%) and how to run the C program using an online compiler.
We hope you found this blog post helpful and informative. If you have any questions or feedback, please leave them in the comments section below.
We love your feedback and invite you to comment on our articles, exercises, examples, quizzes and others. Your feedback helps us make our content awesome and serve you better. Please leave a comment and tell us what you think. How did our content help you learn something new? Thank you for being a part of our community!
Comments
Post a Comment