Javascript required
Skip to content Skip to sidebar Skip to footer

Prompt the User to Enter an Integer and Then Read the Integer Asm

BASICs of C/C++ Programming

Writing simple C++ programs
Example ane
//  Simple printing code.  #include <iostream> using namespace std;  int chief() { 	int a = 10, b = twenty; 	     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 adult already. In the to a higher place example, the argument #include <iostream> indicates that we demand the I/O library. The argument "#using namespace std;" says that this example use output operator "<<" divers in the standard proper noun space. In this example, a user has declared ii 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.

Instance ii
//  Uncomplicated printing code. //  #include <iostream> using namespace std;  int primary() { 	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 Case!

This case 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 8 bits and a bit is either 0 or i. 1KB=2^8=1024 Bytes Data blazon is defined as a ready of values together with a ready of operations. C++ data types may be grouped into three categories:

  • Simple
  • Structured
  • Pointer
  • Uncomplicated Data Types:

    There are three categories within the simple data type

      1. Integral: integer (Whole numbers)
      2. Floating-indicate: Real numbers
      3. Enumeration type: user-defined data type
      4. Important Variants integral data types are
          1. int
          2. bool
          3. Char

    It is as well the number of bytes taken by a data type is compiler dependent. short or short int information type covers whole numbers that can be positive or negative (-128 to 127). unsigned short int takes but positive number (0 to 255)

    Examples:
    • -6728
    • 0
    • 78
    • +763
    • P78
        1. Note that + sign is optional
        2. No commas should be used within an integer (e.chiliad. 10.825 is wrong)

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

    Instance.1

    Post-obit plan 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 summate c. Prints a, b, and c

                #include <iostream>  using namespace std;   int principal()   { 	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 Instance!

    It is adept habit to have prompt earlier cin control so that user knows that he has to reply.

    bool Data Type
  • bool type
    • Two values: true and faux
    • Dispense logical (Boolean) expressions
  • true and false are chosen logical values
  • bool, truthful, and false are reserved words
  • char Data Type
  • The smallest integral data type
  • Used for characters: letters, digits, and special symbols
  • Each character is enclosed in single quotes
    • 'A', 'a', '0', '*', '+', '$', '&'
  • A blank space is a character and is written ' ', with a space left between the single quotes
  • Case
    #include <iostream>  using namespace std;   int primary()   { 	char x1='C';  	char x2='P';  	cout<<" This is a test..."<<endl;  	cout<<"Respond : "<<x1<<x2<<x2<<endl;  	return 0; }        

    Try This Example!

    Result

    Example

    It is good habit to have prompt before cin control and then that user knows that he has to respond.

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

    Try This Example!

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

    Existent Numbers
    1. C++ uses scientific notation to represent real numbers (floating-point note or scientific annotation)
    2. This allows a user to represent too modest and too big numbers
        1. 3.141592
        2. 0.3679
        3. ane.602e-19
        4. 3.4e+64 ( l! L factorial)
        5. 1.2e9 (1.2 GHz)
    3. Existent numbers may be classified as float and double

    [Nosotros always use double in our code every bit it is more accurate than bladder]

    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;    render 0; }        

    Try This Example!

    Issue

    Instance
    #include <iostream>  using namespace std;   int master()   { 	double x1, x2, full;  	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 proper noun for each integer.

    This method Improves readability of your code

      Examples
  • enum management {North, South, East, Due west};
  • // Here North=0, S =1, East =ii, and Due west =3

  • enum radixchoice {binary, octal, decimal, hex};
  • At present ane define a variable effectually this enumerated type direction myheading = East; radixchoice mybase=hex;

    Variables

    Variable is a location in memory where a value can exist stored. Declare variables with data type and name before employ

    Instance int x1; int x2; int sum;

    You tin can declare several variables of same type in i declaration Comma separated listing

    int x1, x2, sum;

  • Variable name must exist a valid identifier
  • Series of characters (messages, digits, underscores)
  • Cannot begin with digit
  • Case sensitive (majuscule letters are dissimilar from lowercase letters)
  • Use 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 own purposes internally
  • There are some reserved keywords for C++, avoid using them every bit variable names.
  • Information technology is better to place a space later on each comma (,) to brand programs more readable. If you like you tin can declare each variable on a separate line. And so that y'all to identify a comment next to each declaration
  • Information technology is improve to place declarations at the offset of a role and separate them from the executable statements with 1 blank line to highlight where the declarations end and the executable statements begin
  • The following listing shows the some reserved words in C++. These reserved words may non exist used equally abiding or variable or any other identifier names.
  • 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 <<" = "<< full << endl;  	cout<<endl;    render 0; }        

    Try This Instance!

    Global Variables:

    Global variables are defined outside of all the functions, usually on meridian of the program. The global variables are available for use the entire program after the declaration and can exist accessed past whatever role in the program. Following are some examples using global and local variables:

    Example
    #include <iostream>  using namespace std;  // Global variable annunciation: int Y;   int main ()   {  	// Local variable declaration:   	int a, b; // actual initialization   	a =10;   	b =xx;   	Y = a + b;   	cout <<"The total is :"<< Y<<endl;     return 0; }        

    Attempt This Instance!

    Result

    Note:Annotation: If a plan can have same name for local and global variables, the value of local variable inside a role volition take preference.

    Instance
    #include <iostream>  using namespace std;   // Global variable declaration:   int Y;   int chief ()   {  	// Local variable declaration and nitialization :   	int a=x, b=xx,Y=30;  	      Y = a + b + Y;   	cout <<"The total is :"<< Y << endl;    render 0; }        

    Try This Example!

    Effect

    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 past C++ language:

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

    Example:

    Example x = x+ane; can exist written as x++; //postfix form

    Related Videos

    Render to superlative Go dorsum to top bill of fare

    Prompt the User to Enter an Integer and Then Read the Integer Asm

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