CIA password

Hi, I need help I am creating a code that allows the user to enter a password must be six digits long and the sum of the digits must be a prime number.
Here is what I have so far right now it is giving me many prime numbers because I can't separate the digits.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <cmath>
#include <string>

using namespace std;

int main()
{
int num=0, x;

cout << "Enter six digit code." << endl;
cin >> num;


while (num>999999 || num<100000){

      cout <<"code doesn't have six digits." << endl;
      cout <<"Enter six digit code." ;
      cin >> num;
}
cout << num << endl;

    for (int i = 2; i < num; i++){
        x = 0;
        for (int j = 2; j < i; j++){
            if (i%j == 0){
                x = 1;
                break;
            }
        }
        if (x == 0){
            cout << i << " is a prime number\n";
        }
    }
//Exit program
return 0;

}
Last edited on
1
2
3
4
5
6
    for (int i = 2; i < num; i++){
        x = 0;
        for (int j = 2; j < i; j++){
            if (i%j == 0){
                x = 1;
                break;


What is this loop for? The first thing you have to do is add the digits of the number, then try to make a loop to compare with a prime number. You need to know what a prime number is! . Then try to come up with some statements to compare them with the new number. Try to do some more, you can do it!
A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime because 1 and 5 are its only positive integer factors, whereas 6 is composite because it has the divisors 2 and 3 in addition to 1 and 6.

So first add the digits, all of them. I would suggest asking the user to input the number as a string, then convert the string to chars, then convert the chars to ints, add them and see if the number has no positive divisors other than 1 and itself.

That means, if num % 1 == 0 and num % num == 0, then it's a prime number. The number will of course be bigger than 1 because you will not take negative input.
Also, you will need to check if num % 2 == 0, if true, not prime.
There is another way of doing this, which might be considered a little foolish, but I personally like it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<iostream>
#include<string>
using namespace std;

bool isPrime(int n){ //check if a number is prime
bool prime = true;
if (n==2){
    return true;
}
for (int i=2; i<=n/2; i++){
    if(n%i==0){
        prime = false;
    }
}
return prime;
}
int ch_to_int (char a){    //convert character digit to normal number (you'll see why it's needed)
int num = a - 48;
return num;
}

int main(){
    string code;  //declare a string code
    cin>>code;
    while(code.size()!=6){
        cout<<"This is not a six digit code, enter a six digit code\n";  //if it's not six digit read again
        cin>>code;
    }
    int sum = 0;
    for (int i=0; i<code.size(); i++){
        sum += ch_to_int(code[i]);        //add each number to the sum
    }
    if(isPrime(sum)==true){
        cout<<"The sum is a prime\n";       //check if it is prime or not.
    }
    else{
        cout<<"The sum isn't a prime";
    }
}
Topic archived. No new replies allowed.