If, Else-If and Else Statements

Thursday, June 23, 2011 2 Comments A+ a-

C++ program for if, if-else and else statements


Let me present to you a program using the selection statements: if, if-else and else statements. These kind of statements are used when there's a condition or set of conditions that needs to be evaluated first in order to proceed and execute certain statements within a program.

/*
If, Else-If and Else Selection
*/
#include <iostream>

using namespace std;

int main(void)
{
 int age;
 char pause;
  
 cout << "Enter your age: ";
 cin >> age;

 //check the age entered   
 if (age == 18)
 {
  cout << "You are 18 years old!" << endl;
 }
 else if (age > 18)
 {
  cout << "You are older than an 18 year old!" << endl;
 }
 else
 {
  cout << "You are below 18 years old!" << endl;
 }

 cin >> pause;

 return 0;

}

Selection statements are very essential and are commonly used within a program because they provide a very important way of implementing an algorithm. They are used for choosing which statements are to be executed or what will the program do or behave within certain sets of conditions.



Mathematical Calculations ( MDAS )

Thursday, June 23, 2011 1 Comments A+ a-

C++ program for Mathematical calculations (MDAS)


This is a simple console application program which shows how we can do simple mathematical calculations like addition, subtraction, multiplication, division and getting the remainder using C++ programming language.


/*
Basic Mathematics program
*/
#include <iostream>

using namespace std;

int main(void)
{
 //variables
 int number1, number2, sum, difference, product, quotient, remainder;
 char pause;

 cout << "Enter first number = ";
 cin >> number1;

 cout << "Enter second number = ";
 cin >> number2;

 sum = number1 + number2;
 product = number1 * number2;

 //consider first which number is bigger
 if (number1 > number2)
 {
  difference = number1 - number2;
  quotient = number1 / number2;
  remainder = number1 % number2;
 }
 else
 {
  difference = number2 - number1;
  quotient = number2 / number1;
  remainder = number2 % number1;
 }

 cout << endl 
  << "Addition = " << sum << endl
  << "Difference = " << difference << endl
  << "Product = " << product << endl
  << "Quotient = " << quotient << endl
  << "Remainder = " << remainder << endl;

 //don't close windows console yet
 cin >> pause;

 return 0;
}


Before we proceed,  noticed that instead of always using

std::cin, std::cout and std::endl

as in our previous examples, now, each of them is already declared once through the using directive before (outside) the main function like

using std::cin;

however what we did here is specific to using only cin,cout and endl.  The purpose is to include only what are specifically used objects which are cin, cout and endl of the namespace std.
However, we could also have it this way:

using namespace std;

which uses the whole std namespace that contains cin, cout and endl. Either way you have the choice whatever implementation you like.

THE ALGORITHM : 
Now, let's take a look at the program, we can see that the user is informed to enter two numbers then the sum and product are calculated first. Then for the difference, quotient and the remainder, we first considered which of the two number has greater value so that we could get a positive result for the difference and for their quotient and remainder through the use of the if-else statement.

The operators we used:

+      //used for addition
-      //used for subtraction
*     //used for multiplication
/      //used for division
%    //modulus operation used to get the remainder of two numbers divided

Add and Multiply 2 Numbers

Monday, June 20, 2011 1 Comments A+ a-

C++ program to Add and Multiply two numbers


Let me show you a simple C++ console application program which will add two numbers and show their sum and product.


/*
Addition and Multiplication of two numbers
*/
#include <iostream>

using namespace std;

int main(void)
{
 //variables
 int number1, number2, sum, product;
 char stay;

 cout << "Input first number: ";
 cin >> number1;

 cout << endl << "Input second number: ";
 cin >> number2;
 
 // add the two numbers
 sum = number1 + number2;
 
 cout << endl << "The sum is " << sum <<endl;

 // multiply the two numbers directly within cout
 cout << endl << "The product is " << number1 * number2 << endl;
 
 //let console wait for an input before exit 
 cin >> stay;

 return 0;
}


Now let us go through the implementation. As we have seen we defined 4 variables:
  1. number1 and number2 - to hold the values of the two numbers
  2. sum - to hold the value of  the sum of number1 and number2
  3. stay - to let the program ask for an input before the console window closes. 
Now maybe you would notice that I did not defined a variable which will hold the value of the product of these two numbers. The answer is because I would like you to know or notice that aside from saving the product into another variable like what we did with the sum.

We can also directly process the calculation within cout and have it processed directly for output. But this would depend on the implementation the program would require and for this one this can just be applied as what we wanted was directly print the results of multiplying two numbers.

Output:

Hello World!

Monday, June 20, 2011 1 Comments A+ a-

Hello World! Our first C++ program


The best way to start learning C++ is to write our first program - Hello World. We will present the basic parts of a running program.

/*
Our first C++ program: Hello World
Print "Hello World!"
*/

#include <iostream>

using namespace std;

int main(void)
{ 
 //print "Hello World!"
 cout << "Hello World!" << endl;
  
 getchar();

 return 0;

}