Q:

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

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Recursion Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C Program to display reverse and length of... >>
<< Write a C Program to print prime factors using Rec...