Assume that the following variables have been defined in a program: int x = 10; int y = 20; int z = 30; Write a cout statement that displays the sum of the variables x, y, and z.

Answer :

Answer:

The program to this question as follows:

Program:

#include <iostream> //defining header file

using namespace std;

int main() //defining main method

{

int x = 10,y = 20,z = 30; //defining integer variable and assigning the value

int sum;//defining integer variable sum  

sum =x+y+z;//adding value,that holds by sum variable  

cout<<"sum: "<<sum; //print value

return 0;

}

Output:

sum: 60

Explanation:

Description of the above code as follows:

  • In the above code three integer variable "x, y, and z" is declared, that initialized with the value, that is 10, 20, and 30.
  • In the next line, another variable "sum" is declared, that holds the above variable value and add the number.
  • Then the print function "cout"  is used, which prints sum variable value.

Other Questions