In this tutorial, we are going to learn to write C Program to check whether a given number is divisible by 11 or not.
Any number where the difference between the sum of digits at odd and even places is 0 or a multiple of 11 is divisible by 11.
Below, we have written and explained the program for how to check the divisibility of a number by 11 with the help of an if-else statement in C.
Problem Statement
We need to check that a given input number by users is divisible by 11 or not by using if-else statements. Here Our program will take integer input from the user. It should print “the given number is divisible by 11” in case of number is divisible by 11. And if the number is not divisible by 11 then it should print “number is not divisible by 11”.
For Example
Example 1:
Suppose we have to check 82907 is divisible by 11 or not?
Sum of digits at odd place = S1 = 22 (8+9+7)
Sum of digits at even place = S2 = 2 (2+0)
S1-S2 = 22-2 = 22 (multiple of 11) so we can say 82907 is divisible by 11.
The above example is showing how you can check quickly if you don’t have a calculator. But for writing the program that logic is completed.
We can easily Check by using % operator in C.
Program to Check Divisibility by 11
#include<stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", & num);
if (num % 11 == 0)
printf("Yes, %d is divisible by 11.", num);
else
printf("No, %d is not divisible by 11.", num);
return 0;
}
Program Output
Output 1:
Enter a number: 123
No, 123 is not divisible by 11.
Output 2:
Enter a number: 121
Yes, 121 is divisible by 11.
Program Explanation
- Let’s go through the program step by step to understand it better:
- We have included stdio.h to enable the program to do input and output operations.
- The main() function is the entry point of any C program.
- Here, we have declared the variable ‘num’ of integer type to store user input.
- printf() function is used to show the output/message on the display.
- scanf() function is used to read the input given by the user.
- if-else statement is used to apply to check whether the given integer input is divisible by 11 or not.
- The % is known as the percentile operator, used to find the remainder when a number is divided by the divisor.
- In our scenario, when we divide a number by 11, and if the remainder is zero, it means that the number is divisible by 11. Otherwise, the program displays the message not divisible by 11.
- The return statement returns 0 to the operating system and denotes the end of a program.
Conclusion
In this tutorial we have learnt to write C program to check whether a given number is divisible by 11 or not with the help of if-else statements. The above program takes a integer as input from the user. And After taking input, with the help of if else statement it checks whether the number is divisible by 11 or not. I hope this blog was helpful to you in understanding the C program.