A directory of Objective Type Questions covering all the Computer Science subjects.
Here you can access and discuss Multiple choice questions and answers for various compitative exams and interviews.

Dear users, compscibits.com is now permanently moved to compsciedu.com. Please update all your links and bookmarks accordingly. Soon, the site will be accessible through URL compsciedu.com only.

This C programming example is to check whether a given number is divisible by 3 or not. You can achieve this by applying multiple programming logic. Here, modulus operator (%) is used to find out if entered number is divisible by 3.

C program to check if a number is divisible by 3 - Source code

#include <stdio.h>
int main()
{
int number;
printf("Enter number:\n");
scanf("%d",&number);
if ((number % 3) == 0)
printf("%d is a multiple of 3",number);
else
printf("%d is not a multiple of 3",number);
return 0;
}

Program Output

Case 1:
Enter number:
493648
493648 is not a multiple of 3
Case 2:
Enter number:
98274651
98274651 is a multiple of 3

Program Explanation

1. This program simply asks user to input an integer number.
2. In order to check for its divisibility by 3, modulus operator (%) is used. This operator return the remainder after dividing the given number by another number.
3. If the modulus operator return 0 as remainder, the number is multiple of 3. Otherwise, it is not a multiple of 3.