avatarMoments Online

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1931

Abstract

cout<<<span class="hljs-string">"Enter second number"</span><<endl; <span class="hljs-comment">//prompts user for second input</span>

cin>>b; <span class="hljs-comment">//input second value</span>

c=a+b; <span class="hljs-comment">//stores sum in c</span>

cout<<<span class="hljs-string">"The sum of the numbers are \n"</span><<c<<endl; <span class="hljs-comment">//displays final result</span>

</pre></div><div id="7545"><pre><span class="hljs-comment">//CODE WITHOUT COMMENTS #1</span>

<span class="hljs-type">int</span> a,b,c;

cout<<<span class="hljs-string">"Enter first number"</span><<endl;

cin>>a;

cout<<<span class="hljs-string">"Enter second number"</span><<endl;

cin>>b;

c=a+b;

cout<<<span class="hljs-string">"The sum of the numbers are \n"</span><<c<<endl;</pre></div><div id="0742"><pre><span class="hljs-comment">//CODE WITHOUT COMMENTS #2</span>

<span class="hljs-meta">#<span class="hljs-keyword">include</span> <span class="hljs-string"><iostream></span></span>

<span class="hljs-keyword">using</span> <span class="hljs-keyword">namespace</span> std;

<span class="hljs-function"><span class="hljs-type">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span> </span>{

<span class="hljs-type">int</span> a,b,c;

cout&lt;&lt;<span class="hljs-string">"Enter first number"</span>&lt;&lt;endl;

cin&gt;&gt;a;

cout&lt;&lt;<span class="hljs-string">"Enter second number"</span>&lt;&lt;endl;

cin&gt;&gt;b;

c=a+b;

cout&lt;&lt;<span class="hljs-string">"The sum of the numbers are \n"</span>&lt;&lt;c&lt;&lt;endl;

<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;

}</pre></div><div id="8d94" class="link-block"> <a href="https://www.jdoodle.com/embed/v0/3inz"> <div> <div>

Options

<h2>JDoodle - free Online Compiler, Editor for Java, C/C++, etc</h2>
            <div><h3>JDoodle is a free Online Compiler, Editor, IDE for Java, C, C++, PHP, Perl, Python, Ruby and many more. you can run…</h3></div>
            <div><p>www.jdoodle.com</p></div>
          </div>
          <div>
            <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div>
          </div>
        </div>
      </a>
    </div><p id="190c">Above is the result of the code in the online compiler <a href="https://www.jdoodle.com/">JDoodle</a></p><h1 id="94ca">An explanation of the code:</h1><p id="5666"><b>int</b> is the data type<i> integer</i> that declares the variables a, b, and c as the storage location for integer values, <b>cout&lt;&lt;“Enter first number”&lt;&lt;endl; </b>prompts the user to enter the first number, <b>cin&gt;&gt;a; </b>accepts input from the user, <b>cout&lt;&lt;“Enter second number”&lt;&lt;endl; </b>and<b> cin &gt;&gt;b; </b>works the same as the previous, <b>c = a + b; </b>stores the sum of <b>a</b> and <b>b</b>,<b> </b>and<b> cout&lt;&lt;“The sum of the numbers are \n”&lt;c&lt;&lt;endl; </b>displays the sum of the numbers.</p><h1 id="48c3">Terminologies:</h1><blockquote id="58d5"><p><b>cout</b>&lt;&lt; as <i>output</i></p></blockquote><blockquote id="740e"><p><b>cin</b>&gt;&gt; as <i>input</i></p></blockquote><blockquote id="a302"><p><b>;</b> as <i>terminator</i></p></blockquote><blockquote id="9e74"><p><b>endl</b> as<i> inserts new line and flushes the stream</i></p></blockquote><blockquote id="ba99"><p><b>\n</b> as <i>break or inserts a new line</i></p></blockquote><h1 id="abda">How the program works:</h1><p id="12c5">The program receives input data from the user as numbers, finds the sum, and displays the result.</p><p id="16f5"><a href="https://medium.com/@momentsonline">©MomentsOnline</a></p></article></body>

Write Your First Simple C++ Program

The program receives integer values, sums, and displays the result

Image by the Author on Canva

This program prompts the user to enter two values, computes and displays the sum of both numbers.

To write this simple c++ program you first need:

Variable

A variable is a container that holds a value in memory. eg. a, num, stu .etc

Declare a variable: helps c++ know the variable you are declaring.

What you need to declare the variable:

  1. Data type
  2. User (prompt to enter a value)

A data type defines what type of data a variable holds. eg. int, char, float .etc

User prompt: cout<<“Enter first number”<

Adding two numbers — C++ code

//CODE WITH COMMENTS

int a,b,c; //declares variable as an integer

cout<<"Enter first number"<<endl; //prompts user for first input

cin>>a; //input first value

cout<<"Enter second number"<<endl; //prompts user for second input

cin>>b; //input second value

c=a+b; //stores sum in c

cout<<"The sum of the numbers are \n"<<c<<endl; //displays final result

//CODE WITHOUT COMMENTS #1

int a,b,c;

cout<<"Enter first number"<<endl;

cin>>a;

cout<<"Enter second number"<<endl;

cin>>b;

c=a+b;

cout<<"The sum of the numbers are \n"<<c<<endl;
//CODE WITHOUT COMMENTS #2

#include <iostream>

using namespace std;

int main() {

    int a,b,c;

    cout<<"Enter first number"<<endl;

    cin>>a;

    cout<<"Enter second number"<<endl;

    cin>>b;

    c=a+b;

    cout<<"The sum of the numbers are \n"<<c<<endl;

    return 0;
}

Above is the result of the code in the online compiler JDoodle

An explanation of the code:

int is the data type integer that declares the variables a, b, and c as the storage location for integer values, cout<<“Enter first number”<<endl; prompts the user to enter the first number, cin>>a; accepts input from the user, cout<<“Enter second number”<<endl; and cin >>b; works the same as the previous, c = a + b; stores the sum of a and b, and cout<<“The sum of the numbers are \n”<c<<endl; displays the sum of the numbers.

Terminologies:

cout<< as output

cin>> as input

; as terminator

endl as inserts new line and flushes the stream

\n as break or inserts a new line

How the program works:

The program receives input data from the user as numbers, finds the sum, and displays the result.

©MomentsOnline

Write
Simple
C Plus Plus Language
C
Programming
Recommended from ReadMedium