C Program to check whether a number is divisible by 5 or not


October 22, 2023, Learn eTutorial
193

How to check a number divisibility?

If we say a number 'x' is divisible by number 'y' then there will be no remainder after the division of 'x' and 'y'. 

How to check whether a number is divisible by 5?

To check whether a given number is divisible by 5, we have to divide the given number by 5 and check whether the remainder is zero or not. If the remainder is zero, then the given number is divisible by 5, else not divisible.

How do we apply the number divisibility logic in this C program?

In this C program, to check whether the remainder is zero or not, we are using the modulus operator. modulus operator will save the remainder after the complete division and we have to check that is zero or not using a if condition. Finally printing the result. 

ALGORITHM

STEP 1: Include the header files for using the built-in functions in C

STEP 2: Accept the number from the user to check the divisibility

STEP 3: Use the modulus operator to get the remainder 

STEP 4: Use an if condition to check whether the remainder is zero or not

STEP 5: If the remainder is zero print divisible else not divisible.

 

C Source Code

                                          /**
 * C program to check the divisibility of any number
 */

#include <stdio.h>

int main()
{
    int num;

    /* Input number from user */
    printf("Enter any number: ");
    scanf("%d", #);


    /*
     * If  num modulo division 5 is 0 
     * then the number is divisible by 5 
     */
    if((num % 5 == 0)
    {
        printf("Number is divisible by 5");
    }
    else
    {
        printf("Number is not divisible by 5");
    }

    return 0;
}

                                      

OUTPUT

Enter any number:  10

Number is divisible by 5