M3-R4: January 2014 (O-Level)
Q. 5(b):
Write a ‘C’ program to find the power of number using function.
Ans:
Finding power of a number using function.
#include <stdio.h>
#include <conio.h>
long power(int, int);
void main(){
int x, n;
long result;
printf("\nEnter a number: ");
scanf("%d", &x);
printf("\nEnter power: ");
scanf("%d", &n);
result = power(x,n);
printf("\n%d to the power %d is %ld",x,n,result);
getch();
}
long power(int x, int n){
int i;
long prod=1;
for(i=0; i<n; i++){
prod = prod * x;
}
return prod;
}
Q. 5(b):
Write a ‘C’ program to find the power of number using function.
Ans:
Finding power of a number using function.
#include <stdio.h>
#include <conio.h>
long power(int, int);
void main(){
int x, n;
long result;
printf("\nEnter a number: ");
scanf("%d", &x);
printf("\nEnter power: ");
scanf("%d", &n);
result = power(x,n);
printf("\n%d to the power %d is %ld",x,n,result);
getch();
}
long power(int x, int n){
int i;
long prod=1;
for(i=0; i<n; i++){
prod = prod * x;
}
return prod;
}
No comments:
Post a Comment