This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 5 years ago.
Whenever i write 5 as n and p as 2 ,i get the output as 24...please let me know what's wrong? for other numbers it is completely fine.
#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
int main()
{
double n, p;
cout << "enter the number" <<endl;
cin >> n;
cout << "enter the power" <<endl;
cin >> p;
int result = pow(n, p);
cout << "Result is " << result;
return 0;
}
You have problems with your data types!
double n, p; // here you use double types
std::cout << "enter the number" << std::endl;
std::cin >> n;
std::cout << "enter the power" << std::endl;
std::cin >> p;
int result = pow(n, p); // then here you use double pow(double, double) but assign it to an int
std::cout << "Result is " << std::result;
Solution:
double result = pow(n, p);
Related
This question already has answers here:
How do I print a double value with full precision using cout?
(17 answers)
Closed 2 years ago.
I don't need to do any calculations just print out the numbers.
double n1 = 1000000.5985;
double n2 = 9999999.0;
double n3 = 678300.893;
When I try
cout << n1 << n2 << n3;
I get 1e+006 1e+007 678301
How do I get it to print the whole number without converting to a string?
Use setprecision() from <iomanip>.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double f = 1000000.5985;
cout << f << endl;
cout << fixed << setprecision(6) << f <<endl;
cout << fixed << setprecision(5) << f << endl;
cout << fixed << setprecision(9) << f << endl;
return 0;
}
Output is
1e+06
1000000.598500
1000000.59850
1000000.598500000
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
There is a program I'm building in which I need to calculate torque and diameter. I have both equations in syntax but my output keeps being zero. What am I missing / doing wrong? (Disclaimer: I'm very new to this so please, I welcome constructive criticism! Anything to help me get better. Thanks).
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
//Output:
/*Enter values for horsepower (p), rpm (n) and shear strength (s): 20 1500 5000
HP 20
rpm 0
psi 0
torque inf
diameter inf */
The intent of the lines
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
cin >> p, n, s;
is not correctly represented in code.
The second line does not read anything into n or s. It's an expression that uses the comma operator. In this particular case, it evaluates n and s and discards the values.
It's as if you have:
cin >> p;
n;
s;
Change that line to:
cin >> p >> n >> s;
You can also be verbose and use:
cin >> p;
cin >> n;
cin >> s;
Take your input like this (cin >> p >> n >> s) way and also print your result after result calculation you are are printing before.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
double p, n, s, t, d;
int main()
{
cout << "Enter values for horsepower (p), rpm (n) and shear strength(s): ";
// cin >> p, n, s;
cin >> p >> n >> s;
cout << setw(10) << "HP " << p << endl;
cout << setw(10) << "rpm " << n << endl;
cout << setw(10) << "psi " << s << endl;
t = 6300 * p / n;
d = pow((16 * t) / s, 0.333);
cout << setw(10) << "torque " << t << endl;
cout << setw(10) << "diameter " << d << endl;
return 0;
}
This question already has answers here:
Comma operator in condition of loop in C
(5 answers)
Closed 7 years ago.
Guys i'm trying to output the sum of all the odd numbers between a and b. Despite the conditions given, there would always be numbers smaller than 'a' outputted.
#include <iostream>
#include <string>
int main()
{
int a =0;
int b =0;
int c=0;
std:: cout <<"Enter a number: \n";
std::cin >> a;
std:: cout <<"Enter a number: \n";
std::cin >> b;
int sum=0;
for(c; c%2!=0, c >= a, c <=b ; c+=2)
{
std::cout << "This is one of the odd numbers between " << a << " and " << b << " : "<< c << std::endl;
sum +=c;
}
std::cout << "The sum is : " <<sum;
return 3;
}
Initialize the value of c with a
Try this
for(c=a; c%2!=0 && c >= a && c <=b ; c++)
{
std::cout << "This is one of the odd numbers between " << a << " and " << b << " : "<< c << std::endl;
sum +=c;
}
This question already has answers here:
How to return an array from a function?
(5 answers)
Closed 7 years ago.
I try to return an array from function in C++.
I made a very easy function to demonstrate it.
#include<iostream>
using namespace std;
int OneDimensional();
void main()
{
int arr[3];
arr = OneDimensional();
cout<<"arr = " << arr[0] <<endl;
cin.get(); cin.get();
}
int OneDimensional()
{
int arr[3];
cout << "Enter a number" <<endl;
cin >> arr[0];
cout << "Enter a number" <<endl;
cin >> arr[1];
cout << "Enter a number" <<endl;
cin >> arr[2];
return arr;
}
But it fails with a lot of errors.
you need to use a pointer.
int * OneDimensional()
{
static int arr[3];
cout << "Enter a number" <<endl;
cin >> arr[0];
cout << "Enter a number" <<endl;
cin >> arr[1];
cout << "Enter a number" <<endl;
cin >> arr[2];
return arr;
}
void main()
{
int *arr;
arr = OneDimensional();
for (int i = 0; i < length; i++ )// length is number of elements in array
{
cout<<"arr = "<< *(arr + i) << endl;
}
}
Check the example here
int OneDimensional() return only a int value, not an array.
main() doesn't have a return
You need read more about C or C++.
Goold Luck!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Why does this code compile and work properly
#include <iostream>
int main()
{
using namespace std;
unsigned short int myInt = 99;
unsigned short int * pMark = 0;
cout << myInt << endl;
pMark = &myInt;
*pMark = 11;
cout << "*pMark:\t" << *pMark << "\nmyInt:\t" << myInt << endl;
return 0;
}
When this one doesn't compile:
#include<iostream>
using namespace std;
int addnumber(int *p, int *q){
cout << *p = 12 << endl;
cout << *q = 14 << endl;
}
int main()
{
int i , j;
cout << "enter the value of first number";
cin >> i;
cout << "enter the value of second number";
cin >> j;
addnumber(&i, &j);
cout << i << endl;
cout << j << endl;
}
In both the code snippets, I am assigning *pointer=somevalue. In the first code, it does not cause an error, but in the second, it causes error in the line
cout << *p = 12 << endl;
cout << *q = 14 << endl;
What mistake I am making?
Because of operator precedence.
To the compiler, your statement looks like
(cout << *p) = (12 << endl);