Showing posts with label Math. Show all posts
Showing posts with label Math. Show all posts

Multiplication Table (10 x 10)

C++ program for Multiplication table (10 x 10)


Problem:
Write a multiplication table ( 10 x 10)
The following program shows a 10 x 10 multiplication table.
It uses the escape sequence for horizontal tab (\t) to properly align the table.

/*
Multiplication Table (10 * 10)
*/
#include <iostream>

using namespace std;

int main(void)
{
 int x,y;
 char waitInput;

 for(x=1; x<=10; x++)
 {
  for(y=1; y<=10; y++)
  {
   //use horizontal tab (\t) to align properly
   cout << "\t" << (x*y);
  }

  //newlines
  cout<<endl<<endl;
 }

 cin>>waitInput;
 return 0;
}

Reverse The Number

C++ program to reverse the number


Problem:
Write a C++ program which ask for a number and reverse its digits.
Ex. Number = 1234 , the answer will be 4321
In order to reverse the number, we have to get the every digit starting from the rightmost or the last digit. In the above example it is number 4, then next is 3, then 2 until 1.

Steps to extract each digit and construct the reversed value of the number:

1) Extract the last digit through the use of modulus operator (%) to get the remainder when the a number is divided by 10.
2) Add the extracted digit to a reversed number variable (ex. reversedNum) which will be constructed. The variable should be initialized to 0 and must be multiplied with 10 before the extracted digit is added.
3) After the last digit is extracted, divide the number with 10 to eliminate the extracted digit and start again to Step 1. The cycle continues until there is number is already 0.

The following program shows how to reverse the digits of a number.

/*
Reverse the number
*/
#include <iostream>

using namespace std;

int main(void)
{
 long number, remainder, reverseNumber = 0;
 char hang;

 cout<< "Enter the number: ";
 cin>>number;
 
 while(number)
 {
  //extract the last digit from the number
  remainder = number % 10;  

  //build the reverseNumber digit by digit
  reverseNumber = (reverseNumber * 10) + remainder;

  //exclude the last digit from the number
  //ready for the next evaluation
  number = number / 10;    
 }

 cout<< "Reversed number = " << reverseNumber <<endl;
 
 //to let console stay
 cin>>hang;
 
 return 0;
}
Output of the program:

//Sample 1

Enter the number: 1235
Reversed number = 5321

//Sample 2

Enter the number: 1023
Reversed number = 3201

//Sample 3

Enter the number: 5007
Reversed number = 7005

//*Sample 4

Enter the number: 012
Reversed number = 21

//*Sample 5

Enter the number: 210
Reversed number = 12

*Note that for inputs having 0 in the beginning and in the end, 0 will not be include in the output, See below:
Input = 012        Output = 21 ( 012 is considered as 12 because the 0 is not significant)
Input = 210        Output = 12 ( instead of 012, 0 is not significant in the answer)


Check If A Number Is A Prime Number


C++ program to check if a number is a prime number


Problem:
Write a C++ program which checks if a number is a prime number or not?
A prime number is any natural number greater than 1 with no other positive factors other than 1 and itself. 

Let's take 5 as an example:
1 and 5 (the number itself) can evenly divide 5 so they are factors of 5.
2, 3, 4 are cannot evenly divide 5 so they are not factors of 5.
Therefore, 5 is a Prime Number.
The program below shows a number is asked and it is evaluated if a prime number or not.


Prime Numbers Between 1-1000

C++ program to find prime numbers between 1 to 1000


Problem:
Write a C++ program which prints all the prime numbers from 1-1000. 

Prime numbers are numbers greater than 1 and which has 1 and the number itself as positive factors or divisors. It doesn't have a positive factor or divisors other than 1 and the number itself.

Let's take 5 for example:
1 and 5 (the number itself) can evenly divide 5 so they are factors of 5.
2, 3, 4 are cannot evenly divide 5 so they are not factors of 5.
Therefore, 5 is a Prime Number.

This is a program which shows the Prime Numbers from 1-1000.


Fibonacci Numbers using Recursion

C++ program - Fibonacci numbers using recursion


Problem:
Write a C++ program which prints the Fibonacci sequence or the Fibonacci numbers.
Fibonacci sequence is named after Leonardo Fibonacci. Fibonacci numbers starts with the first two numbers 0 and 1, and each subsequent numbers takes as the sum of its previous two numbers.

Ex. 0, 1, 1, 2, 3, 5
The first two numbers in the sequence are 0 and 1.
The 3rd number which is 1 is the sum of its previous two numbers - 0 and 1.
The 4th number which is 2 is the sum of its previous two numbers  - 1 and 1.
So on and so forth...
In mathematical equation, Fibonacci sequence can be represented as:

Fn = F(n-1) + F(n-2) where F0 = 0, F1 = 1

The following program shows the Fibonacci sequence using recursion.


Perfect Numbers

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

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

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

Mathematical Calculations ( MDAS )

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

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: