[ create a new paste ] login | about

Link: http://codepad.org/qLa46qvR    [ raw code | output | fork | 4 comments ]

C, pasted on Aug 17:
#include<stdio.h>
#include<conio.h>


double Power_n(double x, long n)
{
	// n >= 0
	double result = 1;
	while(n--)
	{
		result = result * x;
	}
	return result;
}
double qPower_n(double x, long n)
{
	// n >= 0
	double result = 1;
	while(n)
	{
		if(n % 2 == 1)
		{
			result = result * x;
		}
		x = x * x;
		n = n / 2;
	}
	return result;
}
int main()
{
	double x = 3;
	long n = 2;
	double z;
	z = qPower_n(x, n);
	printf("z = %f", z);

	getch();
	return 0;
}


Output:
1
Line 17: error: conio.h: No such file or directory


Create a new paste based on this one


Comments:
posted by Tuankhoilua on May 5
#include <iostream>
using namespace std;

int Mu(int a, int b)
{
if(b == 1)
return a;
else
{
return Mu(a, b - 1)*a;
}
}
int main ()
{
// Tinh t(x,n)=x^n
int Tn = 0, x, n;
cout << "Nhap x ";
cin >> x;
cout << "Nhap n ";
cin >> n;

cout << "T(n) = " << Mu(x ,n) << endl;

system ("pause");
return 0;
}
reply
posted by khanhtienfz on Jun 24
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int n;
do{
printf("\nNhap n = ");
scanf_s("%d", &n);
} while (n < 0 && printf("\nXin vui long nhap lai "));
int x;
printf("\nnhap vao x =");
scanf_s("%d", &x);

int i=1,s = 1;
while (i<= n)
{
s = s*x;
i++;
}
printf("s=%d", s);
//Hoặc có thể dung hàm pow trong thư viện math.h
//Hàm pow <=> lũy thừa
//Ví dụ x^n=pow(x,n)

printf("\n\n");
system("pause");
return 0;
}
reply
posted by truongstctgi on Aug 5

reply
posted by hochoi1368 on Dec 3
#include<iostream>
#include<conio.h>
#include<math.h>

using namespace std;

int main()
{
double x,n;
double T;

do{
cout << "nhap x: ";
cin >> x;
if(x==0){
cout << "moi ban nhap lai!\n";
}
}while(x==0);

cout << "nhap n:";
cin >> n;

T = pow(x,n);

cout <<x<< "^"<<n<< " = "<<T<<endl;

return 0;
}

reply