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.
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:
- 1. prime() - it returns 1 when the number passed as a parameter is prime, otherwise it returns 0
- 2. reverse()-to return the reverse of the number passed as a parameter
- 3. main()- accepts the number from the user, calls the other functions appropriately and checks whether the accepted number is an emirp number or not
ALGORITHM FOR PRIME FUNCTION:
- 1.Start
- 2. Variable ‘n’ is passed as a parameter to this function
- 3.declaring ctr as 0 to count the number of factors of ‘n’
- 4. for i ranging from 1 to ‘n’ check whether ‘n’ is divisible by ‘i’ or not.If yes then increment counter (ctr)
- 5.if ctr is equal to 2 , then return 1 otherwise return 0
- 6.Stop
ALGORITHM FOR REVERSE FUNCTION:
- 1. Start
- 2. Variable ‘n’ is passed as a parameter to this function
- 3. 'Rem' , used for storing the remainder is initialized to 0
- 4. rev , used for storing the reverse of ‘n’ is initialized to 0
- 5. While 'n' is not equal to 0, the following steps need to be executed:
- (i)in rem store the last digit of ‘n’
- (ii)calculate rev as rev*10 +rem
- (iii)update n as n/10
- 6. Return rev to the calling function
- 7. Stop
ALGORITHM FOR MAIN FUNCTION:
- 1.Start
- 2.Accept the number 'n' from the user
- 3.invoke reverse method by passing 'n' as a parameter and store the result in 'rev'
- 4.invoke prime method by passing 'n' as a parameter and store the resultant in prime1.
- 5.repeat the same process by passing 'rev' as a parameter to prime method and store the resultant in prime2
- 6. If both prime1 and prime2 are equal to 1 i.e both 'n' and rev are prime then display ‘n'is an emirp number otherwise go to step 7
- 7. Display 'n' is not an emirp number
- 8.Stop
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);
}
DRY RUN
Supposing n=17 , then when reverse () is invoked , the following operation takes place:
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