hi there I'm trying to write a program that will only multiply the decimal in the number i enter by 9. so for example if i enter 2.345 the program will only multiply .345 by 9 and ignore the 2, i have a basic program for multiplication but i can not figure out how to modify it to get it to do this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main()
{
double firstNumber, answer;
cout << "Enter a number above 0: ";
// Stores two floating point numbers in variable firstNumber and secondNumber respectively
cin >> firstNumber;
// Performs multiplication and stores the result in variable productOfTwoNumbers
answer = firstNumber * 9;
cout << "Product = " << answer;
return 0;
}
where about would I insert it into the code. i have tried in a few places but i keep getting errors. I'm fairly new to c++ so don't have much of a grasp on it yet. thanks for the help
The <cmath> library also has floor (and ceil) functions to round down (or up) a number to the nearest integer. If you subtracted the floor of the original number from itself, you'd be left with the fractional part.
that's great thanks, I'm after finding a code and have it doing close to what i want, so now I'm just trying to get the program to multiply the decimal point by 9 and ignore the rest.
code I'm after finding is
#include <iostream>
#include <iomanip>
int main()
{
printf("enter number");
float f=1;
while (f)
{
std::cin >> f;
int i = (int)f;
std::cout << i << '\t' << std::setprecision(3) << f - i << std::endl;
}
return 0;
}
I like learning how to do things without library functions. It helps me understand what is going on in the code. This is probably a noob answer but I can do the calculation you are looking for like this:
I do not think anything is wrong with @texasbeef results. He was showing a way that does not use cmath floor, but achieves the similar method and same result.