C++ EXERCISES AND MIDTERMS

C++ EXERCISES AND MIDTERMS - Exercise 51 #include

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Exercise 51 #include "stdafx.h" #include <iostream> using namespace std; double totalcost ( int q, double u); int main () { int quantity; double unitprice, i; cout<< "Enter the quantity: " ; cin>>quantity; cout<< "Enter the unit price: " ; cin>>unitprice; i=totalcost (quantity, unitprice); cout<< "The total cost is " <<i<<endl; return 0; } double totalcost ( int q, double u) { double t; t=q*u; return t; } Exercise 52 #include "stdafx.h" #include <iostream> using namespace std; int myaddition ( int first, int second); int main () { int first, second, i; cout<< "Enter the first number: " ; cin>>first; cout<< "Enter the second number: " ; cin>>second; i=myaddition (first, second); cout<< "The sum of the two number is " <<i<<endl; return 0; } int myaddition ( int first, int second) { int sum; sum=first+second; return sum; } Exercise 53 #include "stdafx.h" #include <iostream> using namespace std; void myaddition ( int first, int second); int main () { int first, second; cout<< "Enter the first number: " ; cin>>first; cout<< "Enter the second number: " ; cin>>second; myaddition (first, second); return 0; } void myaddition ( int first, int second) { int sum; cout<<first<<endl; cout<<second<<endl; for ( int i=1; i<=20; i++) {sum=first+second; first=second; second=sum; cout<<sum<<endl; } } Exercise 54 #include "stdafx.h" #include <iostream> using namespace std; int integerPower( int base, int exponent); int main () { int i, base, exponent; cout<< "Enter the base: " ; cin>>base; cout<< "Enter the exponent: " ; cin>>exponent; i=integerPower(base, exponent); cout<< "The base raised to the power is " <<i<<endl; return 0; int integerPower( int base, int exponent) { int a; a=1; for ( int i=1; i<=exponent; i++) {a=a*base; } return a; } Exercise 57 #include "stdafx.h" #include <iostream> using namespace std; int factorial ( int ); int main () { int i; i=1; factorial (i); return 0; } int factorial ( int ) { int x, i, a, b; i=1; b=1; cout<< "Enter a number: " ; cin>>x; a=x; while (i<x) {a=a*b; b=b+1; i++; } cout<< "The factorial of number is " <<a<<endl; return a; } Exercise 58 int factorial ( int ); int main () { int i; i=1; factorial (i); return 0; } int factorial ( int ) { int x, i, a, b; i=1; b=1; cout<< "Enter a number: " ; cin>>x; a=x; while (i<x) {a=a*b; b=b+1; i++; } cout<< "The factorial of number is " <<a<<endl; return a; } Exercise 59 #include "stdafx.h" #include <iostream> using namespace std; int smallest ( int x, int y, int z); int main () { int x, y, z, min; cout<< "Enter first value: " ; cin>>x; cout<< "Enter second value: " ; cin>>y; cout<< "Enter third value: " ; cin>>z; min=smallest (x,y,z); cout<< "The smallest value is " <<min<<endl; return 0;} int smallest ( int x, int y, int z) { int a; if (x<y) a=x; else a=y; if (a<z) a=a; else a=z; return a; } Exercise 60 #include "stdafx.h" #include <iostream> #include "math.h" using namespace std; double distance( double x1, double x2, double y1, double y2); int main () { double x1, x2, y1, y2, d; cout<< "Enter the first x coordinate: "...
View Full Document

This note was uploaded on 10/15/2010 for the course ECE ECE 114 / taught by Professor Yin during the Spring '10 term at Cal Poly Pomona.

Page1 / 15

C++ EXERCISES AND MIDTERMS - Exercise 51 #include

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online