Write a Cout Statement That Displays the Sum of the Variables X, Y, and Z.
BASICs of C/C++ Programming
Writing simple C++ programs
Example 1
// Simple printing code. #include <iostream> using namespace std; int main() { int a = 10, b = 20; cout << "sum is" << a + b << endl; cout << "product is " << a*b << endl; return 0; } Try This Example!
A typical c++ program uses several header files in order to use the library routines that has been developed already. In the above example, the statement #include <iostream> indicates that we need the I/O library. The statement "#using namespace std;" says that this example use output operator "<<" defined in the standard name space. In this example, a user has declared two integers a and b; and, they are initialized to 10 and 20 respectively. Finally it computes the sum and products of a and b and outputs them on the console as shown below.
Example 2
// Simple printing code. // #include <iostream> using namespace std; int main() { int a, b; cout << "Input a:"; cin >> a; cout << "Input b:"; cin >> b; cout << "sum is" << a + b << endl; cout << "product is " << a*b << endl; return 0; } Try This Example!
This example is very similar to the previous one except that the user inputs the values "a" and "b" and they are read using the input operator "cin". Finally the code calculates and prints the values of the sum and product of a and b
Data types
Computer stores the variables in the memory. Therefore, computer needs to know what kind of data we store so that computer reserves some memory space for the variable. A Byte [B] is the minimum amount of memory space for a computer. One Byte is eight bits and a bit is either 0 or 1. 1KB=2^8=1024 Bytes Data type is defined as a set of values together with a set of operations. C++ data types may be grouped into three categories:
Simple Data Types:
There are three categories within the simple data type
- Integral: integer (Whole numbers)
- Floating-point: Real numbers
- Enumeration type: user-defined data type
- Important Variants integral data types are
- int
- bool
- Char
It is also the number of bytes taken by a data type is compiler dependent. short or short int data type covers whole numbers that can be positive or negative (-128 to 127). unsigned short int takes only positive number (0 to 255)
Examples:
- -6728
- 0
- 78
- +763
- P78
- Note that + sign is optional
- No commas should be used within an integer (e.g. 10.825 is wrong)
In order to use a variable in C++, the variables should be declared first with the types.
Example.1
Following program has int a, b, and c. a and b values are given and find c. Prints a, b, and c
#include <iostream> using namespace std; int main() { int a=10, b=5, c; c = a*b; cout << "a = " << a <<", b= "<<b<<endl; cout << "c = " << c << endl; return 0; } Try This Example!
Example.1:
Following program has int a, b, and c. user can enter a and b values and calculate c. Prints a, b, and c
#include <iostream> using namespace std; int main() { int a, b, c; cout<<"Enter integer a and b values :"; cin>>a>>b; c = a*b; cout << "a = " << a <<", b= "<<b<<endl; cout << "c = " << c << endl; return 0; } Try This Example!
It is good habit to have prompt before cin command so that user knows that he has to respond.
bool Data Type
- Two values: true and false
- Manipulate logical (Boolean) expressions
char Data Type
- 'A', 'a', '0', '*', '+', '$', '&'
Example
#include <iostream> using namespace std; int main() { char x1='C'; char x2='P'; cout<<" This is a test..."<<endl; cout<<"Answer : "<<x1<<x2<<x2<<endl; return 0; } Try This Example!
Result
Example
It is good habit to have prompt before cin command so that user knows that he has to respond.
#include <iostream> using namespace std; int main() { char x1='C', x2='P'; cout<<" This is a test..."<<endl; cout<<" Answer : "<<x1<<x2<<x2<<endl; cout<<endl; return 0; } Try This Example!
Example
#include <iostream> using namespace std; int main() { char x1, x2; cout<<" Enter x1 and x2 chacaters:"; cin>>x1>>x2; cout<<" This is a test..."<<endl; cout<<" Answer : "<<x1<<x2<<x2<<endl; cout<<endl; return 0; } Try This Example!
Result
Real Numbers
- C++ uses scientific notation to represent real numbers (floating-point notation or scientific notation)
- This allows a user to represent too small and too large numbers
- 3.141592
- 0.3679
- 1.602e-19
- 3.4e+64 ( 50! Fifty factorial)
- 1.2e9 (1.2 GHz)
- Real numbers may be classified as float and double
[We always use double in our code as it is more accurate than float]
Example
#include <iostream> using namespace std; int main() { double x1, x2; cout<<" Enter x1 and x2 double numbers:"; cin>>x1>>x2; cout<<" This is a test..."<<endl; cout<<" Total : "<< x1 <<" + "<< x2 <<" = "<< x1+x2 << endl; cout<<endl; return 0; } Try This Example!
Result
Example
#include <iostream> using namespace std; int main() { double x1, x2, total; cout<<" Enter x1 and x2 double numbers:"; cin>>x1>>x2; total=x1+x2; cout<<endl; cout<<" Total : "<< x1 <<" + "<< x2 <<" = "<< total << endl; cout<<endl; return 0; } Result
Enumerated data types allows to designate a symbolic name for each integer.
This method Improves readability of your code
- Examples
// Here North=0, South =1, East =2, and West =3
Now one define a variable around this enumerated type direction myheading = East; radixchoice mybase=hex;
Variables
Variable is a location in memory where a value can be stored. Declare variables with data type and name before use
Example int x1; int x2; int sum;
You can declare several variables of same type in one declaration Comma separated list
int x1, x2, sum;
Example
#include <iostream> using namespace std; int main() { double x1, x2, total; cout<<" Enter x1 and x2 double numbers:"; cin>>x1>>x2; total=x1+x2; cout<<endl; cout<<" Total : "<< x1 <<" + "<< x2 <<" = "<< total << endl; cout<<endl; return 0; } Try This Example!
Global Variables:
Global variables are defined outside of all the functions, usually on top of the program. The global variables are available for use the entire program after the declaration and can be accessed by any function in the program. Following are some examples using global and local variables:
Example
#include <iostream> using namespace std; // Global variable declaration: int Y; int main () { // Local variable declaration: int a, b; // actual initialization a =10; b =20; Y = a + b; cout <<"The total is :"<< Y<<endl; return 0; } Try This Example!
Result
Note:Note: If a program can have same name for local and global variables, the value of local variable inside a function will take preference.
Example
#include <iostream> using namespace std; // Global variable declaration: int Y; int main () { // Local variable declaration and nitialization : int a=10, b=20,Y=30; Y = a + b + Y; cout <<"The total is :"<< Y << endl; return 0; } Try This Example!
Result
Operators:
Operators tells the compiler to perform specific mathematical or logical operations. C++ has following built-in operators:
Arithmetic Operators:The following arithmetic operators supported by C++ language:
The increment operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand
Example:
Example x = x+1; can be written as x++; //postfix form
Related Videos
Return to top Go back to top menu
Write a Cout Statement That Displays the Sum of the Variables X, Y, and Z.
Source: https://www.cpp.edu/~elab/ECE114/Basic-of-C++.html
0 Response to "Write a Cout Statement That Displays the Sum of the Variables X, Y, and Z."
ارسال یک نظر