In this tutorial you will learn about the C Program to Round off Floating point Number and its application with practical example.
C Program to Round off Floating point Number
In this tutorial, we will learn to create a C program that will Round off Floating-point Numbers in C programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C programming topics:
- Operators in C Programming.
- Basic Input and Output function in C Programming.
- Basic C programming.
- Conditional Statements in c programming.
Round off Floating point Number.
As we all know the c programming is a very powerful language. In the c programming language, we have many pre-defined functions for arithmetic operations. But today we will Round off the Floating-point Numbers using the c programming language.
With C programming we can make many arithmetics series with just a little code.
With the help of this program, we can Round off Floating-point Numbers.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declare the variables for the program. 2. Take the input number from the user. 3. Pass that number to a if condition. 4. Calculate the round of number. 5. Print the round of that number. 6. End the program. |
Program to Round off Floating point Number:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
#include<stdio.h> int main() { //declaring the variables for the program float num; int flag, roundNum, tempNum; //Taking the input number from the user printf("Enter the number: "); scanf("%f", &num); //checking the number greater than zero if(num>0) { tempNum = num*10; flag = tempNum%10; if(flag>=5) { roundNum = num; roundNum++; } else { roundNum = num; } //printing rounded positive number printf("\nWhole number after rounding off the given real number = %d", roundNum); } //checking the number smaller than zero else if(num<0) { num = -(num); tempNum = num*10; flag = tempNum%10; if(flag>=5) { roundNum = num; roundNum--; } else { roundNum = num; } //printing rounded negative number printf("\nWhole number after rounding off the given real number = -%d", roundNum); } else //printing given number in output { printf("\nThe given number is 0"); } return 0; } |
Output:-
Positive round-of:-
Negative round-of:-
In the above program, we have first initialized the required variable.
- num = it will hold the floating number value.
- flag = it will hold the integer value.
- roundNum = it will hold the integer value.
- tempNum = it will hold the integer value.
Checking the number greater than zero.
Checking the number smaller than zero.
Printing the number equal to zero.