CODING JOY

The stunning potpourri of coding and mundane life

EMIRP NUMBER IN C LANGUAGE



AMRUHA AHMED
10th June,2023.



This article will help you to check whether the given number is an Emirp Number or not.

blog1

When a prime number's reverse also results in a prime number then it is known as an emirp number.


DELVE DEEPER:

In order to check whether a given number is an Emirp Number or not, three functions are required:


ALGORITHM FOR PRIME FUNCTION:


ALGORITHM FOR REVERSE FUNCTION:


ALGORITHM FOR MAIN FUNCTION:

PROGRAM:

The following C program checks whether the given number is an Emirp Number or not:

                

                    #include<stdio.h>
                    int prime(int n)//function to check whether n is prime or not
                    {
                        int ctr=0;//count of factors
                        int i;//loop counter
                        for(i=1;i<=n;i++)
                        {
                            if(n%i==0)
                            ctr++;
                        }
                        if(ctr==2)//if factors are 2 i.e 'n' is a prime number
                        return 1;
                        else
                        return 0;
                    }
                    int reverse(int n)//function to find the reverse of 'n'
                    {
                        int rem,rev;//rem stores remainder, rev stores the reverse of 'n'
                        while(n!=0)
                        {//calculating reverse
                            rem=n%10;
                            rev=rev*10+rem;
                            n=n/10;
                        }
                        return rev;
                    }
                    void main()
                    {
                        int n;//number to be checked
                        printf("\n Enter a number:");
                        scanf("%d",&n);
                        int rev=reverse(n);//stores the reverse of 'n'
                        int prime1=prime(n);//stores the returned value from prime() when 'n' is passed
                        int prime2=prime(rev);//stores the returned value from prime() when 'rev' is passed
                        if(prime1==1&&prime2==1)//if both rev and 'n' are prime
                        printf("\n %d is an Emirp Number",n);
                        else
                        printf("\n %d is not an Emirp Number",n);
                    }
                   
             
         
CODE COPIED

DRY RUN


Supposing n=17 , then when reverse () is invoked , the following operation takes place:

blog1

The result of reverse() is stored in rev which is equal to 71

Now, both n and rev are checked whether they are prime or not using the prime() whose logic is quite simple. The results are stored in prime1 and prime2 respectively. Prime1 in this case is 1 as well as prime2 since 17 and 71 are prime nunbers

According to the condition of emirp number, both a number and its reverse need to be a prime number. Since prime1 and prime2 are both equal to 1, the condition is satisfied .


OUTPUT-1:

Enter a number:17

17 is an Emirp Number

OUTPUT-2:

Enter a number:45

45 is not an Emirp Number