Sie sind auf Seite 1von 17

WHILE

Programming Fundamentals (CS118) Fall 2018


Abeeda Akram
Print 1-1000 numbers
Start
Set counter = 1
while(counter <= 1000)
Output counter
increment counter
end while
Stop
Loop
Initialize counter
while (condition)
statements
increment counter
End While
Exercise 1
Pseudocode:

Count = 1 What is output of above program


WHILE (Count < 5)

OUTPUT Count 1234


Count= Count +1

END WHILE
Exercise 2
Pseudocode:

Count = 1
What is output of above program
WHILE (Count < 5)

OUTPUT “ * ” ****
Count= Count +1

END WHILE
Exercise 3
Pseudocode:
Count = 1

WHILE (Count < 5)


What is output of above program
OUTPUT Count
Count= Count +1
END WHILE 1234****
Count = 1
WHILE (Count < 5)

OUTPUT “ * ”
Count= Count +1
END WHILE
Exercise 4
Pseudocode:
Count = 1
Z=1

WHILE (Count < 10) What is output of above program


1 1
IF Count is equal to 3
Z=2
3 2
OUTPUT ZEND IF 5 2
7 2
OUTPUT Count “ ” 9 2
OUTPUT Z
OUTPUT new line
Count= Count + 2
END WHILE
Exercise 5
Input Variables: X
Pseudocode:
Z=1
Count = 1
What is output of above program
WHILE Count < 3 if input is
INPUT X 10 5
IF X is greater than 5
Z=2
ELSE
Z=4
END IF

Count= Count +1
END WHILE
OUTPUT Z
Exercise 6
Pseudocode:
Sum = 0
Count = 1

What is output of this program


WHILE Count < 5

Sum = Sum + Count


Count= Count +1
END WHILE

OUTPUT Sum
Exercise 7
Pseudocode:

Count = 1

WHILE Count > 0


Infinite Loop
Count= Count +1
that never
END WHILE terminates
Exercise 8

Pseudocode:

Count = 0 Loop that will never


WHILE Count NOT equals t0 zero
true
OUTPUT Count
Count= Count +1

END WHILE
Exercise9
Pseudocode:
Count = 1
What is output of above program

WHILE (Count < 5)

OUTPUT “ * ”
Count= Count +1
If (Count == 4)
OUTPUT “ New line”
Count = 1
END IF
END WHILE
Write Code:
• To calculate Factorial of any number.
• To calculate the power of any given number.
• To Print Table of Any Number up till 10:
Exercise 11
Print Table of Any Number
1. Set Number1 = 1 Exercise 12
2. Set Number2 = 2
3. Set Number3 = 3
4. Set value of Sum = Number1+Number2+Number3
5. while (sum <= 39)
6. begin while loop
Number1 = Number2
Number2 = Number3
Number3 = Sum
Sum = Number1+Number2+Number3
output “*”
7. end of while loop
8. output “Number1 is :”, output Number1
9. output “Number2 is:”, output Number2
10. output “Number3 is:”, output Number3
11. output “Sum is:”, output Sum
START
Exercise 13
Number1 = 1
Number2 = 30
Sum = 0
while (Number2 >= Number1)
begin while loop
Number1 = Number1 + 1
Number2 = Number2 – 2
Sum = Number1 + Number2
end while loop
output “Number1 is :”, output Number1
output “Number2 is:”, output Number2
output “Sum is”, output Sum
END
Problems
1. Write pseudocode for a program which reads 10 integers from the user,
stopping only when the user, and output the largest value input.
2. A student buys various books at the start of the semester from a bookshop.
Write pseudocode for a program which takes the total amount spent on
books and the total number of books as input and outputs the average cost
per book.
3. Write pseudocode for a program that prints the numbers from 1 to 100. But
for multiples of three print “Fizz” instead of the number and for the multiples
of five print “Buzz”. For numbers which are multiples of both three and five
print “FizzBuzz”.

Das könnte Ihnen auch gefallen