Ask Question

Name:
Title:
Your Question:

Answer Question

Name:
Your Answer:
User Submitted Source Code!


Description:
  asasa
Language: C/C++
Code:
/* CSCI1520
*
* Assignment: 3
* Student ID: 1155126614
* Name      : Ko Pui Sze
* Email     : [email protected]/
*
* Please insert the above information in all future assignment submissions.
*/


#include <iostream>
using namespace std;

int main() {

//step1 User Input
//s= integer, n= pairs

int s, n, input;
    cout <<"Enter an integer: ";
        cin>> s;
    
        while (s<=0){
                cout<<"Input must be +ve! Enter again."<<endl;
                cout <<"Enter an integer: ";
                cin>>s;
        }
    
            cout<<"How many pairs of emirps? ";
            cin>>n;
            
            while (n<=0){
                cout<<"Input must be +ve! Enter again."<<endl;
                cout<<"How many pairs of emirps? ";
                cin>>n;
       
 }//step 1 User input
    int cnt=0;
    input=s;
    

    //keep looping until cnt<n, so we will receive n-1 results
    while(cnt<n){
        int  x, k, rev=0, y=0;
        int isPrime = 1;
        x=s;
        
        //check the input number is prime or not
        for(k=2; k<=x/2; ++k)
               {
                    if(x<2)
                    isPrime=false;
                   
                    else
                        if(x%k ==0){
                           isPrime=false;
                            break;
                    }
                   
    }
        //if input no. is prime, reverse it
        if(isPrime==1){
                     y=x;
                    while (y> 0){
                    rev=rev*10+y%10;
                     y=y/10;
                     }
                    y=rev;
            
        //after reversing, if rev no not equal to input, then check rev is prime or not
           if (y!=x)
          { isPrime=2;
             for(k=2; k<=y/2; ++k)
               {
                    if(y<2)
                    isPrime=false;
                
                    else
                        if(y%k ==0){
                           isPrime=false;
                            break;
                            
                        }
    }
}
        }
//isPrime=2 means rev and input are both prime, so we can get the cout
if(isPrime==2 && x<y)
{ cout<< x <<" <> "<<y<<endl;
   cnt++;
}
 
else if(isPrime==2 && x>y && y<input){
  cout<< x <<" <> "<<y<<endl;
     cnt++;
  }
  
        
       
        
        s++;
    }
    return 0;
           

        }
Comments: