You are currently viewing C Program to Check a Given number Divisible by 5 or not using if-else

C Program to Check a Given number Divisible by 5 or not using if-else

In this tutorial, we are going to learn to write C Program to check whether a given number is divisible by 5 or not. As we know that a number is divisible by 5 when  0 or 5 is at its unit digit.  Below, we have written and explained the writing program for how to check the divisibility of a number by 5 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 5 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 5” in case of number is divisible by 5. And if the number is not divisible by 5 then it should print “number is not divisible by 5”.

For Example

Enter a number: 15
The number 15 is divisible by 5.

In the above example you can see given input is 15. As 15 is divided by 5, So the output here is “The number 15 is divisible by 5”.

Program to Check a Given Number is Divisible by 5 or not in C

#include<stdio.h>
int main()
{
    int num;
    printf("Enter a number: ");
    scanf("%d", &num);
    if(num % 5 == 0)
    {
        printf("The given number is divisible by 5");
    }
    else
    {
        printf("The given number is not divisible by 5");
    }
    return 0;
}

Output 1:

Input :
Enter a number: 25

Output:
The given number is divisible by 5

Output 2:

Input :
Enter a number: 23

Output:
The given number is not divisible by 5

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 check whether the given integer input is divisible by 5 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 5 and if the remainder is zero, it means that the number is divisible by 5. Otherwise, the program displays the message not divisible by 5.
  • The return statement returns 0 to the operating system and denotes the end of a program.

Conclusion

In this tutorial, we have learned to write a C program to check whether a given number is divisible by 5 or not with the help of if-else statements. The above program takes an 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 5 or not. I hope this blog was helpful to you in understanding the C program.