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;
}