>ch; cout<<"the lowercase letter of "< >ch; cout<<"the lowercase letter of "<

Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010.

Similar presentations


Presentation on theme: "C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010."— Presentation transcript:

1 C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010

2 Quiz: Write a program that asks the user to enter an uppercase letter and prints out the lowercase of that letter.

3 Quiz Solution: #include Using namespace std; int main() { cout<<"enter an uppercase letter: "; char ch; cin>>ch; cout<<"the lowercase letter of "<<ch<<" is : "; cout<< (char)(ch+'a'-'A')<<endl; system("PAUSE"); return 0; }

4 Exercise #1: Write a program that takes a character input from the user then: 1.If the input character in uppercase print it in lowercase. 2.If the input character in lowercase print it in uppercase. 3.The output should be as follows:

5 Solution: #include using namespace std; int main() { cout << "Enter a letter: "; char ch; cin>>ch; if(ch>='a'&&ch<='z') {cout<<"you entered "<<ch<<". It is a lowercase letter\n"; cout<<"the uppercase equivalent is: "<<(char)(ch-('a'-'A'))<<endl;; } else if(ch>='A'&&ch<='Z') {cout<<"you entered "<<ch<<". It is an uppercase letter\n"; cout<<"the lowercase equivalent is: "<<(char)(ch+('a'-'A'))<<endl; } else cout<<"you entered "<<ch<<". It is not a letter\n"; system("pause"); return 0; }

6 Exercise #2: Write a program that takes 3 integers as inputs from the user then prints out the largest. for solution see lecture notes

7 Exercise #3: Write a program that takes 3 integers as inputs from the user then prints out the three numbers in descending order.

8 Solution #include using namespace std; int main() { cout<<"enter three integers: "; int i, j, k ; int num1, num2, num3; cin>>i>>j>>k; if (i > j) if (i > k) { num1=i; if (k>j) {num2=k; num3=j;} else { num2=j; num3=k; } } else {num1=k; num2=i; num3=j;} else if(j>k) {num1=j; if (i>k) {num2=i; num3=k; } else {num2=k; num3=i; } } else {num1=k; num2=j; num3=i;} cout<<"the numbers are: "<< num1<<" "<<num2<<" "<<num3<<" \n"; system("PAUSE"); return 0; }

9 Exercise #4: A. In the code fragment below, the programmer has almost certainly made an error in the first line of the conditional statement. 1. What is the output of this code fragment as it is written? 2. How can it be corrected to do what is the programmer surely intended? int n = 5; if (n = 0) cout << "n is zero" << ".\n"; else {cout << "n is not zero" << ".\n"; cout << "The square of n is " << n * n << ".\n";} B. What is the output when the following code fragment is executed? int n, k = 5; n = (100 % k ? k + 1 : k - 1); cout << "n = " << n << " k = " << k << endl;


Download ppt "C++ programming I Lab#3 Control Statements Instructor: Eng. Ruba A. Salamah Tuseday: 17/10/2010."

Similar presentations


Ads by Google