C program to print rightmost digit of integral left-most digit of real part

Write a C program to read a floating-point number from the standard input and print the rightmost digit of an integral part and the left-most digit of the real part.

Problem Definition:

In this program, we will read a floating-point number from the keyboard. Next, we find the rightmost digit of an integral part and the left-most digit of the real part and display the same.

Source code of C program to print rightmost digit of integral left-most digit of real part

#include<stdio.h>
#define EXIT_SUCCESS 0
int main()
{
  float num,real_part;
  int int_part,r_num,i_num;

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

  int_part=num;
  real_part=num-int_part;

  printf("\n The integral part is %d",int_part);
  printf("\n Real part is %f",real_part);

  i_num=int_part%10;   //gives the left most digit of real part

  real_part=real_part*10; //gives the rightmost digit of integer part
  r_num=(int)real_part;

  printf("\n The rightmost digit of an integral part is %d",i_num);
  printf("\n The leftmost digit of the real part is %d",r_num);

  return EXIT_SUCCESS;
}

Output:

Case 1:

Enter floating type number: 123.456

See also  C program to read diameter and compute Area Perimeter

The integral part is 123

The real part is 0.456001

The rightmost digit of an integral part is 3

The leftmost digit of the real part is 4

Case 2:

Enter floating type number: 321.456

The integral part is 321

The real part is 0.455994

The rightmost digit of an integral part is 1

The leftmost digit of the real part is 4

Program Explanation:

1. First, create 2 variables such as num, real_part of floating-point type, and 2 variables int_part, and r_num,i_num of integer type.

2. Next, ask the user to enter a floating-point number.

3. Find the integer and real part of the floating-point number and display the result.

4. Use the modulus operator to get the rightmost digit of the integer part.

5. Multiply the real part by 10 and type to an integer to get the left-most digit of the real part.

6. Finally, display the right-most digit of the integer part and the left-most digit of the real part.

See also  C program to read length width and compute Area Perimeter

Summary:

This tutorial discusses how to write a program to read a floating-point number from the standard input and print the rightmost digit of an integral part and the left-most digit of the real part. If you like the tutorial share it with your friends. Like the Facebook page for regular updates and YouTube channel for video tutorials.

Leave a Comment

Your email address will not be published. Required fields are marked *