In this article, we will write a very simple C program to check if a number is divisible by 3 and 5.
C Program to check if a given number is divisible by 3 and 5
This program first takes an integer number as an input from the user. Then, it checks if the number is divisible by both 3 and 5 using the modulo operator %
. If the remainder when dividing the input number by 3 is 0 and the remainder when dividing by 5 is 0, then the number is divisible by both 3 and 5, else it is not divisible by both.
#include <stdio.h>
int main() {
int num;
// Input number from user
printf("Enter a number: ");
scanf("%d", &num);
// Check if the number is divisible by both 3 and 5
if (num % 3 == 0 && num % 5 == 0) {
printf("%d is divisible by both 3 and 5.\n", num);
} else {
printf("%d is not divisible by both 3 and 5.\n", num);
}
return 0;
}
Output:
Output 1:
Enter a number: 15
15 is divisible by both 3 and 5.
Output 2:
Enter a number: 12
12 is not divisible by both 3 and 5.
Leave a Reply