Source Codes
Back
Visual C++ Source Code for a Euclid Algorithim Calculator (Thanks Rob)
#include <iostream>
using namespace std ;
int main()
{
int x; //first number
int y; //second number
int temp; //holding space for x
int remainder; //holding space for the remainder
// read in the two integers
cout << endl ;
cout << "Enter the first number (integer) : " ;
cin >> x ;
cout << "Enter the second number (integer) : " ;
cin >> y ;
//echo inputs
cout << "Input numbers are: " << x << " , " << y << endl ;
if (x<y)
{ // exchange values of x and y
temp = x;
x = y;
y = temp;
}
/* At this point we will always have x >= y */
remainder = x % y;
while (remainder != 0)
{
x = y;
y = remainder;
remainder = (x % y);
}
// display the result
cout << endl ;
cout << "The GCD is: " << y << endl ;
return (0); // terminate with success
}
Binomial Coefficient Calculator
#include <iostream>
using namespace std ;
int binomial(int n, int k) ; // function prototype
int main()
{
int n, k ; // parameters for the binomial number
int result ;
cout << endl ;
// read in n & k
cout << "Enter n (positive integer) : " ;
cin >> n ;
cout << "Enter k (positive integer) : " ;
cin >> k ;
result = binomial (n, k) ;
cout << "Binomial number " << n << "C"
<< k
<< " = " << result << endl ;
return (0) ;
}
int binomial(int n, int k)
/*****************************************/
/* Computes the binomial coefficient nCk */
/* */
/* Inputs: */
/* n, k (integers) */
/* */
/* Output: */
/* binomial coefficient nCk (integer) */
/*****************************************/
{
int numerator, denominator ;
int i ; // needed to compute numerator & denominator
if (n < k)
{
return(0) ;
}
else
{
denominator = 0 ; // initial value
for (i = 1 ; i <= k ; i = i+1)
denominator = k * i ;
numerator = 0 ;
for (i = 1; i<= n ; i = n-k+1)
numerator = n * i ;
return (n, k) ;
} // else
}
Grade Calculator
#include <iostream>
using namespace std ;
int main()
{
float score; //score achieved in class
int grade; //score translated into letter grade
// read in total score
cout << endl ;
cout << "Enter total score (float, must be <= 100) : " ;
cin >> score ;
if (score >= 85)
grade = 'A';
else if (score >= 75)
grade = 'B';
else if (score >= 65)
grade = 'C';
else if (score >= 55)
grade = 'D';
else
grade = 'F';
// display the result
cout << endl ;
cout << "Your grade for CMIS 102 is: " << grade <<
endl ;
return (0); // terminate with success
}