I don't know why my code is giving to me incorrect results.
When I put a number like 6670680902 the result is 6.67068e+0.7 (which is 66706800). That is not the correct result.
When I use the calculator the correct result of 667006080902 / 100 is 66706809.02.
What should I do to fix it?
#include "stdafx.h"
#include "conio.h"
#include "iostream"
using namespace System;
using namespace std;
int main()
{
float a;
float b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout << "result: " << b;
_getch();
return 0;
}
The first issue here is by default C++ will show larger numbers using scientific notation, there are ways to prevent this for floating point numbers like floats. One simple way is to add << fixed before your number:
cout << "result: " << fixed << b;
Which will return 66706812.0.
The next problem is that floats are not good at being precise, which is why the number still isn't correct. Floats are less precise compared to something like a double which has twice the precision. If you use a double instead for a and b:
int main()
{
double a;
double b;
//...
cout << "result: " << fixed << b;
//...
}
you will get the value you expect: 66706809.02
Can do by using 'limits`
#include "iostream"
#include <string>
#include <limits>
using namespace System;
using namespace std;
int main()
{
double a;
double b;
cout << "Ingrese el codigo: "; cin >> a;
b = a / 100;
cout.precision(numeric_limits<double>::digits10 + 1);
cout << "result: " << b << endl;
_getch();
return 0;
}
Output:
result: 66706809.02
Related
I want to display 265612.2 how ever the output still 265612
I input : 4.2
#include <iostream>
using namespace std;
int main()
{
double a;
double b;
cout<<"Enter the number of light year : ";
cin>>a;
b = a * 63241;
cout<<b;
return 0;
}
You need to "manipulate" the output stream (std::cout):
#include <iomanip> // manipulating helpers
#include <iostream>
int main() {
double a;
double b;
std::cout<<"Enter the number of light year : ";
if(std::cin >> a) {
std::cout << std::setprecision(1) // one decimal
<< std::fixed; // fixed precision
b = a * 63241.01;
std::cout << b;
}
}
Demo
What is the difference between :
#define PI 3.14
using namespace std;
int main()
{
int r;
float area;
cout << "Enter the radius:";
cin>>r;
area=r*r*PI;
cout << area << endl;
}
and
#define PI 3.14
using namespace std;
int main()
{
int r;
cout << "Enter the radius:";
cin>>r;
cout << r*r*PI <<endl;
}
The answer is in your case it changes nothing.
You can always use godbolt to take a look at the asm.
https://godbolt.org/z/wkFHM5
As you can see
area=r*r*PI;
cout << area << endl;
produces the same code as
cout << r*r*PI << endl;
The intermediate variable is a way and storing data that you want to reuse that is all.
//This is my code so far
//It is in microsoft studio visual
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
using namespace std;
int main()
{
int a, b;
cout << "Input a whole number:"; //The following lines are to make it interactive
cin >> a;
cout << "Input a whole number:";
cin >> b;
int result = 1;
for (int i = 0; i < b; i++)
result *= a;
cout << "The Answer is:" << result;
cout << "\n";
system("PAUSE");
}
//How do I expand the power of it to handle more?
I just need to increase the power of it to handle more. I need this for a class and I can't quite figure it out. I met the qualifications, but I want it to be more powerful.
Im writing this program from "Programming and principles with C++" and i need to write a program that takes too integers and finds the sum, difference, greater and less than value, and ratio.
For some reason i can't get greater than and less than to work. It doesn't actually perform the function. It just simple prints the numbers ie: 4 will be less than 2.
My second problem is how do i write a equation that will do ratios for me?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open() {char ch;cin>>ch;}
int main()
{
int a;
int b;
cout<<"Enter two values.\n";
cin>>a; cin>>b;
if (a > b);cout<< a << " Is greater than " << b << "\n";
if(a < b);cout<< a << " Is less than " << b << "\n";
cout<<a << " plus " << b << " is " << a+b << "\n";
cout<<a << " minus " << b << " is " << a-b << "\n";
keep_window_open();
return 0;
}
First you need to remove the semi colons after if (a>b) and if (a < b).
To do ratios, I suggest finding the greatest common factor between a and b, and then executing the following line:
cout<<"Ratio of "<<a<<" and "<<b<<" is "<<(a/gcd)<<":"<<(b/gcd);
Where gcd is the greatest common factor of a and b.
I think the following code snippet answers all your questions.
#include<iostream.h>
void main()
{
float a,b;
cout<<"Enter 2 numbers";
cin>>a>>b;
cout<<"Plus = "<<(a+b)<<"\n";
cout<<"Minus = "<<(a-b)<<"\n";
cout<<"Greater = "<<((a>b)?a:b)<<"\n";
cout<<"Smaller = "<<((a<b)?a:b)<<"\n";
cout<<"Ratio = 1:"<<(1/(((a<b)?a:b)/((a>b)?a:b)));
}
long double m;
cout << "enter double: "; cin >> m;
cout << "m = " << m <<endl;
Input:
enter double: 1.546640625
Output:
m = 1.54664
I have to convert into a binary with point, and when I read numbers like 2.359375000
Output:
m = 2.35938
And it works, but I think the problem is the zero in 1.546640625
You have read the whole value of the double. The problem is with the cout. It by default rounds the value to 6 digits after the decimal point.
To set the precision cout uses, use setprecision from <iomanip>:
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
long double d;
cin >> d;
cout << setprecision(10) << d << endl;
return 0;
}