I want to add square of 2 numbers and get the average...........
but dont know to get the square of a number in C
please post the keyword to get square or cube.........thanks
I want to add square of 2 numbers and get the average...........
but dont know to get the square of a number in C
please post the keyword to get square or cube.........thanks
Code:File Edit Options Buffers Tools C Help #include <stdio.h> #include <math.h> int main() { int a = 2; int b = 3; int square = pow(a, 2) + pow(b, 2); int average = square / 2; printf("square == %d || average == %d\n", square, average); return 0; }
How does using pow really help? If they're just squaring, they don't need a function for it. Square a number in C the same way you do in math. Multiply it by itself.Code:foosquared = foo * foo;
Quzah.
Hope is the first step on the road to disappointment.
He mentioned getting the cube of a number also. I inferred from that, and the the tone of his post, that he didn't want to just use number * number, for his answers.
By this if he want to do x^n then he have to write
Code:const int ans = x * x * x * x * x ............ n times of what ?????
You could just use a loop.Code:for( x = 0, end = start; x < pow; x++ ) end *= start;
Quzah.
Hope is the first step on the road to disappointment.
wat is the use of pow anyway?..just newby
pow
Grab a book or a man page. Or use a search engine. Sheesh.
Quzah.
Hope is the first step on the road to disappointment.
pow() is an API provided by <math.h>
lets say u want to do 7 ^ 8 then u just simply call this API pow(7, 8)
u have to decide which is better
ORCode:int result = 1; for (unsigned int index = 0; index < 8; ++index) { result *= 7; }
unsigned int result = pow(7, 8);
or on shell just say man powCode:
There is a third option: use a version of pow for integral exponents that computes x^n more efficiently than just multiplying x n times.Originally Posted by RockyMarrone
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Use a lookup table!
Quzah.
Hope is the first step on the road to disappointment.
That is probably not generally feasible though... but if we want to actually compute 7^8 at compile time, I'll probably either whip out my calculator if I cannot (be bothered to) do it mentally, or just bite and useOriginally Posted by quzah
Code:const int foo = 7 * 7 * 7 * 7 * 7 * 7 * 7 * 7;
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
even i dont understand why i will go for lookup table as i m having the reach to math.h
Hope is the first step on the road to disappointment.