w3resource

C Exercises: Display first 10 Fermat numbers

C Numbers: Exercise-30 with Solution

Write a program in C to display first 10 Fermat numbers.

Sample Solution:

C Code:

# include <stdio.h>
# include <stdlib.h>
# include <math.h>


int main()
{
    int n=0;
	double result;
	printf("\n\n Display first 10 Fermat numbers:\n");
	printf("-------------------------------------\n");
	printf(" The first 10 Fermat numbers are: \n");
while (n <= 10) 
		{
            result= pow(2, pow(2, n)) + 1;
            n++;
            printf("%lf \n",result);
        }
}

Sample Output:

 The first 10 Fermat numbers are:                                                                             
3.000000                                                                                                      
5.000000                                                                                                      
17.000000                                                                                                     
257.000000                                                                                                    
65537.000000                                                                                                  
4294967297.000000                                                                                             
18446744073709551616.000000                                                                                   
340282366920938463463374607431768211456.000000                                                                
115792089237316195423570985008687907853269984665640564039457584007913129639936.000000                         
13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858
186486050853753882811946569946433649006084096.000000                                                          
inf 

Pictorial Presentation:

C programming: Display first 10 Fermat numbers.

Flowchart:

Flowchart: Display first 10 Fermat numbers

C Programming Code Editor:

Contribute your code and comments through Disqus.

Previous: Write a program in C to check whether a given number is a perfect cube or not.
Next: Write a program in C to find any number between 1 and n that can be expressed as the sum of two cubes in two (or more) different ways.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



C Programming: Tips of the Day

Why do C and C++ compilers allow array lengths in function signatures when they're never enforced?

It is a quirk of the syntax for passing arrays to functions.

Actually it is not possible to pass an array in C. If you write syntax that looks like it should pass the array, what actually happens is that a pointer to the first element of the array is passed instead.

Since the pointer does not include any length information, the contents of your [] in the function formal parameter list are actually ignored.

Ref : https://bit.ly/3fhlvdH