emirp number in C
In this article, we will discuss the emirp number in C with its algorithm, example, and applications.
What is the emirp number?
An emirp number is a prime number that remains prime when its digits are reversed. The word "prime" is spelt "emirp" to represent it.
For illustration, let's think about the number 37. It is a prime number that becomes 73 when the digits are flipped over. Therefore, 37 is an emirp number.
Every prime number is not an emirp. When the digits of the number 3 are reversed, we still get the number 3, which is not regarded as a distinct prime number. For instance, the number 3 is a prime number. 3 is not an emirp number as a result.
Algorithm:
The following is a C algorithm for verifying emirp numbers:
- Begin with the specified number "num".
- Verify that the num is prime. The process should be stopped if the number is not an emirp number and is not prime.
- Reverse num's digits to get reversedNum.
- Verify reversedNum is a prime number. A number is an emirp number if it is prime and reversedNum is not equal to num. Declare num to be an emirp number and end the algorithm.
- Numer is not an emirp number if reversedNum is not a prime number or if reversedNum is equal to num. Declare num not to be an emirp number and end the algorithm.
Let us see the program:
//checking emirp number using functions
#include <stdio.h>
//defining prime function
//checking whether a given number is prime or not
int prime(int num) {
if (num <= 1) {
return 0;
}
for (int i = 2; i * i <= num; i++)
{
if (num % i == 0)
{
return 0;
}
}
return 1;
}
//reverse function for reversing the given number
int reverse(int num) {
int reversedNum = 0;
while (num > 0) {
reversedNum = reversedNum * 10 + num % 10;
num /= 10;
}
return reversedNum;
}
/*now checking reversed number is prime or not. If it is prime, it is considered an emirp number*/
int emirp(int num) {
int reversedNum = reverse(num);
if (prime(num) && prime(reversedNum) && num != reversedNum)
{
return 1;
}
return 0;
}
int main()
{
int num;
//inputting the number
printf("Enter a number- ");
scanf("%d", &num);
if (emirp(num))
{
printf("%d is an emirp number.\n", num);
}
else
{
printf("%d is not an emirp number.\n", num);
}
return 0;
}
Output:
![emirp number in C](https://static.tutorialandexample.com/c/emirp-number-in-c1.png)
Explanation:
This program's "prime" function determines whether a number is prime by iterating from 2 to the integer's square root and determining whether it is divisible. The digits of a number are reversed with the "reverse" function. Calling prime on both the original number and its reversed counterpart ensures that they are not the same. The "emirp" function determines whether a given number is an emirp number.
When the user is asked to enter a number, the program first verifies that it is an emirp number. After that, it outputs the outcome appropriately.
Applications of emirp number in c:
emirp numbers can be utilized in various applications and algorithms in C programming. Here are a few illustrations:
Applications in cryptography: emirp numbers can be utilized in C-based cryptographic methods. For instance, emirp numbers can be used as prime factors or as part of a critical generation procedure to produce secure RSA keys.
Prime factorisation: By iterating through emirp numbers and seeing if they divide the given number evenly, one can determine the prime factors of a big integer.
Prime number generation: A list of prime numbers can be created using emirp numbers. A program that determines whether a number is prime and whether the number's reversed counterpart is also prime is known as an emirp number.
Identification of palindromes: emirp numbers are prime palindromes. By confirming that a given number is prime and a palindrome (meaning that its digits are the same when reversed), you may create a program that determines whether it is an emirp number.