Vous êtes sur la page 1sur 7

Exercise 1 /* This program will demonstrate the use of Basic Arithmetic Operation Created by: Diana Jane T.

Lazaro Date: October 15, 2012 */ #include <iostream> using namespace std; //preprocessor directive //standard namespace

int main() //main function is the entry point of a C++ program { //begin //variable declarations int length, width, perimeter=0, product=0, sum=0; double depth, volume=0, undergroundsurfacearea= 0; //Input cout<<"Input Length : "; cin>>length; cout<<"Input Width : "; cin>>width; cout<<"Input Depth : "; cin>>depth;

//cout is the standard output comman in c++ //cin is the standard input statement in c++

//Process sum = length + width; product = length * width; perimeter = 2 * sum; volume = product * static_cast <double> (depth); undergroundsurfacearea = perimeter * static_cast <double> (depth) + product; //Output cout<<length<<" + "<<width<<" = "<<sum<<endl; cout<<length<<" * "<<width<<" = "<<product<<endl; cout<<"Perimeter = "<<2<<" * "<<sum<<" = "<<perimeter<<endl; cout<<"Volume = "<<product<<" * "<<depth<<" = "<<volume<<endl; cout<<"Underground Surface Area = "<<perimeter<<" * "<<depth<<" + "<<product<<" = "<<undergroundsurfacearea<<endl; system("pause"); return 0; } // end //Prompt the user to perss any key to go back to the main program // termintate the program normally

Exercise 2 /* This program will demonstrate the use of Basic Arithmetic Operation Created by: Diana Jane T. Lazaro Date: October 15, 2012 */ #include <iostream> using namespace std; int main() { int object, a=0, b=0, c=0, d=0, e=0; cout<<"Input Weight of Object : "; cin>>object; a = object / 100; b = (object - a c = (object - a d = (object - a e = (object - a

* * * *

100) / 50; 100 - b * 50) / 10; 100 - b * 50 - c * 10) / 5; 100 - b * 50 - c * 10 - d * 5) / 1;

cout<<"The number of weights needed for this object are: "<<endl<<a<<" For 100lb. "<<endl<<b<<" For 50lb. "<<endl<<c<<" For 10lb. "<<endl<<d<<" For 5lb. "<<endl<<e<<" For 1lb. "<<endl; system("pause"); return 0; }

// My First C++ Program #include <iostream> using namespace std; int main() { cout <<"Hello World"<<endl; system("pause"); return 0; }

/* This program will demonstrate the use of Basic Arithmetic Operation Created by: Cristina A. Pascua Date: October 15, 2012 */ #include <iostream> using namespace std; //preprocessor directive //standard namespace

int main() //main function is the entry point of a C++ program { //begin //variable declarations int num1, num2, sum=0, difference=0, product=0, intquotient=0, intremainder=0; double quotient=0; //Input cout<<"Input first number : "; cin>>num1; cout<<"Input second number : "; cin>>num2;

//cout is the standard output comman in c++ //cin is the standard input statement in c++

//Process sum = num1 + num2; difference = num1 - num2; product = num1 * num2; intquotient = num1 / num2; intremainder = num1 % num2; quotient = static_cast <double> (num1) / static_cast <double> (num2); //Output cout<<num1<<" + "<<num2<<" = "<<sum<<endl; cout<<num1<<" - "<<num2<<" = "<<difference<<endl; cout<<num1<<" * "<<num2<<" = "<<product<<endl; cout<<"Result of integer division = "<<num1<<" / "<<num2<<" = "<<intquotient<<endl; cout<<"Result of integer division = "<<num1<<" % "<<num2<<" = "<<intremainder<<endl; cout<<num1<<" / "<<num2<<" = "<<quotient<<endl; system("pause"); return 0; } // end //Prompt the user to perss any key to go back to the main program // termintate the program normally

/* This program wil prompt the user to input month represented by an integer number (112). Output will be the corresponding month in string. */ /*#include<iostream> using namespace std; int main() { int month; cout<<"Input month represented by an integer number (1-12) : "; cin>>month; //solution 1 using if else if (month == 1) cout<< month <<" else if (month == 2) cout<< month <<" else if (month == 3) cout<< month <<" else if (month == 4) cout<< month <<" else if (month == 5) cout<< month <<" else if (month == 6) cout<< month <<" else if (month == 7) cout<< month <<" else if (month == 8) cout<< month <<" else if (month == 9) cout<< month <<" else if (month == 10) cout<< month <<" else if (month == 11) cout<< month <<" else if (month == 12) cout<< month <<" else cout<< month <<" system("pause"); return 0; } */ #include<iostream> using namespace std; int main() { int month;

