Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




C++ - NUMBERS


As we have seen in the Data Types topic, there are two types of Numbers supported by C++. Integers and floating point numbers.


Integers are the whole numbers (i.e. 7 or 5 or 2.14).


Any whole number(Be it -4 or 45) can be represented by the Int datatype


Integers are the whole numbers (i.e. 7 or 5 or 2.14).


Below are the data types used for Integers.

  1. int Data Type


    The Data Type for a whole number is int.


    The Int data type is used to store whole numbers from -32,768 to 32,767. And takes 2 bytes of memory.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
    	
        	int x = -30535;
            cout << x;
            
            return 0;
    	 }	
    


    Output :



      -30535

  1. unsigned int Data Type


    The Data Type for unsigned number is unsigned in. i.e. unsigned int Data Type can hold positive numbers.


    The Int data type is used to store whole numbers from 0 to 32,767.


    And takes 2 bytes of memory.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
    	
            unsigned int x = 30535;
            cout << x;
            
            return 0;
        }
    


    Output :



      30535

  1. signed int Data Type


    The Data Type for signed number is signed int. i.e. unsigned int Data Type can hold positive and negative numbers.


    The signed int data type is used to store whole numbers from -32,768 to 32,767.


    And takes 2 bytes of memory.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
    	
            signed int x = -30535;
            cout << x;
            
            return 0;
        }
    


    Output :



      -30535

  1. long Data Type


    Say, we have a large value 3372036854775808.


    Well! In this case int data type will not be able to hold that value.


    And in this scenario, we need a long data type.


    And takes 4 bytes of memory.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
    	
            long x = 3372036854775808;
            cout << x;
            
            return 0;
        }
    


    Output :



      3372036854775808

    So, in the above example, we have created a variable x of type long. And assigned a large value 3372036854775808 to it.


  1. unsigned long int Data Type


    The Data Type for unsigned long int number is unsigned long int. i.e. unsigned long int Data Type can hold positive numbers.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
    	
            unsigned long int x = 3372036854775808;
            cout << x;
            
            return 0;
        }
    


    Output :



      3372036854775808

  1. short Data Type


    The short Data Type is used to hold whole numbers between -32,768 to 32,767.


    Say, there is a number i.e. 2768. So, to save space we can use Short Data Type.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
    	
            short x = -2768;
        	cout << x;
        	
        	return 0;
    	 }
    


    Output :



      -2768

    Note : We can still use int with numbers 5 and 2768.

  1. unsigned short Data Type


    The unsigned short Data Type is used to hold whole numbers between 0 to 32,767.


    Say, there is a number i.e. 2768. So, to save space we can use unsigned short Data Type.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
    	
            unsigned short x = 2768;
        	cout << x;
        	
        	return 0;
    	 }
    


    Output :



      2768

  1. Float Data Type


    A float data type is used to hold floating point numbers. And is used to hold the floating point numbers of 4 bytes.


    For example 5.987.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
            
            float x = 5.987;
        	cout << x;
        	
        	return 0;
    	 }
    


    Output :



      5.987

    So, in the above example, we have created a variable x of type float. And assigned the value 5.987 to it.


    float x = 5.987;

  1. double Data Type


    A double data type is used to hold floating point numbers. And is used to hold the floating point numbers of 64 bit.


    It is almost same as Float data type. Just that it can hold a floating point number of a larger size.


    For example 5.98743.


    Example :



    	#include <iostream>
    	using namespace std;
    
    	int main() {
            
            double x = 5.98743;
        	cout << x;
        	
        	return 0;
    	 }	
    


    Output :



      5.98743