Data types and Input or Output in C++ Programming Language

Data Types in C++

In this instructional exercise, we will find out with regards to essential information Data types, for example, int,  float, char, and so on in C++ programming with the assistance of example.

  • Int - integer (whole number), without decimals, such as 123 or -123
  • Double - stores floating-point numbers, with decimals, such as 19.99 or -19.99
  • Float - stores a letter, such as 'a' or 'B'. The four values are surrounded by single references
  • String - stores text, such as "Hello World". String values are double prices on all sides
  • bool - stores values with two states: true or false
  • char - char is used for character. Such as 'A' , 'z'.

C++ Basic Input or Output

In C++, cout statement is used to print or display output to the user.
In C++, cin statement is used to take or get input from user

Explanation with the help of Examples

Example 1: String Output

#include <iostream>
using namespace std;

int main()
{
    cout<<"This is first example of C++ Programming"<<endl;
    return 0;
};

Output:
  This is first example of C++ Programming. 

If you don't add the using namespace std; statement, then you need to use std::cout instead of cout
we use ; instead of <<endl;
For example

#include <iostream>

int main()
{
    std::cout<<"This is first example of C++ Programming";
    return 0;
};



Example 2: Numbers and Characters Output

To print character variable or number, you need to use the same cout

#include <iostream>
using namespace std;

int main()
{
    int num1;
    int num2;
    char ch;
    ch='A';
    num1=10;
    num2=20;

    cout<<"Number one is "<<num1<<endl;
    cout<<"Number two is "<<num2<<endl;
    cout << "character: " << ch << endl;
    return 0;
};


Output:
Number one is 10
Number two is 20


C++ Input:

In C++, Cin is used to get input from user
Point: We use >> operator in cin to get Input

Example no 3: Integer input or output

In this program we use cin>>number; to get input
#include <iostream>
using namespace std;

int main() {
    int number;
    cout << "Enter an integer number : ";
    cin >> number;
    cout << "The input value of Number is : " << number;
    return 0;
};

Output:
Enter an integer number : 25
The input value of Number is : 25


Example 4: C++ Multiple inputs

#include <iostream>
using namespace std;

int main() {
    float a;
    int num;

    cout << "Enter a float number and an integer: ";
    cin >> a >> num;

    cout << "Float Number is : " << a << endl;
    cout << "Integer Number is : " << num <<endl;

    return 0;
}

Output:
Enter a float number and an integer: 10.9  25
Float Number is : 10.9
Integer Number is : 25

Post a Comment