[ create a new paste ] login | about

Link: http://codepad.org/qLa46qvR    [ raw code | output | fork | 12 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 Justforyou on Nov 15
Cho e hỏi là cái dòng
s = s*x;
i++;
Tại sao lại là s*x và i++ vậy ???
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
posted by phamquanghoaz on Aug 19
cho e hỏi là nếu n < 0 thì lũy thừa nó sẽ ra 1/ x^n mà sao e code miết không ra, có ai chỉ e với
reply
posted by thongvien123 on Aug 2
#include<stdio.h>
#include<conio.h>

double Txn(int x, int n){
double T = 1;
double a = n;
if (n < 0)
{
a *= -1;
}
for (int i = 0; i < a; i++)
{
T *= x;
}
return T;
}

int main (){
double x;
printf("Enter x: ");
scanf("%lf", &x);
double n;
printf("Enter n: ");
scanf("%lf", &n);

double result = Txn(x, n);
double R = 1.0 / result;
if (n >= 0)
{
printf("\n=> T(%.3f^%.3f) = %.3f", x, n, result);
}else
{
printf("\n=> T(%.3f^%.3f) = %.3f", x, n, R);
}
getch();
return 0;
}
reply
posted by phaman151087@gmail.com on Aug 28
ai giai thich hộ e bài trên với. e là newbie. thank so much
reply
posted by vjpkerpoker on Jan 3
#include <stdio.h>
int main()
{
int x, n;
printf("nhap x:");
scanf("%d", &x);
printf("nhap n:");
scanf("%d", &n);
int sum = 1;
int a = 1;
while (a <= n)
{
sum = sum * x;
a++;
}
printf("%d^%d=%d", x, n, sum);
}

reply
posted by vjpkerpoker on Jan 3
#include <stdio.h>
int main()
{
int x, n;
printf("nhap x:");
scanf("%d", &x);
printf("nhap n:");
scanf("%d", &n);
int sum = 1;
int a = 1;
while (a <= n)
{
sum = sum * x;
a++;
}
printf("%d^%d=%d", x, n, sum);
}

reply
posted by B21DCVT339 on Aug 1
#include"stdio.h"
#include"math.h"
int main(){
double x,T;
scanf("%lf",&x);
int n;
scanf("%d",&n);
T=pow(x,n);
printf("%.2lf",T);
}
reply
posted by B21DCVT339 on Aug 1
#include"stdio.h"
#include"math.h"
int main(){
double x,T;
scanf("%lf",&x);
int n;
scanf("%d",&n);
T=pow(x,n);
printf("%.2lf",T);
}
reply