Print your name and age with variables

Description: Create a std::string for your name and an int for your age. Print My name is Riya and I am 21 years old. with your own values.

Hint:

  • Join the parts with << on std::cout.

Solution

#include <iostream>
#include <print>
#include <string>

int main() {
    std::string name = "Riya";
    int age = 21;
    std::cout << "My name is " << name << " and I am " << age << " years old.\n";
    std::println("My name is {} and I am {} years old.", name, age);
}
Output

My name is Riya and I am 21 years old.
My name is Riya and I am 21 years old.

How it works

  • << sends each part to the output stream, one after the other.
  • std::println (C++23) uses {} placeholders.

Read C++ Variables.


Learn more about the similar topics:
Tutorials
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.