C program to get the integer and fraction or decimal part

C program to get the integer and fraction or decimal part:

In this post, we will learn how to get the integer and fraction or decimal part from a given floating-point number in C. Our program will take the floating-point number as input from the user and it will print the integer part and the fraction part.

We can solve this problem in different ways in C. I will show you two different ways in this article.

Method 1: By casting the number to an integer:

In this method, we can cast the number to an integer by using (int) typecasting. Once we get the integer part, we can easily calculate the decimal part by finding the difference between the given number and the integer part.

For example, for the number 12.3456, we can get the integer part 12 with typecasting. If we subtract this from the original number, 12.3456 - 12, it will give the decimal part .3456.

Below is the complete program:

#include <stdio.h>

int main()n
{
    float num;

    printf("Enter a number: ");
    scanf("%f", &num);

    int num_integer = (int)num;
    float num_decimal = num - num_integer;

    printf("Integer part: %d, Decimal part: %f\n", num_integer, num_decimal);

    return 0;
}

Download the code from GitHub

In this program,

  • The num is a floating-point variable to store the user input number.
  • It asks the user to enter a number, read it and store it in the num variable.
  • The num_integer variable is used to store the integer part of the number num. We are casting it to an integer to get the integer part.
  • The num_decimal variable is used to store the decimal or fraction part of the number. We are subtracting the value of num_integer from num to get this value.
  • The last printf statement is printing both integer and decimal part.

If you run this program, it will print the output as below:

Enter a number: 12.3456
Integer part: 12, Decimal part: 0.345600

Enter a number: 99.99
Integer part: 99, Decimal part: 0.989998

Enter a number: 12.45
Integer part: 12, Decimal part: 0.450000

C program to get the integer and fraction or decimal part example

Method 2: By using the modf function:

modf is an inbuilt function defined in the math.h header file. We can use this function to convert a floating point variable to an integer and fractional or decimal part.

The syntax of the modf function is:

double modf(double v, double *p)
  • The parameter v is the floating-point value we want to divide into fractional and decimal parts.
  • The parameter p is a pointer to store the integer part.

It returns the fractional part of v.

Example C program with modf:

The below program uses modf to get the integer and fractional part from a given number.

#include <stdio.h>
#include <math.h>

int main()
{
	double num, integerValue, decimalValue;

	printf("Enter a number: ");
	scanf("%lf", &num);

	decimalValue = modf(num, &integerValue);

	printf("Integer part: %lf, Decimal part: %lf\n", integerValue, decimalValue);

	return 0;
}

Download the code from GitHub

Here,

  • The num, integerValue and decimalValue are three double variables to hold the user input number, the integer part and the decimal part of the number respectively.
  • It asks the user to enter a number and reads it in the num variable. We are using the scanf method to read the user input number.
  • By using the modf function, the program finds the integer and decimal parts of the number num. These values are assigned to the integerValue and decimalValue double variables.
  • The last printf is printing the calculated integer part, and decimal part of the number.

If you run this program, it will print the output as below:

Enter a number: 12.3456
Integer part: 12.000000, Decimal part: 0.345600

Enter a number: 99.99
Integer part: 99.000000, Decimal part: 0.990000

Enter a number: 12.45
Integer part: 12.000000, Decimal part: 0.450000

C program to get the integer and fraction or decimal part example

You might also like: