Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

c++ - How can I find prime reversed numbers?

I have to write a program to check if the entered number has these qualifications:

A number that is prime it self, the reverse of that number is also prime, and the number's digits are prime numbers too (Like this number: 7523). If the needs meet, it has to show "yes" when you enter and run the program otherwise "no".

I know both codes for prime and reverse numbers but I don't know how to merge them.

This is the code:

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

using namespace std;

void prime_check(int x) {
  int a, i, flag = 1;
  cin >> a;
  for (i = 2; i <= a / 2 && flag == 1; i++) {
    if (a % i == 0)
      flag = 0;
  }
  if (flag == 1)
    cout << "prime";
  else
    break;
}

int main() {
  int a, r, sum = 0;
  cin >> a;
  while (a != 0) {
    r = a % 10;
    sum = (sum * 10) + r;
    a = a / 10;
  }
}

The program has to check each digit of the number entered to see if it is prime or not in every step, then show "yes", but it doesn't work.

question from:https://stackoverflow.com/questions/65914452/how-can-i-find-prime-reversed-numbers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I have done some changes to your code, and added comments everywhere I've made changes. Check it out:

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

using namespace std;

bool prime_check(int x) {  // I have changed the datatype of this function to bool, because I want to store if all the digits are prime or not
  int i, flag = 1;  // Removed the variable a, because the function is already taking x as input
  for (i = 2; i <= x / 2 && flag == 1; i++) {
    if (x % i == 0)
      flag = 0;
  }
  return flag == 1;
}

int main() {
  int a, r, sum = 0, original;  // added original variable, to store the number added
  bool eachDigit = true;  // added to keep track of each digit
  cin >> a;
  original = a;
  while (a != 0) {
    r = a % 10;
    eachDigit = prime_check(r);  // Here Each digit of entered number is checked for prime
    sum = (sum * 10) + r;
    a = a / 10;
  }

  if (eachDigit && prime_check(original) && prime_check(sum))  // At the end checking if all the digits, entered number and the revered number are prime
    cout << "yes";
  else
    cout<< "no";

}

For optimization, you can check if the entered number is prime or not before starting that loop, and also you can break the loop right away if one of the digits of the entered number is not prime, Like this:

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

using namespace std;

bool prime_check(int x) {  // I have changed the datatype of this function to bool, because I want to store if all the digits are prime or not
  int i, flag = 1;  // Removed the variable a, because the function is already taking x as input
  for (i = 2; i <= x / 2 && flag == 1; i++) {
    if (x % i == 0)
      flag = 0;
  }
  return flag == 1;
}

int main() {
  int a, r, sum = 0;
  bool eachDigit = true, entered;  // added to keep track of each digit
  cin >> a;
  entered = prime_check(a);
  while (a != 0 && entered && eachDigit) {
    r = a % 10;
    eachDigit = prime_check(r);  // Here Each digit of entered number is checked for prime
    sum = (sum * 10) + r;
    a = a / 10;
  }

  if (eachDigit && entered && prime_check(sum))  // At the end checking if all the digits, entered number and the revered number are prime
    cout << "yes";
  else
    cout<< "no";

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...