Only getting up to 1 decimal place in result [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
In the code below I wanted the answer to be up to 2 decimal places but the result only has 1 decimal place. I already used the setprecision.
#include <iostream>
using namespace std;
int main()
{
int a;
int b;
cin >> a >> b;
if (b > a && a % 5 == 0)
{
cout << ((b - 0.50) - a);
}
else
{
cout << b;
}
}
How can I get 2 decimal places in the answer?

I already used the setprecision.
Certainly you are not using it right, for two decimal places, regarldess of the integral part, you need to use std::fixed.
Example:
//...
if (b > a && a % 5 == 0)
{
std::cout << std::fixed << std::setprecision(2) << ((b - 0.50) - a);
// ^^^^^^^^^^
}
//...
Sample exec:
Input:
5
10
Output:
4.50

Related

iam writing a integer check in c++ language [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
i wrote a function that check if the variable is integer or not but if I put double or float it give me that it`s integer can anyone help me
bool check(int a) {
if (a == (int)a) {
cout << "integer" << endl;
}
else {
cout << "not integer" << endl;
}
return 1 ;
}
void main() {
int a;
cout << "enter a number" << endl;
cin >> a;
check(a);
}
You are taking in an integer value as an argument:
bool check(int a) // int a
So for example, if you pass in 3.12 into check(), then it will get converted to 3. That's why:
a == (int)a
.. will mean:
3 == (int)3 // true
..while to get the correct result, it should be:
3.12 == (int)3 // false
So to fix your issue, just change the parameter type:
bool check(double a)
..and also the input variable type:
double a;
Samples:
enter a number
3
integer
enter a number
3.12
not integer

Expect four printouts from while loop, getting only one [closed]

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 2 years ago.
Improve this question
I'm trying to write a program using a while loop that gives an output based on range of input variable declared.
#include <iostream>
using namespace std;
int main() {
float fahren = 0;
float celsius;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
cout << celsius;
fahren+= 100;
}
return 0;
}
I expect 4 values to be returned, however I only get 1, which is the value at 0.
You print 4 values but without separator, you might add std::cout << std::endl;
while (fahren < 400) {
// convert fahreneheit to celsius
// Subtract 32, then multiply it by 5, then divide by 9
celsius = 5 * (fahren - 32) / 9;
std::cout << celsius << std::endl; // added new line here
fahren += 100;
}
Demo

Enhanced if statement with two (different) variable comparison operators [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I wanted to make a simple enhanced if statement (that is the correct definition right?) when a == 1 OR b > 2 it prints out how to say how are you sir in Arabic.
Is it possible to compare two different variables in an if and else if statement? I got myself in a Chinese finger trap with all the ('s and )'s and the different types of logical operators.
Here is my c++ code:
#include <iostream>
using namespace std;
//If / Else statement is basically a enhanced if statement.
int main()
{
int a = 1;
int b = 2;
if ((a==1)||(b>2)){
cout << "Kevak chala komm?" << endl;
}
if (((else)) (a == 1) && (b == 2)))) {
cout << "Louis C.K. is back my brothers!" << endl;
}
else{
cout << "Jek shi mash? " << endl; // How are you in polish.
}
return 0;
}
The correct syntax for that looks like this:
if (a == 1 || b > 2 ){
std::cout << "A" << std::endl;
}
else if (a == 1 && b == 2) {
std::cout << "B" << std::endl;
}
else {
std::cout << "C" << std::endl;
}
But it doesn't make any sense in your particular case, since B will never be printed (if a == 1, the first clause will hit, never using the second).
Is it possible to compare two different variables in an if and else if statement?
Yes it is very much possible, you can compare two or more different variables in if and else if stetement and also there can be multiple else if statement as well.
The below code line is wrong
if (((else)) (a == 1) && (b == 2)))) {
write else if instead.

How to find out if n! can be expressed as a multiplication of three consecutive numbers? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Hey I have gotten this far with my code
#include
using namespace std;
int factorial(int n);
int main()
{
int n;
cout << "Enter a positive integer: ";
cin >> n;
cout << "Factorial of " << n << " = " << factorial(n);
return 0;
}
int factorial(int n)
{
if(n > 1)
return n * factorial(n - 1);
else
return 1;
}
Now I need to check if the factorial can be a multiplication of three consecutive numbers, and I am really stuck on that, thanks for help in advance!!
I don't think that except for 3!, 4! or 5! there are such numbers.
Still, if you want to look for these numbers:
int hint = std::pow(fact, 1./3.);
for (int trial = -10;, trial < 11; ++trial)
{
// check if (hint + trial - 1) * (hint + trial) * (hint + trial + 1) is equal to your result
}
I use 21 values to check, it's too much, but it's the idea.

fill missing places in c++ program to get desired output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Fill up the blanks with appropriate keyword to get the desired output according to the test cases. (Language use c++)
Sample Test Cases
input output
Test Case 1 4 square = 16, ++ square = 25
test case 2 -8 square =64, ++ square = 49
#include <iostream>
using namespace std;
______ int SQUARE(int x) { ______ x * x; }
​
int main() {
int a , b, c;
cin >> a ;
b = SQUARE(a);
cout << "Square = " << b << ", ";
c = SQUARE(++a);
cout << "++ Square = " << c ;
return 0;
}
Second blank place is "return"
First may be "inline" or nothing