INTERNATIONAL UNIVERSITY
C/C++ Programming
Course ID: IT116
Lab 2
Iterations
Full name + Student ID: ……………………………………………………………………………………………………
…………………………………………………………………………………………………….
Class: ……………………………………………………………………………………………………………………………….
Group: …………………………………………………………………………………………………………………………….
Date: ………………………………………………………………………………………………………………………………
_____________________________________________________________________________________
1
INTERNATIONAL UNIVERSITY
I. Objectives
Use the while, for, do...while repetition statement to execute statements in a
program repeatedly, identify between counter-controlled and sentinel-controlled
repetition, learn structured programming, use increment, decrement, assignment
operators.
II. Pre-Lab Preparation
Students are require to review theory of the topics before the lab time.
III. In-Lab Procedure
Exercise 1
Write a C program that reads 10 integers. The program should count the number of
positive and negative values among 10 inputs. Print them on the screen. (Use iteration
statements). Zero is neither a positive or a negative number.
Output:
Enter number 1: 16 Enter number 5: -21 Enter number 9: 42
Enter number 2: -7 Enter number 6: -15 Enter number 10: -37
Enter number 3: 23 Enter number 7: -33
Enter number 4: 49 Enter number 8: 54
Number of positive: 5
Number of negative: 5
Exercise 2
Write a C program that reads a student grade which is a character. The program should
print the classification based on the grade as:
_____________________________________________________________________________________
2
INTERNATIONAL UNIVERSITY
Grade Classification
A Excellent
B Good
C Fair
D Average
F Weak
Other characters Invalid
After showing the Classification, the program should ask the user to continue to read
another grade or not. by choosing yes (‘y’) or no (‘n’). If yes, continues, otherwise, prints “Exit
program...”.
Hints: Use switch Multiple selection
Output:
Enter a grade: B Invalid. Enter a grade: D
Good! Average!
Do you want to continue? y Do you want to continue? n
Enter a grade: Y Exit program...
Exercise 3
Write a C program to calculate the sum of series:
3 5 7
x−x + x −x …
The user should input the value for x and the number of terms
For Example
Output:
Input the value of x: 2
Input number of terms: 5
The sum of series: 410
_____________________________________________________________________________________
3
INTERNATIONAL UNIVERSITY
Exercise 4
Write a C program to calculate the factorial of a positive number.
The factorial can be described: n !=1× 2× 3 … n
Note: Do not use recursion function. Use iterations instead,
Output:
Enter a positive integer: 4
Factorial of 4= 24
Exercise 5
Write a C program to convert a Binary number to Decimal number using iterations.
Program should allow user to try again or to stop the program.
Output:
Enter binary value: 1010
Decimal value: 10
Exercise 6
Write a C program that Program to generate a “mystery” number by the computer, that is a
random number from 1 to 100. Then, the user will guess this number by prompting a value.
There are three possible responses:
1. Excellent! You guessed the number!
2. Too low. Try again...
3. Too high. Try again...
_____________________________________________________________________________________
4
INTERNATIONAL UNIVERSITY
The game ends when the user guesses the correct number. After that, let the user decide to
replay or not by choosing yes (‘y’) or no (‘n’)
Hint: Include stdlib.h and time.h. Use the following codes to generate a random number:
srand(time(0)); // random time moment
guess =rand()%100 +1 ; // generate a random number in the range [0-100]
Output:
I have a number between 1 and 100.
Can you guess my number?
Please type your first guess...
7
Too low. Try again...
50
Too high. Try again...
45
Too high. Try again...
42
Too high. Try again...
40
Excellent! You guessed the number!
Would you like to continue (y or n)?
n
Game ends here...
Bonus Exercise
_____________________________________________________________________________________
5
INTERNATIONAL UNIVERSITY
Write a C program that Program to calculate the sin( x) which is described in series as belows:
3 5 7
x x x
sin ( x )=x− + − +… where x is angle∈radian
3! 5! 7!
The user should input the value for x and the number of terms
Tips: take a reference from Exercise 3 and 4
For Example
Output:
Enter the value for x : 0.52
Enter the value for n : 5
The value of sin(0.52) = 0.4969
THE END
_____________________________________________________________________________________