Docsity

C++ Programming: Relational Operators, If-Else Statement, and Lab Tasks, Exercises of Computer Programming

Toggle sidebar
bg1
Assignment Test SYN A
๎€ŠTake any Five Digit number and store that in
reverse order in a variable and display your
result.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12

Partial preview of the text

Download C++ Programming: Relational Operators, If-Else Statement, and Lab Tasks and more Exercises Computer Programming in PDF only on Docsity!

Assignment Test SYN A

๎€Š

Take any Five Digit number and store that in

reverse order in a variable and display your

result.

Assignment Test SYN B

๎€Š

Write a program to convert Decimal numbers

less than 32 to Binary number and store that

five digit binary number in a single variable.

Display your result

Relational Operators

๎€Š

number_1 = 44; //assignment

๎€Š

number_2 = 12; //assignment

๎€Š

(number_1 == number_2) // false

๎€Š

(number_2 <= 12) //true

๎€Š

(number_1 > number_2) //true

๎€Š

(number_1 >= 44) //true

๎€Š

(number_2 != 12) // false

๎€Š

(7 < number_2) //true

The If Statement

๎€Š

The if statement is the simplest of the decision

statements.

๎€Š

The if keyword is followed by a test expression

in parentheses.

// ifdemo.cpp // demonstrates IF statement #include using namespace std; int main() { int x; cout << โ€œEnter a number: โ€; cin >> x; if( x > 100 ) cout << โ€œThat number is greater than 100\nโ€; return 0; }

The if...else Statement

๎€Š

The if statement lets you do something if a

condition is true.

๎€Š

If it isnโ€™t true, nothing happens.

๎€Š

Suppose we want to do one thing if a condition

is true, and do something else if itโ€™s false.

๎€Š

Thatโ€™s where the if...else statement comes in

๎€Š

It consists of an if statement, followed by a

statement or block of statements, followed by

the keyword else, followed by another

statement or block of statements.

// badelse.cpp // demonstrates ELSE matched with wrong IF #include using namespace std; int main() { int a, b, c; cout << โ€œEnter three numbers, a, b, and c:\nโ€; cin >> a >> b >> c; if( a==b ) if( b==c ) cout << โ€œa, b, and c are the same\nโ€; else cout << โ€œa and b are different\nโ€; return 0; }

Lab Task 4

Question: Write a program that asks the user to enter his/her GPA. It must display the grade according to following criteria. (Note: If else if statements should be used.) GPA GRADE 4.00 A 3.500 โ€“ 3.999 B+ 3.000 โ€“ 3.499 B 2.500 โ€“ 2.999 C+ 2.000 โ€“ 2.499 C 1.000 โ€“ 2.000 D 0 โ€“ 1.000 F docsity.com

Lab Task 5

๎€Š

Write a temperature-conversion program that

gives the user the option of converting

Fahrenheit to Celsius or Celsius to Fahrenheit.

Then carry out the conversion. Use floating-

point numbers. Interaction with the program

might look like this:

Type 1 to convert Fahrenheit to Celsius

2 to convert Celsius to Fahrenheit : 1

Enter temperature in Fahrenheit : 70

In Celsius thatโ€™s : 21.