Respuesta :
Answer:
There are 2 ways to do extract the decimal part:
Explanation:
- First method using number
int main() {
double num = 23.345;
int intpart = (int)num;
double decpart = num - intpart;
printf(""Num = %f, intpart = %d, decpart = %f\n"", num, intpart, decpart);
}
- Second method using string:
#include <stdlib.h>
int main()
{
char* inStr = ""123.4567"";
char* endptr;
char* loc = strchr(inStr, '.');
long mantissa = strtod(loc+1, endptr);
long whole = strtod(inStr, endptr);
printf(""whole: %d \n"", whole);
printf(""mantissa: %d"", mantissa);
}
Answer:
The program to this question can be given as:
Program:
#include <iostream> //header file
using namespace std;
void cal(double number) //defining method cal
{
double decimal_part; //defining variable
int integer_part; //defining variable
integer_part = (int)number; //type casting
decimal_part = number - integer_part; //holding decimal value
cout<<"number you inserted :"<<number<<endl; //print value
cout<<"integer part :"<<integer_part<<endl; //print integer part
cout<<"decimal part :"<<decimal_part<<endl; // print decimal part
}
int main() //defining main method
{
double number; //defining variable number
cout<<"Input floating point number :"; //message
cin>>number; //taking input from user
cal(number); //calling function and passing value in function
return 0;
}
Output:
Input floating point number :786.786
number you inserted :786.786
integer part :786
decimal part :0.786
Explanation:
- In the above C++ language program, firstly include a header file then define a method that is "cal". This method accepts a double parameter and returns nothing because its return type is void.
- Inside a method, two variable is defined that is "integer_part and decimal_part". In which variable integer_part is an integer variable that holds only integer value and variable decimal_part is used to holds decimal value only and to print function calculated value the cout (print function).
- In the main method, a variable number defines that datatype is a double type that accepts a decimal or floating-point value from user input and passes the value in the function parameter and call the function.