Design and Implement an Application That Reads an Integer Value and Prints the Sum of All Even

BASICs of C/C++ Programming

Writing simple C++ programs
Instance ane
//  Simple printing lawmaking.  #include <iostream> using namespace std;  int main() { 	int a = x, b = 20; 	     cout << "sum is" << a + b << endl; 	cout << "product is " << a*b << endl;  	render 0; }          

Attempt This Example!

A typical c++ program uses several header files in gild 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 instance use output operator "<<" divers in the standard proper name space. In this example, a user has declared two integers a and b; and, they are initialized to ten and 20 respectively. Finally it computes the sum and products of a and b and outputs them on the console every bit shown below.

Example 2
//  Simple printing lawmaking. //  #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;  	render 0; }          

Try This Example!

This example is very like 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

Figurer stores the variables in the memory. Therefore, computer needs to know what kind of data nosotros store so that computer reserves some retention infinite for the variable. A Byte [B] is the minimum amount of retentivity space for a computer. 1 Byte is eight $.25 and a chip is either 0 or 1. 1KB=2^8=1024 Bytes Information type is defined as a set of values together with a prepare of operations. C++ information types may be grouped into three categories:

  • Unproblematic
  • Structured
  • Arrow
  • Simple Information Types:

    There are 3 categories within the simple data type

      1. Integral: integer (Whole numbers)
      2. Floating-indicate: Existent numbers
      3. Enumeration type: user-divers information blazon
      4. Important Variants integral information types are
          1. int
          2. bool
          3. Char

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

    Examples:
    • -6728
    • 0
    • 78
    • +763
    • P78
        1. Annotation that + sign is optional
        2. No commas should be used inside an integer (e.grand. 10.825 is incorrect)

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

    Instance.ane

    Post-obit programme 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 principal()   { 	int a=ten, b=5, c; 	    c = a*b; 	        cout << "a = " << a <<", b= "<<b<<endl;  	cout << "c = " << c << endl;  	return 0; }          

    Try This Instance!

    Example.ane:

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

    Attempt This Case!

    It is good addiction to have prompt before cin command then that user knows that he has to answer.

    bool Data Type
  • bool blazon
    • Two values: truthful and imitation
    • Manipulate logical (Boolean) expressions
  • true and false are called logical values
  • bool, truthful, and false are reserved words
  • char Data Blazon
  • The smallest integral information blazon
  • Used for characters: letters, digits, and special symbols
  • Each graphic symbol is enclosed in single quotes
    • 'A', 'a', '0', '*', '+', '$', '&'
  • A blank space is a grapheme and is written ' ', with a infinite left between the single quotes
  • Example
    #include <iostream>  using namespace std;   int main()   { 	char x1='C';  	char x2='P';  	cout<<" This is a examination..."<<endl;  	cout<<"Answer : "<<x1<<x2<<x2<<endl;  	render 0; }        

    Endeavor This Example!

    Result

    Example

    It is good habit to accept 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;   	render 0; }        

    Try This Case!

    Instance
    #include <iostream>  using namespace std;   int main()   { 	char x1, x2;  	cout<<" Enter x1 and x2 chacaters:";  	cin>>x1>>x2;  	cout<<" This is a examination..."<<endl;  	cout<<" Answer : "<<x1<<x2<<x2<<endl;  	cout<<endl;    return 0;  }        

    Endeavour This Example!

    Result

    Real Numbers
    1. C++ uses scientific annotation to represent real numbers (floating-point annotation or scientific note)
    2. This allows a user to represent too small and too large numbers
        1. 3.141592
        2. 0.3679
        3. 1.602e-19
        4. 3.4e+64 ( 50! Fifty factorial)
        5. 1.2e9 (1.ii GHz)
    3. Existent 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 primary()   { 	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!

    Consequence

    Example
    #include <iostream>  using namespace std;   int main()   { 	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 name for each integer.

    This method Improves readability of your code

      Examples
  • enum direction {Due north, South, East, West};
  • // Here North=0, S =1, Eastward =2, and Due west =iii

  • enum radixchoice {binary, octal, decimal, hex};
  • Now one define a variable around this enumerated type management myheading = East; radixchoice mybase=hex;

    Variables

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

    Example int x1; int x2; int sum;

    You lot can declare several variables of same blazon in one annunciation Comma separated list

    int x1, x2, sum;

  • Variable name must exist a valid identifier
  • Series of characters (messages, digits, underscores)
  • Cannot begin with digit
  • Case sensitive (uppercase letters are different from lowercase messages)
  • Employ identifiers of 31 characters or fewer
  • Avert identifiers that brainstorm with underscores and double underscores, because C++ compilers may utilise names like that for their ain purposes internally
  • In that location are some reserved keywords for C++, avoid using them as variable names.
  • It is meliorate to place a infinite later each comma (,) to brand programs more readable. If yous like yous tin declare each variable on a carve up line. And then that yous to place a comment next to each declaration
  • It is better to place declarations at the beginning of a function and separate them from the executable statements with one blank line to highlight where the declarations cease and the executable statements begin
  • The following list shows the some reserved words in C++. These reserved words may not be used as constant 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 <<" = "<< total << endl;  	cout<<endl;    return 0; }        

    Attempt This Instance!

    Global Variables:

    Global variables are divers outside of all the functions, usually on meridian of the program. The global variables are bachelor for utilise the entire program after the annunciation and can exist accessed by any role in the program. Following are some examples using global and local variables:

    Example
    #include <iostream>  using namespace std;  // Global variable declaration: int Y;   int principal ()   {  	// 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:Notation: If a program can take same name for local and global variables, the value of local variable inside a function volition accept preference.

    Example
    #include <iostream>  using namespace std;   // Global variable declaration:   int Y;   int primary ()   {  	// Local variable declaration and nitialization :   	int a=x, b=xx,Y=xxx;  	      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
  • Arithmetics Operators:The following arithmetic operators supported past C++ linguistic communication:

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

    Example:

    Example x = ten+ane; can be written equally ten++; //postfix grade

    Related Videos

    Return to summit Get back to acme carte du jour

    johnsoncous1959.blogspot.com

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

    0 Response to "Design and Implement an Application That Reads an Integer Value and Prints the Sum of All Even"

    ارسال یک نظر

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel