The Program in C++ Program to Display First 10 Fermat Numbers is given below:
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int fermatNumber;
cout << "Hello Codeauri Family, the first 10 Fermat numbers are:" << endl;
for (int i = 0; i < 10; i++) {
fermatNumber = pow(2, pow(2, i)) + 1;
cout << fermatNumber << endl;
}
return 0;
}
Output:
Hello Codeauri Family, the first 10 Fermat numbers are:
3
5
17
257
65537
-2147483648
-2147483648
-2147483648
-2147483648
-2147483648
Pro-Tips💡
In this program, the Fermat numbers are calculated using the formula fermatNumber = pow(2, pow(2, i)) + 1
, where i
is the index of the Fermat number, starting from 0.
The pow
function is used to calculate powers.
The for
loop runs 10 times, each time calculating the next Fermat number and printing it to the standard output.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.