is equivalent to the month of January."<<endl; is equivalent to the month of February."<<endl; is equivalent to the month of March."<<endl; is equivalent to the month of April."<<endl; is equivalent to the month of May."<<endl; is equivalent to the month of June."<<endl; is equivalent to the month of July."<<endl; is equivalent to the month of August."<<endl; is equivalent to the month of September."<<endl; is equivalent to the month of October."<<endl; is equivalent to the month of November."<<endl; is equivalent to the month of December."<<endl; is an INVALID month."<<endl;

cout<<"Input month represented by an integer number (1-12) : "; cin>>month; //solution 2 using switch case switch (month) { case 1 : January."<<endl; case 2 : case 3 case 4 case 5 case 6 case 7 case 8 case 9 September."<<endl;

cout<< month <<" is equivalent to the month of

break; cout<< month <<" is equivalent to the break; : cout<< month <<" is equivalent break; : cout<< month <<" is equivalent break; : cout<< month <<" is equivalent break; : cout<< month <<" is equivalent break; : cout<< month <<" is equivalent break; : cout<< month <<" is equivalent break; : cout<< month <<" is equivalent

month of February."<<endl; to the month of March."<<endl; to the month of April."<<endl; to the month of May."<<endl; to the month of June."<<endl; to the month of July."<<endl; to the month of August."<<endl; to the month of

case 10: October."<<endl; case 11: November."<<endl; case 12: December."<<endl; default: }

break; cout<< month <<" is equivalent to the month of break; cout<< month <<" is equivalent to the month of break; cout<< month <<" is equivalent to the month of break; cout<< month <<" is an INVALID month."<<endl;

system("pause"); return 0; }

/* This program will prompt the user to input an integer month and output how many days are there in the month entered.*/ #include<iostream> using namespace std; int main() { int month; cout<<"Input an integer month (1-12) : "; cin>>month; //solution 1 if (month==4 || month==6 || month==9 || month==11) cout<<month<<" has 30 days."<<endl; else if (month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12) cout<<month<<" has 31 days."<<endl; else if (month == 2) cout<<month<<" has 28 or 29 days."<<endl; else cout<<month<<" is an INVALID input."<<endl; //solution 2 /*switch(month) { case 4: case 6: case 9: case 11: case case case case case case case 1: 3: 5: 7: 8: 10: 12:

cout<<month<<" has 30 days."<<endl; break;

cout<<month<<" has 31 days."<<endl; break; cout<<month<<" has 28 or 29 days."<<endl; break; cout<<month<<"is an INVALID input."<<endl;

case 2:

default: } */ system("pause"); return 0; }

/*

This program will simulate the Rock, Paper, Scissors Game. Created by: Diana Jane T. Lazaro Date: October 22, 2012

*/ #include<iostream> using namespace std; int main() { char P1, P2; cout<<"Let the Battle Begin..:)"<<endl; cout<<"Player 1, choose your weapon: "; cin>> P1; cout<<"Player 2, choose your weapon: "; cin>> P2; if (P1=='R' && P2=='S' || P1=='r' && P2=='s') cout<<"The winner is Player 1 because rock breaks scissors."<<endl; else if (P1=='S' && P2=='P' || P1=='s' && P2=='p') cout<<"The winner is Player 1 because scissors cut paper."<<endl; else if (P1=='P' && P2=='R' || P1=='p' && P2=='r') cout<<"The winner is Player 1 because paper covers rock."<<endl; else if (P1=='S' && P2=='R' || P1=='s' && P2=='r') cout<<"The winner is Player 2 because rock breaks scissors."<<endl; else if (P1=='P' && P2=='S' || P1=='p' && P2=='s') cout<<"The winner is Player 2 because scissors cut paper."<<endl; else if (P1=='R' && P2=='P' || P1=='r' && P2=='p') cout<<"The winner is Player 2 because paper covers rock."<<endl; else if (P1==P2) cout<<"Its a draw because they chose the same weapon."<<endl; else cout<<"Invalid Input."<<endl; system("pause"); return 0; }

Vous aimerez peut-être aussi