0% found this document useful (0 votes)
5 views

Intensive Programming Lab 4

Uploaded by

a96546490
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Intensive Programming Lab 4

Uploaded by

a96546490
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Intensive Programming Lab

Lab 4: Basic Decision Constructs/Selection Statements (if-else)

Learning Objectives:

The objective of this exercise is to get you to write, compile and run a number of simple programs
in C which make use of basic decision constructs/selection statements.

Introduction:

Decision making structures require that the programmer specify one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false.

Following is the general form of a typical decision making structure found in mos t of the
programming languages:

 if statement:
An ‘if’ statement consists of a Boolean expression followed by one or more statements.
 if...else statement:
An ‘if’ statement can be followed by an optional ‘else’ statement, which executes when
the Boolean expression is false.
 nested if statements:
You can use one ‘if’ or ‘else if’ statement inside another ‘if’ or ‘else if’ statement(s).
Syntax:

if(boolean_expression) {
// statement(s) will execute if the boolean expression is true
}

Example 1:

Write a C++ program that input two numbers from user and tells the relationship they satisfy.

Code:
#include <iostream>
using namespace std;

/* function main begins program execution */


int main( void )
{
int num1; /* first number to be read from user */
int num2; /* second number to be read from user */

cout<<"Enter two integers, and I will tell you the relationships they satisfy:\n";

cin>>num1>>num2; /* read two integers */

if (num1!=num2) {
cout<<num1<< "is not equal to "<<num2<<endl;
} /* end if */

if (num1<=num2 ) {
cout<<num1<<" is less than "<<num2<<endl;
} /* end if */
if (num1>=num2) {
cout<<num1<<" is greater than "<<num2<<endl;
} /* end if */

if ( num1==num2) {
cout<<num1<<" is equal to "<<num2<<endl;
} /* end if */

return 0; /* indicate that program ended successfully */


} /* end function main */

Task 1: Write a C++ program to enter temperature in Fahrenheit and convert to Celsius. If value
of Celsius temperature is 25 print ‘Normal’ , if greater than 25 the print ‘High’ and if less than
25 print ‘Low’.

Task 2: Write a C-Program to check whether a number is negative, positive or zero.

Task 3: Write a C++ program to find maximum between three numbers.

Task 4: Write a C++ program to check whether a number is even or odd.

You might also like