Power series of 3 using c programming language
C Programming Language Function in C Language (Article) Function in C Language (Program)
1133Program:
#include #include void main() { int i,m; for(i=1; i<=20; i++) { m = pow(3,i); printf(" 3^%d = %d \n",i,m); } }
Output:
3^1 = 3 3^2 = 9 3^3 = 27 3^4 = 81 3^5 = 243 3^6 = 729 3^7 = 2187 3^8 = 6561 3^9 = 19683 3^10 = 59049 3^11 = 177147 3^12 = 531441 3^13 = 1594323 3^14 = 4782969 3^15 = 14348907 3^16 = 43046721 3^17 = 129140163 3^18 = 387420489 3^19 = 1162261467 3^20 = -2147483648 Press any key to continue . . .
Explanation:
-
Include Libraries:
#include <stdio.h>
: This includes the standard input-output library for functions likeprintf
.#include <math.h>
: This includes the math library for mathematical functions likepow
.
-
Main Function:
void main()
: The main function where the execution of the program begins.
-
Variable Declarations:
int i, m
: Two integer variablesi
andm
are declared.i
will be used as the loop counter, andm
will store the result of the power calculation.
-
For Loop:
for(i = 1; i <= 20; i++)
: A loop that starts withi = 1
and runs untili
is 20, incrementingi
by 1 on each iteration.
-
Power Calculation:
m = pow(3, i)
: Thepow
function from the math library is used to calculate 3i3^i. The result is stored in the variablem
.
-
Printing the Result:
printf("3^%d = %d\n", i, m)
: Theprintf
function is used to print the result in the format "3^i = m", wherei
is the current loop counter andm
is the calculated power of 3.
#include #include int main() { int i, m; // Loop from 1 to 20 for(i = 1; i <= 20; i++) { // Calculate 3 raised to the power of i and cast to int m = (int)pow(3, i); // Print the result printf("3^%d = %d\n", i, m); } return 0; // Return success status }
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.