Simple C++ Mathematics Program

All tutorials created in C++ to be posted in here.
3 posts Page 1 of 1
Contributors
User avatar
Duggypker
New Member
New Member
Posts: 10
Joined: Tue Mar 23, 2010 3:10 am

Simple C++ Mathematics Program
Duggypker
Hello Duggypker here. Today I am going to show you how to make an extremely simple mathematics program on C++.

I am going to show you addition in the program I use. But you can simply replace the + with a * for multiplication. - for subtraction, or / for division.

Let's get started shall we?

I am using the Code blocks C++ IDE which you can download here: http://www.codeblocks.org/downloads just click Download the binary release and your good to go.
Code: Select all
#include <iostream>

using namespace std;

int main() {
int a, b, c;

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

 cout << "Enter a second number: \n";
  cin >> b;

 c = a + b;
 cout << "Our solution is: " << c;

return 0;
 }
Now to explain...
First let me start with the #include <iostream>, using namespace std; what are these?

The #include <iostream> is what tell the compiler we are using that we want to use "iostream" in our program. Which is the basic standard C++ libary for all compilers.
using namespace std; is saying what namespace we will be using. In this case: std.

What is a namespace? Well a namespace is basically a list of variable and function names, in order to not get standard library names mixed with others, the standard library creates the namespace std, so any time you want to use a command from the standard library you must include this line.

int main() { is a function declaration. Which in turn is a block of code which begins and ends in braces "{ }".

cout << is a standard output command. So since we put cout << "Enter a number:"; the command prompt will display "Enter a number:"

\n means simply new line.

The << is known as an insertion operation. which 'inserts' our text into cout.

cin >> is a standard input which is normally the keyboard handling these kind of things. Which adjusts and sets itself to the entered integer. in this case, the integer would be the numbers entered for a and b.

c = a + b; this here is where the math happens. The program takes the integers set for a and b and then use addition to combine them. Like I said earlier you can replace the + with a -, *, and /.

return 0; is one of several methods used to exit the function.

Well that concludes this simple program. If you need any help are confused, or just want to ask a question feel free to! I will answer as soon as I can!

-Duggypker
Image
Current Project: From the Ashes RTS
User avatar
35000vr
Top Poster
Top Poster
Posts: 176
Joined: Sat Mar 06, 2010 5:09 pm

Re: Simple C++ Mathematics Program
35000vr
thanks!
‼ <----- Copy it,it is together and if you backspace it it will both erase
☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"<-----Some Cool symbols.
♂<-----Boy Symbol :)
²ƽ<--------Mini 2 and 5!
ð<----Not sure what it is.
☺☻<-----Smiles
♪♫<----Music Notes
Others:ß┬ƒ○║■ã¿┼↑
User avatar
Livengood
Serious Programmer
Serious Programmer
Posts: 444
Joined: Tue Feb 16, 2010 6:24 am

Nice job there Duggy :)
getting used to the C++ coding there?
good work my friend :D
Image
3 posts Page 1 of 1
Return to “C++ Tutorials”