C++ Program using function allows the user to enter a number and than rounds it off,correct to the nearest integer
#include <iostream.h>
#include <conio.h> //HEADER FILES
void main() // MAIN FUNCTION
{
clrscr(); // CLEAR THE SCREEN
float input=0,remainder=0; // INITIALIZATION AND DECLARATION OF
int whole=0,answer=0; // VARIABLES
gotoxy (15,15); // GO TO A PARTICULAR POSITION
cout << "ENTER A FLOAT NUMBER : "; // MESSAGE
cin >> input; // STORE THE INPUT INTO THE VARIABLE
whole = input; // THIS LINE STORES THE INTEGER PART OF THE VALUE
// INTO A SEPERATE VARIABLE
remainder = input - whole; // REMAINDER IS THE VALUE AFTER THE DECIMAL
// POINT
if (remainder >= 0.5) // IF REMAINDER IS GREATER THAN 0.5
answer = whole + 1; // ADD 1 TO THE VALUE
else // OTHERWISE
answer = whole; // KEEP THE INTEGER PART AS IT IS
gotoxy (15,16);
cout << "Rounded Off Number Is : " << answer; // DISPLAY THE ANSWER
getch(); // WAIT FOR AN INPUT FROM THE USER.
} // END OF FUNCTION
Related Posts : C++ Programs
0 comments:
Post a Comment