Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Thursday, July 23, 2020

C program to chek divisibility of number

C program to chek divisibility of a given number

Write a C program to input a number from user and check it's divisibility by some other number as divisor using if...else. Let's see the below step by step logic check divisibility of a number.

Exmaple:

  1. INPUT

  2. Enter any number to check it's divisibility: 30
  3. Enter another number as divisior: 6

  1. Enter any number to check it's divisibility: 45
  2. Enter another number as divisior: 8

  OUTPUT

30 is divisible by 6.
45 is not divisible by 8.

Required knowledge for this exercise

Logic to check divisibility of a number using if...else:

A number is exactly divisible by some other number if it gives 0 as remainder after dividing otherwise not. Example: 18 is exactly divisible by 9 i.e18 / 9 = 2with a remainder 0.

in C, modulo% division operator returns remainder after integer division. If it returns zero it means number is exactly divisible otherwise not. For Example: if(18 % 4 == 0)it's not true since it returns 2 as remainder, therefore 18 is not divisible by 4. 

Now let's see the below step by step logic to check divisibility of a number.

1) Input any number from user and store it in variable num using scanf()
2) Check the given number is exactly divisible by some other number or not using modulo % operator. Likeif (num % divisor == 0)if it's remainder is equal to0then number is exactly divisible otherwise not. Since we know that, modulo %operator always returns remainder.

Program to check divisibility of a number using if...else: 

/* C program to check divisiblity of a number */ #include <stdio.h> int main() { int num, divisor; /* Input a number from user */ printf("Enter any number to check it's divisivility: "); scanf("%d", &num);

  • /* Input another number as divisor from user */ printf("Enter another number as divisor: "); scanf("%d", &divisior); /* * If num modulo division % and another number as divisor i.e * if(num % divisor == 0) is equal to 0 then number is divisible else not. */ if(num % dvisor == 0) { printf("Yes! %d is divisible by %d", num, divisior); } else { printf("NO! %d is not divisible %d", num, divisor); } return 0; }

  • Output:
    Enter any number to check it's divisibility: 30
    Enter another number as divisor: 6
    Yes! 30 is divisible by 6

    Enter any number to check it's divisibility: 45 
    Enter another number as divisor: 8
    No! 45 is not divisible by 8



    Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

    Help others by sharing this page.

    Ahmad Irshad

    Author & Editor

    I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

    0 Comments:

    Post a Comment

    Please don't enter any spam link in the comment box.


    Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
    Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
    Contact Us