Dremendo Tag Line

Print all pairs of two digit twin prime numbers using for loop in C++

for Loop - Question 11

In this question, we will see how to print all pairs of two-digit twin-prime numbers in C++ programming using for loop. To know more about for loop click on the for loop lesson.

Q11) Write a program in C++ to print all pairs of two-digit twin-prime numbers using for loop.

A twin-prime numbers are numbers that differs from another prime number by two. Examples of two-digit twin-prime numbers are: (11, 13), (17, 19), (29, 31), (41, 43), etc.

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i,j,fc,n;
    cout<<"List of all twin prime numbers between 10 to 99\n";
    for(i=10; i<=99; i=i+1)
    {
        fc=0;
        for(j=1; j<=i; j=j+1)
        {
            if(i%j==0)
            {
                fc=fc+1;		// counting the factors of i (value of i)
            }
        }

        if(fc==2)
        {
            fc=0;
            n=i+2;		// taking another number from the current value of i with a difference of 2

            for(j=1; j<=n; j=j+1)
            {
                if(n%j==0)
                {
                    fc=fc+1;		// counting the factors of n (value of n)
                }
            }

            if(fc==2)
            {
                cout<<"("<<i<<", "<<n<<")"<<endl;
            }
        }
    }
    return 0;
}

Output

List of all twin prime numbers between 10 to 99
(11, 13)
(17, 19)
(29, 31)
(41, 43)
(59, 61)
(71, 73)
video-poster