Exponentiation using multiplication
#include
#include
int exp (int b, int e)
{
int result;
result = 1;
while (e != 0)
{
result = result * b;
e = e - 1;
}
return(result);
}
int main ()
{
int b, e;
cout << "Enter base and exponent: ";
cin >> b >> e;
cout << b << " to the " << e << " = " << exp(b,e) << endl;
return(0);
}
Weird behavior from C++ compiler
#include
#include
typedef int bool;
const bool true = 1;
const bool false = 0;
void main ()
{
int zero = 0;
int two = 2;
int three = 3;
cout << (false && (1/zero == 5)) << endl;
cout << ((1/zero == 5) && false) << endl;
cout << ((1/zero == 5) && (two == three)) << endl;
}
More weird C++
#include
#include
void main ()
{
int i,j,k;
i=10; j=20; k=30;
cin >> i >> j >> k;
cout << i << endl << j << endl << k << endl;
}
Convert temperatures
#include
#include
float f (float x)
{
return ((9.0 / 5.0) * x + 32.0);
}
void main ()
{
float x, res;
cout << "Enter x: ";
cin >> x;
res = f(x);
cout << "Result = " << res << endl;
}
No comments:
Post a Comment