All pastes #85471 Raw Edit

Anonymous

public text v1 · immutable
·published 2006-07-11 23:32 UTC
rendered paste body
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes
int menu();
int getNum( string, int, int );
int calcFactorial( int );
int calcMtoN( int, int );
int calcLCM( int, int );
int calcGCD( int, int );


int main()
{
int menuChoice, val1, val2, result;


//Get the user's choice

menuChoice = menu();


//While the user wants to perform a mathematical operation

while( menuChoice != 5 )
  {

// If the user wants to calculate a factorial, then get a number from the user,
// perform the calculation, and display the result

  if( menuChoice == 1 )
    {
	val1 = getNum( "Enter a number", 0, 10 );
	result = calcFactorial( val1 );
	cout << endl << "The factorial of " << val1 << " is " << result;
	}


// Otherwise if the user wants to raise a number to a power, then get the number
// and power from the user, perform the calculation, and display the result

  else if( menuChoice == 2 )
    {
	val1 = getNum( "Enter a number to be raised to a power", 1, 20 );
	val2 = getNum( "Enter the power", 1, 6 );
    result = calcMtoN( val1, val2 );
	cout << endl << val1 << " raised to the " << val2 << " power is " << result;
    }


// Otherwise if the user wants to calculate the least common multiple, then get
// the two numbers from the user, perform the calculation, and display the result

  else if( menuChoice == 3 )
    {
	val1 = getNum( "Enter the 1st number", 1, 50 );
	val2 = getNum( "Enter the 2nd number", 1, 50 );
    result = calcLCM( val1, val2 );
	cout << endl << "The least common multiple of " << val1 << " and " << val2 << " is " << result;
    }


// Otherwise the user wants to calculate the greatest common divisor, then get
// the two numbers from the user, perform the calculation, and display the result

  else 
    {
	val1 = getNum( "Enter the 1st number", 1, 50 );
	val2 = getNum( "Enter the 2nd number", 1, 50 );
    result = calcGCD( val1, val2 );
	cout << endl << "The greatest common divisor of " << val1 << " and " << val2 << " is " << result;
    }


//Wait until the user presses Enter before displaying the menu again

  system("pause");

//Get the user's next choice
  menuChoice = menu();
  }


cout << endl << endl << "Thank you for using the Math Operations Program" << endl;

return 0;
}

//**************     Code all of the functions below this line     **************

//*****************************************		//remember to put the function boxes in

int menu()
	{
	system("cls");
	int menuChoice;
	cout << "Mathematical Operations Menu\n"		
		<< "1) Calculate the FACTORIAL of a number\n"
		<< "2) Calculate M raised to the Nth POWER\n"	 	 
		<< "3) Calculate the LEAST COMMON MULTIPLE of two numbers\n"
		<< "4) Calculate the GREATEST COMMON DIVISOR of two numbers\n\n" 	 
		<< "5) Exit\n\n"
		<< "Enter your choice: ";
	cin >> menuChoice; 
	while(menuChoice < 0 || menuChoice > 5)
		{
		cout << "\nError: Invalid Entry, Try again.";
		cin >> menuChoice;
		}
	return menuChoice;
	}
//******************************************
int getNum(string str, int lower, int upper)
	{
	int num;
	cout << str <<" between " << lower << " and " << upper << ": ";
	cin >> num;
	while(num < lower || num > upper)
		{
		cout << "\nError: Number entered must be within limits. Try Again. ";
		cin >> num;
		}
	}
	
//******************************************
int calcFactorial(int val1)
	{
	int i;
	if (val1 == 0)
		{
		return 1;
		}
	for(i=val1 - 1; i>1; i--)
		{
		val1 *=i;
		}
	return val1;
	}
//******************************************
int calcMtoN( int num, int power)
	{
	int ndx;
	int temp;
	temp = num;
	
	for (ndx = 1; ndx < power; ndx++)
		{
		temp *= num;
		}
	return (temp);	  
	}	 
//******************************************
int calcLCM( int num1, int num2)
	{
	int i;
	i = num1;
	while ( i < num1*num2)
		{
		if(i % num1 == 0 && i % num2 == 0)
			{
			return i;
			}
		i++;
		}
	return i;
	}
	
//******************************************
int calcGCD(int num1, int num2)
	{
	int i;
	i = num1%num2;
	while(i != 0)
		{
		num1=num2;
		num2=i;
		i= num1%num2;
		}
	return num2;
	}
//******************************************