It is as up-to-date for both C and C++ as possible, with examples for much of the language features.
It is NOT for beginners, period. Many of the examples on language features use other language features without explanation. And the write-ups on language features are heavy on tech jargon, as befits a reference.
@Windsen - what problem are you having? What don't you understand? What have you attempted so far? Post your current code. NB There's plenty of previous posts on this forum and elsewhere on how to calculate/obtain prime numbers.
show what you tried.
a tip for the first part:
a positive number > 1000 has 4 or more digits. 1000 itself is the only 4 digit illegal value, you can compare "1000" to check the special case.
if you read it as a string, and every character isdigit(), and the length() >3, its ok, right?
if you read it as a string, you can either iterate it [] or sort() and peel the largest digit.
can you do that much (validate and find the largest digit)? remember that '9' and 9 are not directly related, but it may not matter since '0' < '1' ... < '9' so comparison still works correctly. Just be careful not to confuse integer values and letters as this can be a silent bug due to letters actually BEING a type of integer.
the first part is harder to do if you read the input into an integer. you can still do it, its just a bit of trouble. %10 gets you the least digit (ones place) and /10 removes the least digit, repeat until number is zero in a loop...
learned what? If you see something you don't know, ask specifically.
schools still have not picked up on {}.
int x{};
is the same as
int x = 0;
as far as you are concerned.
as for do-while, all loops are the same, the difference is just syntax elegance. do-while sometimes avoids having extra code outside the loop, but that is minor.
#include <iostream>
// #include <cmath> // NO NEED FOR THIS
usingnamespace std;
bool isPrime (int num);
int biggestDigit(int x); // MISSED A ';'
int main ()
{
int a_number = 0;
cout << "Enter a number: ";
cin >> a_number;
int answer = biggestDigit(a_number);
cout << "Biggest digit is " << answer << endl;
return 0;
}
// FUNCTIONS GO HERE NOT INSIDE main()
int biggestDigit(int x)
{
int biggest = -1; // EVERY DIGIT WILL BE BIGGER THAN THIS
int leftover = 0;
int digit = 0;
do
{
digit = x % 10;
leftover = x / 10;
if(digit > biggest)
biggest = digit;
x = x/10;
} while( leftover != 0 );
return biggest;
}
Enter a number: 35082
Biggest digit is 8
Program ended with exit code: 0
#include <iostream>
usingnamespace std;
bool isPrime (int);
int biggestDigit(int);
int main ()
{
// PART 1
for(int i = 0; i < 101; i++)
{
if( isPrime(i) )
cout << i << ' ';
}
cout << '\n';
// PART 2
int no_attempts{1};
int number{0};
while(
cout << "Enter a number: " &&
cin >> number &&
number < 1000 &&
no_attempts < 5
)
{
cout << "Number must be greater than 1000\n";
no_attempts++;
}
if(no_attempts < 5)
{
int answer = biggestDigit(number);
cout << "Biggest digit is " << answer << endl;
}
else
cout << "Too many tries\n";
return 0;
}
bool isPrime (int x)
{
if(x < 2)
returnfalse;
for (int i = 2; i * i <= x; i++)
{
if( x % i == 0)
returnfalse;
}
returntrue;
}
int biggestDigit(int x)
{
int biggest = -1;
int leftover = 0;
int digit = 0;
do
{
digit = x % 10;
leftover = x / 10;
if(digit > biggest)
biggest = digit;
x = x/10;
} while( leftover != 0 );
return biggest;
}
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Enter a number: 34
Number must be greater than 1000
Enter a number: 7089
Biggest digit is 9
Program ended with exit code: 0
while(
cout << "Enter a number: " &&
cin >> number &&
number < 1000 &&
no_attempts < 5
)
{
cout << "Number must be greater than 1000\n";
no_attempts++;
// this is the point where you can compute "tries left" from no_attempts and show it
}
#include <iostream>
#include <string>
usingnamespace std;
bool isPrime (int);
int biggestDigit(int);
string howManyLeft(int);
int main ()
{
// PART 1
for(int i = 0; i < 101; i++)
{
if( isPrime(i) )
cout << i << ' ';
}
cout << '\n';
// PART 2
int no_attempts{1};
int number{0};
while(
cout << "Enter a number: " &&
cin >> number &&
number < 1000 &&
no_attempts < 5
)
{
cout << "Number must be greater than 1000\n";
cout << howManyLeft(no_attempts);
no_attempts++;
}
if(no_attempts < 5)
{
int answer = biggestDigit(number);
cout << "Biggest digit is " << answer << endl;
}
else
cout << "Too many tries\n";
return 0;
}
bool isPrime (int x)
{
if(x < 2)
returnfalse;
for (int i = 2; i * i <= x; i++)
{
if( x % i == 0)
returnfalse;
}
returntrue;
}
int biggestDigit(int x)
{
int biggest = -1;
int leftover = 0;
int digit = 0;
do
{
digit = x % 10;
leftover = x / 10;
if(digit > biggest)
biggest = digit;
x = x/10;
} while( leftover != 0 );
return biggest;
}
string howManyLeft(int x)
{
string howMany[]
{
"5 tries left\n", "4 tries left\n", "3 tries left\n",
"2 tries left\n", "1 tries left\n"
};
return howMany[x];
}
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Enter a number: 1
Number must be greater than 1000
4 tries left
Enter a number: 1023
Biggest digit is 3
Program ended with exit code: 0