X=2 and Y = 3 What Would Cin >> X >> Y; Read?

Basics of C/C++ Programming

Writing simple C++ programs
Example 1
//  Simple printing lawmaking.  #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; }          

Endeavor 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 argument #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 proper name space. In this example, a user has declared two integers a and b; and, they are initialized to x and 20 respectively. Finally it computes the sum and products of a and b and outputs them on the console as shown below.

Example two
//  Simple printing lawmaking. //  #include <iostream> using namespace std;  int master() { 	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; }          

Endeavor This Example!

This example is very like to the previous i except that the user inputs the values "a" and "b" and they are read using the input operator "cin". Finally the lawmaking 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 information nosotros store and then that computer reserves some memory space for the variable. A Byte [B] is the minimum corporeality of memory infinite for a computer. One Byte is eight bits and a bit is either 0 or 1. 1KB=2^8=1024 Bytes Information type is defined as a fix of values together with a set up of operations. C++ information types may be grouped into three categories:

  • Uncomplicated
  • Structured
  • Arrow
  • Simple Information Types:

    There are iii categories within the uncomplicated information type

      1. Integral: integer (Whole numbers)
      2. Floating-point: Real numbers
      3. Enumeration blazon: user-defined data type
      4. Of import Variants integral data types are
          1. int
          2. bool
          3. 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 tin be positive or negative (-128 to 127). unsigned brusque int takes just positive number (0 to 255)

    Examples:
    • -6728
    • 0
    • 78
    • +763
    • P78
        1. Annotation that + sign is optional
        2. No commas should be used within an integer (east.yard. ten.825 is wrong)

      In order to utilize a variable in C++, the variables should be declared first with the types.

    Instance.i

    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=ten, b=5, c; 	    c = a*b; 	        cout << "a = " << a <<", b= "<<b<<endl;  	cout << "c = " << c << endl;  	return 0; }          

    Try This Instance!

    Instance.1:

    Following program has int a, b, and c. user tin 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; }          

    Attempt This Example!

    It is proficient habit to have prompt earlier cin command and so that user knows that he has to reply.

    bool Data Type
  • bool type
    • Two values: true and false
    • Manipulate logical (Boolean) expressions
  • true and false are called logical values
  • bool, true, and false are reserved words
  • char Data Type
  • The smallest integral data type
  • Used for characters: messages, digits, and special symbols
  • Each character is enclosed in single quotes
    • 'A', 'a', '0', '*', '+', '$', '&'
  • A bare space is a graphic symbol and is written ' ', with a space left betwixt the single quotes
  • Case
    #include <iostream>  using namespace std;   int main()   { 	char x1='C';  	char x2='P';  	cout<<" This is a exam..."<<endl;  	cout<<"Answer : "<<x1<<x2<<x2<<endl;  	render 0; }        

    Try This Case!

    Result

    Instance

    It is proficient habit to have prompt before cin control so that user knows that he has to respond.

    #include <iostream>  using namespace std;   int chief()   { 	char x1='C', x2='P';  	cout<<" This is a examination..."<<endl;  	cout<<" Respond : "<<x1<<x2<<x2<<endl;  	cout<<endl;   	return 0; }        

    Endeavour 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!

    Event

    Existent Numbers
    1. C++ uses scientific annotation to represent real numbers (floating-bespeak notation or scientific note)
    2. This allows a user to represent likewise small and besides large numbers
        1. iii.141592
        2. 0.3679
        3. 1.602e-19
        4. 3.4e+64 ( 50! Fifty factorial)
        5. 1.2e9 (i.2 GHz)
    3. Real numbers may exist classified as bladder and double

    [We always utilize double in our code as it is more than accurate than float]

    Example
    #include <iostream>  using namespace std;   int chief()   { 	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;    render 0; }        

    Effort This Instance!

    Outcome

    Case
    #include <iostream>  using namespace std;   int main()   { 	double x1, x2, full;  	cout<<" Enter x1 and x2 double numbers:";  	cin>>x1>>x2; 	full=x1+x2;  	cout<<endl;  	cout<<" Total : "<< x1 <<" + "<< x2 <<" = "<< full << endl;  	cout<<endl;    return 0; }        
    Consequence

    Enumerated data types allows to designate a symbolic name for each integer.

    This method Improves readability of your lawmaking

      Examples
  • enum direction {North, Due south, East, West};
  • // Here North=0, South =1, East =2, and W =3

  • enum radixchoice {binary, octal, decimal, hex};
  • Now 1 define a variable effectually this enumerated blazon 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 employ

    Example int x1; int x2; int sum;

    You tin declare several variables of same type in 1 declaration Comma separated list

    int x1, x2, sum;

  • Variable proper noun must be a valid identifier
  • Serial of characters (letters, digits, underscores)
  • Cannot begin with digit
  • Example sensitive (uppercase messages are different from lowercase letters)
  • Utilize identifiers of 31 characters or fewer
  • Avoid identifiers that begin with underscores and double underscores, because C++ compilers may use names like that for their ain purposes internally
  • At that place are some reserved keywords for C++, avoid using them as variable names.
  • Information technology is ameliorate to identify a infinite later each comma (,) to make programs more readable. If you like you tin can declare each variable on a dissever line. And then that you to place a comment next to each declaration
  • Information technology is better to place declarations at the first of a office and split up them from the executable statements with 1 blank line to highlight where the declarations end and the executable statements begin
  • The post-obit list shows the some reserved words in C++. These reserved words may not be used as constant or variable or any other identifier names.
  • Instance
              #include <iostream>  using namespace std;   int master()   { 	double x1, x2, total;  	cout<<" Enter x1 and x2 double numbers:";  	cin>>x1>>x2; 	total=x1+x2;  	cout<<endl;  	cout<<" Full : "<< x1 <<" + "<< x2 <<" = "<< full << endl;  	cout<<endl;    render 0; }        

    Endeavor This Instance!

    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 afterwards the declaration and can exist accessed by whatever part 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 annunciation:   	int a, b; // actual initialization   	a =10;   	b =xx;   	Y = a + b;   	cout <<"The full is :"<< Y<<endl;     return 0; }        

    Try This Instance!

    Result

    Note:Note: If a programme can have aforementioned name for local and global variables, the value of local variable inside a function volition take preference.

    Example
    #include <iostream>  using namespace std;   // Global variable declaration:   int Y;   int main ()   {  	// Local variable announcement and nitialization :   	int a=x, 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
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Arithmetic Operators:The following arithmetic operators supported by C++ linguistic communication:

    The increase operator ++ adds 1 to its operand, and the decrement operator -- subtracts 1 from its operand

    Example:

    Case x = x+one; can be written as x++; //postfix class

    Related Videos

    Return to top Go back to superlative bill of fare

    rogertheept.blogspot.com

    Source: https://www.cpp.edu/~elab/ECE114/Basic-of-C++.html

    0 Response to "X=2 and Y = 3 What Would Cin >> X >> Y; Read?"

    Postar um comentário

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel