how to find prime number in c++ and how to find the greatest digit in a number.

I'm trying to write a function that will print out function from 1 to 100 and a function that prints out the greatest digit in the integer that was input by the user.

The assignment is:
Program prompts user to input a positive integer x greater than 1000. If the inputted value is invalid, program continuously prompts user to input a valid number. Program then finds the biggest digit of x by using biggestDigit function (c). And prints all the prime numbers from 0 to 100 using isPrime function (b).

and this is my code so far:

#include<iostream>
#include<cmath>
using namespace std;
int biggestDigit(int a){



void is prime(){
for(int a=1;a<=100;a++){



int main(){
int n;
cout<<"Enter an integer greater than 1000: ";
cin>>n;
while(n<=1000){
cout<<"Try again: ";
cin>>n;
}
cout<<biggestDigit(n);
cout<<isPrime();
cout<<endl;
return 0;
}

i'm stuck and need help.
Thanks.

Last edited on
Here I have an example for a function which read-in an integer. Its control flow is somewhat ugly, but that's almost the case if it shall being able handling various input errors.
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
/*
 * Reads an integer. Range: [min : max].
 */
int read_integer( const int min = 0, const int max = 1000, std::istream& istr = std::cin, std::ostream& ostr = cout)
{
    int number;
    while (true) {
        if (istr >> number) {
            if ( min <= number && number <= max) {
                return number;  // here the solely return point
            }
            ostr << "The number must be in a range of " << min << " and "<< max<< ".\n";
            continue;
        }
        ostr << "The input was no number. Try again.\n";
        
        // reset the failbit
        istr.clear();
        
        // consume all wrong input
        char c;
        do {
            c = istr.get();
        } while (c != ' ' && c != '\n');
    }
}
Getting the biggest digit of an int:

1
2
3
4
5
6
7
8
9
10
11
12
13
int biggestDigit( int number)
{
    char max = 0;
    std::string num_str = std::to_string( number);
    
    for(char c : num_str) {
        if ( c >= '0' && c <= '9' && c > max) {
                max = c;
        }
    }
    // makes the ASCII character to an int
    return static_cast<int>(max - '0');
}
Topic archived. No new replies allowed.