Perfect Numbers

Tuesday, July 26, 2011 2 Comments A+ a-

C++ program to find Perfect Numbers between 1 to 1000


This program prints all the perfect numbers from 1 to 1000. Perfect numbers are those numbers in which the sum of the number's proper positive factors (excluding the number itself) is equal to that number.
Let's take the number 6 for example having the factors 1,2,3 and adding these factors will equate to the number itself (1+2+3 = 6).

/*
Perfect Numbers
show perfect numbers from 1-1000
*/

#include <iostream>

using namespace std;

int main(void)
{ 
 //variables
 int i,j,sumOfFactors;

 cout << "These are the perfect numbers:" << endl;

 //check numbers from 1-1000
 for (i = 1; i <= 1000; i++)
 {
  sumOfFactors = 0;

  //look for the factors of a number
  for (j = 1; j < i; j++)
  {
   //check for factors and add them up
   if (0 == (i%j))
   {
    sumOfFactors += j;
   }
  }

  //if perfect number, print
  if (sumOfFactors == i)
  {
   cout << i << endl;
  }
 }
   
 getchar();

 return 0;
}

Division and Remainder Without Using the "\" ,"%" Operators

Tuesday, July 19, 2011 2 Comments A+ a-

C++ program for Division and remainder without using the / and % operators




Division, just like multiplication, can be implemented without using its own division operator which is the character "/". This can be done through subtraction since division can be implemented as a series of subtraction.
In the code below, the while loop checks if the dividend is still greater than or equal to the divisor. If so, dividend will continue subtracting from itself the value of the divisor while quotient (initialized as 0) will increment by 1 each time. What remains for the dividend (lDividend) after the loop is the remainder of the division. 



/*
Division without using / operator
Remainder without using % operator
*/
#include <iostream>

using namespace std;

int main(void)
{ 
 //variables
 int dividend, divisor, quotient, remainder;
 char stay;

 cout << "Input dividend: ";
 cin >> dividend;

 cout <<endl << "Input divisor: ";
 cin >> divisor;

 quotient = 0;

 // always subtract dividend with the divisor
 //until dividend is lesser than the divisor
 while (dividend >= divisor)
 {
  dividend -= divisor;
  //quotient = count every time subtraction is done
  quotient++;
 }
 
 cout << endl << "The qoutient = " << quotient << endl;

 //remainder = the remaining value after a series of substraction
 remainder = dividend;
 cout << "The remainder = " << remainder << endl;

 //wait for an input before exit 
 cin >> stay;

 return 0;
}

Multiplication Without Using '*' Operator

Tuesday, July 19, 2011 1 Comments A+ a-

C++ program for Multiplication without using the * operator


Multiplication can be implemented without using the multiplication operator which is asterisks ( * ). This can be done through addition since multiplication is a series of addition.
In the code below, the first number is used as loop counter as to how many times the second number is added to the product variable initialized to 0.

/*
Multiplication without using * operator
*/
#include <iostream>

using namespace std;

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

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

 cout << endl << "Input second number: ";
 cin >> number2;

 cout <<endl << number1 << " * " << number2 << " = ";

 product = 0;

 // choose either of the two numbers for the addition
 // choose the other number as a counter
 while (number2 > 0)
 {
  //add the number
  product += number1;

  //decrement counter
  number2--;
 }
 
 cout << product <<endl;
 
 //wait for an input before exit 
 cin >> stay;

 return 0;
}

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;

}