What is the difference between int and float data types dealing with numbers in C++ language. Also write a C++ program to demonstrate it.
Int and Float are both data types on C++ language. Int (integer constant) is any integer(positive or negative) number without any decimal point. Sample Int values are 5, -5, +35, 2000, etc. Float (floating constant) is any signed or unsigned number that have decimal point. Sample float values are +15.34, -7.8, +4.8950, etc.
Sample code for int value is:
Sample code for float
Sample code that demonstrate it:
// sample variablesvariables
// declaring variables:
int c, d;
c = 5;
d = 2;
c = c + 1;
result = c - d;
// print out the result:
cout << result;
// terminate the program:
The int and float are some of the basic fundamental data types in C++.
int stands for Integer constant, meaning, it is any number(positive or negative) that is represented without any decimal point.
Example: 6, -11, +26, 100
There are two types of int:
short int (short) Short Integer. 2bytes in size, with number range of, signed: -32768 to 32767 and unsigned: 0 to 65565
long int (long) Long Integer. 4bytes in size, with number range of, signed: -2147483648 to 2147483647 and unsigned: 0 to 4294967295
float stands for Floating constant, meaning, it is any signed or unsigned numbers that is represented having decimal point.
Example: +11.735 , -7.5 , +6.628
float Floating point number. 4bytes in size, with number range of 3.4e +/- 38 (7 digits)
Please note that the given values (size and range) are found on most 32-bit systems.
The solution for your question is simple.
Int Data Type
Integer int data type is used to store integer values like 34, 68790 etc. To declare a variable of type integer type keyword int followed by the name of the variable. you can give any name to a variable.
int sum; or int x;
or we can give value to it like
This is called declaring and defining the variable of data type int.
The int data type can store both values in negative or positive. It will always b a whole number. Its Values ranges from -2,147,483,648 to 2,147,483,647. It uses 4 bytes of memory.
Float Data Type
It is used to store decimal values or the values in fractions. Also called floating point data type. To declare the variable of floating type keyword float and the variable name.
This is called declaration and definition. Float has further three types.
Float type range is very high. it use 4 bytes of memory. 7 digits accuracy.
cout << "Integer Value is " << x << endl;
cout << "Floating Value is " << y << endl;
This program simply displays output values of int data type and float data type. Hope you fully understands the difference.
Point to remember:
if you use int data type and gives it a decimal value like 33.33, it will only read the value before decimal and cant read the fractional value. so we use it were we want to deal with whole numbers. And we use float type where we want to use decimal numbers.