find pow(2,5), pow(3,6), and pow(10,3) - without using any function
#include<stdio.h>
int main(void)
{
int x, y, p=1,i;
x = 2;
y = 5;
for(i=1; i<=y; i++)
{
p = p*x;
}
printf("%d power of %d is %d\n",x,y,p);
x = 3;
y = 6;
p=1;
for(i=1; i<=y; i++)
{
p = p*x;
}
printf("%d power of %d is %d\n",x,y,p);
x = 10;
y = 3;
p=1;
for(i=1; i<=y; i++)
{
p = p*x;
}
printf("%d power of %d is %d\n",x,y,p);
return 0;
}
find pow(2,5), pow(3,6), and pow(10,3) - with using functions in 1 file
#include <stdio.h> //function declaration
int power (int x, int y); //function declaration
int main(void)
{
int a, b, r;
a = 2;
b = 5;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
a = 3;
b = 6;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
a = 10;
b = 3;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
return 0;
}
int power (int x, int y) // function definition
{
int p=1, i;
for(i=1; i<=y; i++) //i is 6, p is 32
{
p = p*x; //16*2
}
return p;
}
#include <stdio.h> //function declaration
int power (int x, int y); //function declaration
void welcome (void);
int main(void)
{
int a, b, r;
welcome();
a = 2;
b = 5;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
a = 3;
b = 6;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
a = 10;
b = 3;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
return 0;
}
int power (int x, int y) // function definition
{
int p=1, i;
for(i=1; i<=y; i++) //i is 6, p is 32
{
p = p*x; //16*2
}
return p;
}
void welcome (void)
{
printf("hello, welcome to c programming!\n");
}
find pow(2,5), pow(3,6), and pow(10,3) - with using functions in different files
main.c
--------
#include <stdio.h> //function declaration
#include "Mylib.h"
int main(void)
{
int a, b, r;
welcome();
a = 2;
b = 5;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
a = 3;
b = 6;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
a = 10;
b = 3;
r = power(a,b); //function call
printf("%d power of %d is %d\n",a,b,r); //function call
return 0;
}
Mylib.c
--------
int power (int x, int y) // function definition
{
int p=1, i;
for(i=1; i<=y; i++) //i is 6, p is 32
{
p = p*x; //16*2
}
return p;
}
void welcome (void)
{
printf("hello, welcome to c programming!\n");
}
Mylib.h
---------
extern int power (int x, int y); //function declaration
extern void welcome (void);
recursion ==> a function calls itself
repetitive in nature:
iteration: loops
recursion: function calls itself
factorial ==>
5! ==> 5*4*3*2*1 ==> 5*4!
4! ==> 4*3*2*1
n! ==> n*(n-1)!
f ==>120
1! ==> 1
0! ==> 1
-3! ==> undefined
#include <stdio.h>
int main (void)
{
int n = 6;
int i;
int f=1;
for(i=n; i>=1 ;i--)
{
f=f*i;
}
printf("%d! is %d\n",n, f);
return 0;
}
Iteration:
#include <stdio.h>
int factorial (int n);
int main (void)
{
int x = 5,fact;
fact = factorial(x);
printf("%d! is %d\n",x, fact);
return 0;
}
int factorial (int n)
{
int i;
int f=1;
for(i=n; i>=1 ;i--)
{
f=f*i;
}
return f;
}
2. Recursion:
#include <stdio.h>
int factorial (int n);
int main (void)
{
int x = 6,fact;
fact = factorial(x);
printf("%d! is %d\n",x, fact);
return 0;
}
int factorial (int n)
{
if(n==0 || n==1)
{
return 1;
}
else
{
return n*factorial(n-1);
}
}