Computer Science, asked by naveengnaveen1491, 11 months ago

WAP to accept 2 numbers and check whether they are twin prime or not use loop


VemugantiRahul: programming language unspecified
VemugantiRahul: is it C ?
VemugantiRahul: I used C

Answers

Answered by VemugantiRahul
1
Hi there!
Here's the answer:

•°•°•°•°•°<><><<><>><><>°•°•°•°•°•°

¶¶ Twin Prime Numbers ¶¶

¶ Twin prime are those numbers which are prime and having a difference of 2 between them.

¶ Eg: (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73) etc

•°•°•°•°•°<><><<><>><><>°•°•°•°•°•°

¶ Steps:
• Take 2 Numbers as input from the User.
• Check whether the given numbers are Prime Numbers.
• Check whether the difference is 2 between them.
• If both conditions are satisfied, display message on the screen that given numbers are twin prime.
• If not, print message as given numbers are not twin prime.


•°•°•°•°•°<><><<><>><><>°•°•°•°•°•°


/* C- Program to test whether the two given given numbers are twin prime */



#include<stdio.h>
#include<conio.h>
#include<Math.h> // include a header file for abs functions for absolute value.

int main()
{

…int i,n1,n2; // here n1 and n2 are the two input numbers

…int d; // d is the variable to store the difference of two numbers

…int c1=0,c2=0; // c1 and c2 are the counter values for checking the numbers are prime

…clrscr();

…printf("\n Enter a Number 1:");
…scanf("%d",&n1);
…printf("\n Enter Number 2:");
…scanf("%d",&n2);

…for(i=1;i<=n1;i++)
……{
………if(n1%i==0)
……………c1++; // if counter c1 is equal to 2 then the number n1 is prime
……}

…for(i=1;i<=n2;i++)
……{
………if(n2%i==0)
……………c2++; // if counter c2 is equal to 2 then the number n2 is prime
……}

…d=abs(n1-n2); // here d is difference which calculate the absolute value i.e Modulus of |n1-n2|

…if(d==2 && c1==2 && c2==2)
………printf("The Given Two Numbers are Twin Prime\n");
…else
………printf("The Given Two Numbers are Not Twin Prime\n");

…getch();
…return 0;
}

•°•°•°•°•°<><><<><>><><>°•°•°•°•°•°

Just used …… to follow INDENTATION.

•°•°•°•°•°<><><<><>><><>°•°•°•°•°•°

©#£€®$

:)

Hope it helps
Similar questions