Nov 16, 2019 at 2:18pm UTC
The output of float variable doesn't give the fraction other decimal place
[code]
#include<iostream>
#include<process.h>
using namespace std;
int main()
{
char again;
do
{
int x;
label: cout<<"please enter number"<<endl;
cin>>x;
while (x<=2)
{
cout<<"Please enter number more than 2"<<endl;
cin>>x;
}
float z;
for(int i=x-1;i>=2;i--)
{
z = x % i;
if(z==0)
{
cout<<x<<"/"<<i<<"="<<z<<endl;
cout<<"this number "<<x<<"is not a primary number"<<endl;
cout<<"try another number?"<<" press 'y' for yes"<<endl;
cin>> again;
if(again=='y')
{
goto label;
}
else
{
exit (0);
}
}
z=x/i;
cout<<x <<"/"<<i <<"= "<<z<<endl; //why the output of z not fractional whilest it is declared as float?
}
cout<<"this number "<<x<<" is a primary number"<<endl;
cout<<"try another number?"<<" press 'y' for yes"<<endl;
cin>>again;
}while (again=='y');
return(0);
}
/code]
Last edited on Nov 16, 2019 at 3:32pm UTC
Nov 16, 2019 at 2:41pm UTC
It would help us if you posted a full program as an example, so that it would actually compile, so that we can see what you're talking about.
Last edited on Nov 16, 2019 at 2:42pm UTC
Nov 16, 2019 at 3:47pm UTC
here you are the whole code, it is a program to examine if the number is a primary or not and ask the user if he needs to try again.
thanks for your help
Nov 16, 2019 at 4:38pm UTC
Hello Ganado
I sent the complete code
Nov 16, 2019 at 7:08pm UTC
If I'm understanding your issue correctly... when you have two ints, and you divide them, in C++ this does integer division, so the result is a whole number.
If you want "3/4" to be 0.75 instead of 0, then you have to convert one of the operands to a float.
e.g. static_cast <float >(numerator) / denominator
Nov 16, 2019 at 7:12pm UTC
Thank you Ganado, I appreciate your help