Programming Fundamentals: by Imran Kazmi

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 8

Programming Fundamentals

Lecture 6
by
Imran Kazmi
Assignment given in the previous lecture
1) Write a program that input number of week’s day and display the name of the
day. For example if user enters 1, it display Friday and so on.(use switch statement)

2) write a program that input two numbers and an operator( +,-,*,/), in case the
operator is +, addition of two numbers is calculated, for * multiplication is
calculated and so on( (i) use switch statement, (ii)use if-else) .

3) Write a program to check whether a triangle is valid or not, while three angles of
the triangle is input through the keyboard. Triangle is valid if the sum of triangle is
equal to 180 degree.

4) Write a program to calculate the roots of the quadratic equation while value of a
, b and c is input through the keyboard. All cases should be discussed(b2-4ac>0,
b2-4ac<0,b2-4ac=0).
Loop Control Instructions

Computer has the ability to perform a set of


instructions repeatedly. This involves repeating
some portion of the program either a specified
number of times or until a particular condition is
being satisfied. This repetitive operation is done
through a loop control instructions.
Example

int sum ;
sum = 1+2+3+4+5+……..+10 ;
cout << sum ;
Loop Control Instructions

start

Initialize

False
test

True
stop
Body of loop

Increment/decrement
Loop
• Basic three methods through we can repeat a
part of a program

• i) using “ while ” loop


• ii) using “ for “ loop
• iii) using “ do-while” loop
While Loop
Initialization;
while ( Logical Expression )
{
do this;
do this;
and this;
change value of the expression;
}
Example

int sum , number ;


sum = 0 ;
number = 1 ;
while ( number <= 10 )
{
sum = sum + number ;
number = number + 1 ;
}
cout << “ The sum of the first 1000 integer starting from 1 is ” << sum ;

You might also like