How to sum two integers in C++ - Wisdom Lighter

Saturday, 22 July 2017

How to sum two integers in C++

C++

Program:

#include <iostream>

using namespace std;

int main()
{
    int first , second , sum ;

    cout << "Enter the first integer" << endl;
    cin >> first ;

    cout << "Enter the second integer" << endl;
    cin >> second;

    sum = first + second;

    cout << "sum = " << sum ;

    return 0;
}


Output:


9 comments:
Write comments
  1. return 0 at the end of C++ code why please discuss

    ReplyDelete
  2. devC++ vs code block which is best

    ReplyDelete
    Replies
    1. I think devC++ codeblock are basic tool for writing c++ code what you think please list top c++ tools

      Delete
  3. C++ if Statement

    if (testExpression)
    {
    // statements
    }

    The if statement evaluates the test expression inside parenthesis.

    If test expression is evaluated to true, statements inside the body of if is executed.

    If test expression is evaluated to false, statements inside the body of if is skipped.
    How if statement works?

    Working of if statement in C++ Programming
    Flowchart of if Statement

    Flowchat of if statement in C++ Programming

    Above figure describes the working of an if statement.
    Example 1: C++ if Statement

    // Program to print positive number entered by the user
    // If user enters negative number, it is skipped

    #include
    using namespace std;

    int main()
    {
    int number;
    cout << "Enter an integer: ";
    cin >> number;

    // checks if the number is positive
    if ( number > 0)
    {
    cout << "You entered a positive integer: " << number << endl;
    }

    cout << "This statement is always executed.";
    return 0;

    }

    Output 1

    Enter an integer: 5
    You entered a positive number: 5
    This statement is always executed.

    Output 2

    Enter a number: -5
    This statement is always executed.

    C++ if...else

    The if else executes the codes inside the body of if statement if the test expression is true and skips the codes inside the body of else.

    If the test expression is false, it executes the codes inside the body of else statement and skips the codes inside the body of if.
    How if...else statement works?

    Working of if else statement in C++ Programming
    Flowchart of if...else

    Flowchart of if...else statement in C++ Programming
    Example 2: C++ if...else Statement

    // Program to check whether an integer is positive or negative
    // This program considers 0 as positive number

    #include
    using namespace std;

    int main()
    {
    int number;
    cout << "Enter an integer: ";
    cin >> number;

    if ( number >= 0)
    {
    cout << "You entered a positive integer: " << number << endl;
    }

    else
    {
    cout << "You entered a negative integer: " << number << endl;
    }

    cout << "This line is always printed.";
    return 0;
    }

    Output

    Enter an integer: -4
    You entered a negative integer: -4.
    This line is always printed.

    ReplyDelete
  4. C++ while and do...while Loop
    Loops are used in programming to repeat a specific block of code. In this article, you will learn to create while and do...while loops in C++ programming.
    C++ while Loop

    In computer programming, loop repeats a certain block of code until some end condition is met.

    There are 3 type of loops in C++ Programming:

    for Loop
    while Loop
    do...while Loop

    C++ while Loop

    The syntax of a while loop is:

    while (testExpression)
    {
    // codes
    }

    where, testExpression is checked on each entry of the while loop.
    How while loop works?

    The while loop evaluates the test expression.
    If the test expression is true, codes inside the body of while loop is evaluated.
    Then, the test expression is evaluated again. This process goes on until the test expression is false.
    When the test expression is false, while loop is terminated.

    Flowchart of while Loop

    Flowchart of while loop in C++ Programming
    Example 1: C++ while Loop

    // C++ Program to compute factorial of a number
    // Factorial of n = 1*2*3...*n

    #include
    using namespace std;

    int main()
    {
    int number, i = 1, factorial = 1;

    cout << "Enter a positive integer: ";
    cin >> number;

    while ( i <= number) {
    factorial *= i; //factorial = factorial * i;
    ++i;
    }

    cout<<"Factorial of "<< number <<" = "<< factorial;
    return 0;
    }

    Output

    Enter a positive integer: 4
    Factorial of 4 = 24

    In this program, user is asked to enter a positive integer which is stored in variable number. Let's suppose, user entered 4.

    Then, the while loop starts executing the code. Here's how while loop works:

    Initially, i = 1, test expression i <= number is true and factorial becomes 1.
    Variable i is updated to 2, test expression is true, factorial becomes 2.
    Variable i is updated to 3, test expression is true, factorial becomes 6.
    Variable i is updated to 4, test expression is true, factorial becomes 24.
    Variable i is updated to 5, test expression is false and while loop is terminated.

    C++ do...while Loop

    The do...while loop is a variant of the while loop with one important difference. The body of do...while loop is executed once before the test expression is checked.

    The syntax of do..while loop is:

    do {
    // codes;
    }
    while (testExpression);

    How do...while loop works?

    The codes inside the body of loop is executed at least once. Then, only the test expression is checked.
    If the test expression is true, the body of loop is executed. This process continues until the test expression becomes false.
    When the test expression is false, do...while loop is terminated.

    Flowchart of do...while Loop

    Flowchart of do while loop in C++ programming
    Example 2: C++ do...while Loop

    // C++ program to add numbers until user enters 0

    #include
    using namespace std;

    int main()
    {
    float number, sum = 0.0;

    do {
    cout<<"Enter a number: ";
    cin>>number;
    sum += number;
    }
    while(number != 0.0);

    cout<<"Total sum = "<<sum;

    return 0;
    }

    Output

    Enter a number: 2
    Enter a number: 3
    Enter a number: 4
    Enter a number: -4
    Enter a number: 2
    Enter a number: 4.4
    Enter a number: 2
    Enter a number: 0

    ReplyDelete
  5. C++ Program to Display Fibonacci Series
    In this article, you will learn to print fibonacci series in C++ programming (up to nth term, and up to a certain number).

    Example 1: Fibonacci Series up to n number of terms

    #include
    using namespace std;

    int main()
    {
    int n, t1 = 0, t2 = 1, nextTerm = 0;

    cout << "Enter the number of terms: ";
    cin >> n;

    cout << "Fibonacci Series: ";

    for (int i = 1; i <= n; ++i)
    {
    // Prints the first two terms.
    if(i == 1)
    {
    cout << " " << t1;
    continue;
    }
    if(i == 2)
    {
    cout << t2 << " ";
    continue;
    }
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;

    cout << nextTerm << " ";
    }
    return 0;
    }

    Output

    Enter the number of terms: 10
    Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

    Example 2: Program to Generate Fibonacci Sequence Up to a Certain Number

    #include
    using namespace std;

    int main()
    {
    int t1 = 0, t2 = 1, nextTerm = 0, n;

    cout << "Enter a positive number: ";
    cin >> n;

    // displays the first two terms which is always 0 and 1
    cout << "Fibonacci Series: " << t1 << ", " << t2 << ", ";

    nextTerm = t1 + t2;

    while(nextTerm <= n)
    {
    cout << nextTerm << ", ";
    t1 = t2;
    t2 = nextTerm;
    nextTerm = t1 + t2;
    }
    return 0;
    }

    Output

    Enter a positive integer: 100
    Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

    ReplyDelete

  6. CS106L handout


    Assignment 3: KDTree

    following is pdf link for assignmetn

    Due: Tuesday, March 13th, 11:59pm
    http://web.stanford.edu/class/cs106l/handouts/005_assignment_3_kdtree.pdf
    This time around, you'll be writing a data structure to efficiently find points that are nearby a target point. Writing it will take an understanding of templates, constructors (including copy constructors), and operator overloading. Best of luck! If things go wrong, don't hesitate to email me.

    There's no web demo with this one, but the starter files include test cases. If you have any trouble getting started, want any clarification, or spot any typos/errors, please email me!

    ReplyDelete
  7. CS 152: Programming Languages

    22-Mar: Assignment 4 released!

    https://www.seas.harvard.edu/courses/cs152/2016sp/assignments.html

    ReplyDelete
  8. CS 152: Programming Languages
    Resources
    Text books

    A number of excellent books and on-line resources overlap with the course's content and can provide alternate explanations despite differences in notation and approach. Let the instructor know if you have trouble finding the intersection between these resources and the course content.

    "Types and Programming Languages" by Benjamin C. Pierce, MIT Press, 2002.
    Available on reserve at the library.
    "Practical Foundations for Programming Languages" by Robert Harper, Cambridge University Press, 2013.
    Draft available on Harper's website.
    "Concepts in Programming Languages" by John C. Mitchell, Cambridge University Press, 2003.
    Available online through Harvard University Libraries eContent Collection. To view on a Mac, follow these instructions.
    "The Formal Semantics of Programming Languages" by Glynn Winskel, MIT Press, 1993.
    Available on reserve at the library.
    "Programming Languages: Application and Interpretation" by Shriram Krishnamurthi.
    There are two editions, both available on the author's website: http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/.

    OCaml resources

    Installation: https://ocaml.org/docs/install.html
    Tools: https://github.com/realworldocaml/book/wiki/Installation-Instructions

    ReplyDelete