Separate number from decimal place

Do not miss this exclusive book on Binary Tree Problems. Get it now for free.

In this article, we will solve the problem of separation of number from before the decimal and after and provide the implementation in C programming language.

Table of contents:

  1. Problem Statement
  2. Solution
  3. Implementation

Problem Statement

In this problem we will take a floating point number and separate it into two part. One that comes before decimal point and onr that comes after

Example: 19.26
output
19
26

Solution

First we take the input from the user and store it in a float data type for example in a.
int data type can store only whole numbers so we create an int data type b and store a in b.
now that part of the number before the decimal point is stored in b. for example if the input from the user is 23.94, then 23 is stored in b.
To get the part after the decimal point, we have to subtract b from a and store it in a and then multiply it with 100. in the above example we get 0.94 after subtracting and then 94 after multiplying it with 100 which is the desired output.
now we print b and a.

Implementation

Following is the implementation in C programming language

#include<stdio.h>
int main()
{
    float a,c;
    int b,d;
    printf("Enter a real number=");
    scanf("%f",&a);
    b=a;
    printf("%d\n",b);
    c=a-b;
    c*=100;
    d=c;
    printf("%d",d);
    return 0;
}

Output
Enter a real number=34.56
34
56

Sign up for FREE 3 months of Amazon Music. YOU MUST NOT MISS.