Monday, April 22, 2024

cout in c++:


The cout object in C++ is an object of class iostream. It is characterized in iostream header record. It is utilized to show the result to the standard result gadget for example screen. It is related with the standard C result stream stdout. The information should have been shown on the screen is embedded in the standard result stream (cout) utilizing the addition operator(<<).



Program 1:

Below is the C++ program to implement cout object:

// C++ program to illustrate the use
// of cout object
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    // Print standard output
    // on the screen
    cout << "Welcome to GFG";
 
    return 0;
}
Output:
Welcome to GFG

 

Note: More than one variable can be printed using the insertion operator(<<) with cout.

Program 2:

Below is the C++ program to implement the above approach:

// C++ program to illustrate printing
// of more than one statement in a
// single cout statement
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    string name = "Akshay";
    int age = 18;
 
    // Print multiple variable on
    // screen using cout
    cout << "Name : " << name << endl
         << "Age : " << age << endl;
 
    return 0;
}
Output: 
Name : Akshay
Age : 18
The cout proclamation can likewise be utilized with some part works:

1.cout.write(char *str, int n): Print the principal N character perusing from str.
2.cout.put(char &ch): Print the person put away in character ch.
3.cout.precision(int n): Sets the decimal accuracy to N, while utilizing float values.

Program 3:

Below is the implementation of the member functions of the cout.write() and cout.put():

// C++ program to illustrate the use
// of cout.write() and cout.put()
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    char gfg[] = "Welcome at GFG";
    char ch = 'e';
 
    // Print first 6 characters
    cout.write(gfg, 6);
 
    // Print the character ch
    cout.put(ch);
    return 0;
}
Output: 
Welcome

 

Program 4:

Below is the C++ program to illustrate the use of cout.precision():

// C++ program toillustrate the use
// of cout.precision()
#include <iostream>
using namespace std;
 
// Driver Code
int main()
{
    double pi = 3.14159783;
 
    // Set precision to 5
    cout.precision(5);
 
    // Print pi
    cout << pi << endl;
 
    // Set precision to 7
    cout.precision(7);
 
    // Print pi
    cout << pi << endl;
 
    return 0;
}
Output: 
3.1416
3.141598
You must visit these resources.

No comments:

Post a Comment

last post

C language Vs C++ language in 2024

Popular Posts