Write a C Program to check number is divisible by 11 and 9 using recursion

By | 23.03.2017

C Program to check whether number is divisible by 11 and 9 or not


Write a C Program to check whether number is divisible by 11 and 9 or not by recursion. Here’s simple Program that tests whether a number is divisible by 11 and 9 or not using Recursion in C Programming Language.


Recursion : :


  • Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.
  • The C programming language supports recursion, i.e., a function to call itself. But while using recursion, programmers need to be careful to define an exit condition from the function, otherwise it will go into an infinite loop.
  • Recursive functions are very useful to solve many mathematical problems, such as calculating the factorial of a number, generating Fibonacci series, etc.

Below is the source code for C Program to check number is divisible by 11 and 9 using recursion which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/* Program that tests whether a number is divisible by 11 and 9 or not */


#include<stdio.h>
int divisibleBy9(long int x);
int divisibleBy11(long int x);

int main( )
{
        long int num;
        printf("Enter the number to be tested : ");
        scanf("%ld", &num);

        if(divisibleBy9(num))
                printf("\nThe number is divisible by 9\n");
        else
                printf("\nThe number is not divisible by 9\n");

        if(divisibleBy11(num))
                printf("\nThe number is divisible by 11\n");
        else
                printf("\nThe number is not divisible by 11\n");

                return 0;

}/*End of main()*/

int divisibleBy9( long int n )
{
        int sumofDigits;
        if(n==9)
                return 1;
        if(n<9)
                return 0;
        sumofDigits=0;
        while(n>0)
        {
                sumofDigits += n%10;
                n/=10;
        }
        divisibleBy9(sumofDigits);
}/*End of divisibleBy9()*/

int divisibleBy11( long int n )
{
        int s1=0, s2=0,diff;

        if(n == 0)
                return 1;
        if(n < 10)
                return 0;

        while(n>0)
        {
                s1 += n%10;
                n /=10;
                s2 += n%10;
                n /= 10;
        }
        diff = s1>s2 ? (s1-s2) : (s2-s1);
        divisibleBy11(diff);
}/*End of divisibleBy11()*/

OUTPUT  : :


***************OUTPUT***************


***************FIRST RUN************

Enter the number to be tested : 6534

The number is divisible by 9

The number is divisible by 11



***************SECOND RUN************

Enter the number to be tested : 657

The number is divisible by 9

The number is not divisible by 11



***************THIRD RUN************

Enter the number to be tested : 742

The number is not divisible by 9

The number is not divisible by 11

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

4 2 votes
Article Rating
Category: C Programming Recursion Programs Tags:

About Tunde A

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments