Help Tia to extract decimals
Tia is new to programming. She needs to write a program which will receive the input as a floating point number. She needs to print the decimal part of the floating point of the number. She has written a few lines of code. Can you fill up the rest.
Read the input as string. Use string operations on the input to derive the answer easily
Answers
Answer:
#include<iostream>
#include<string>
#include<math.h>
using namespace std;
int main(){
string num;
string substr=".";
cin>>num;
if (num.find(substr) != string::npos) {
size_t pos = num.find('.');
string str3 = num.substr (pos+1);
cout<<"Floating part is : "<<str3;
}
else
cout<<"Floating part is : ";
}
Explanation:
To extract the decimals of the floating-point of the number the rules and the c.ode can look as follows
- Define- Here we use "num" to store our input value and to calculate the value. "a" is defined as the integer part. "i" denotes the integer part and "f" provides the floating value.
- So the c.ode be like-
int main()
{
float num,a;
int integer;
printf("\n Enter a number with 2 decimal digit:");
scanf("\n%f",&num);
integer=num;
a= num-integer;
printf("\n Number=%f",num);
printf("\n Integer part=%i",integer);
printf("\n Decimal part=%f",a);
}
- Output - Hence, if the entered value is 25.55568, the output be as,
Enter a number with 2 decimal digit:25.55568.
Number=25.555679
Integer part=25
Decimal part=0.555679
#SPJ3