Saturday, April 19, 2025

C++ Simple Calculator

Last tutorial about Telnet Server Header will be useful in this one. We will change welcome message to "Welcome to Simple Calculator v 0.1". Nothing special in header for calculator.

Then we are creating 2 variables first_number and second_number (integers, whole numbers) that will hold some values.

After that, we have another integer "result" which is result of addition of 2 variables we have.

Then, we are creating simple report using cout instructions. Run this code:

#include <iostream>

using namespace std;

int main() {

    cout << "#########################################" << endl;
    cout << "#" << endl;
    cout << "#" << endl;
    cout << "# Welcome to Simple Calculator v 0.1 #" << endl;
    cout << "#" << endl;
    cout << "#" << endl;
    cout << "#########################################" << endl;

    int first_number = 5;
    int second_number = 10;

    int result = first_number + second_number;

    cout << "-------------------------------------" << endl;
    cout << "Result of Additon: "                   << endl;
    cout << result                                  << endl;
    cout << "-------------------------------------" << endl;

    return 0;

}

 Result:

#########################################
#
#
# Welcome to Simple Calculator v 0.1 #
#
#
#########################################
-------------------------------------
Result of Additon:
15
-------------------------------------

Process returned 0 (0x0)   execution time : 0.022 s
Press any key to continue.

 You can also have simple version of some hard-coded calculation, but this is not useful that much. It's better to use variables.

#include <iostream>

using namespace std;

int main() {

    int result = 5 + 10;

    cout << result << endl;

    return 0;

}

Result: 

15

Process returned 0 (0x0)   execution time : 0.051 s
Press any key to continue.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...