Lab05-RepetitionStatements
Transcript of Lab05-RepetitionStatements
-
8/10/2019 Lab05-RepetitionStatements
1/8
ICS 103 Computer Programming in C
Lab# 4 Repetition StatementsObjectives:Learn C repetition statements by covering the following topics:
1. while loops
. !o"while loops. for"loops4. $este! loops
while"statement% while"statement is &se! to e'ec&te a statement or a compo&n!"statement (ero or more times aslong as the while"con!ition is tr&e:while(condition)
while-body
)he while"bo!y can be a simple statement in which case it m&st be terminate! by a semicolon or itmay be a compo&n!"statement in which case it sho&l! not be terminate! by a semicolon:
Simple statement while"bo!y Compo&n!"statement while"bo!y
while(condition) statement;
while(condition) compound-statement
*'amples:
while"loop o&tp&tint n + 1,while-n + 1/0 printf-23! 2 n0, n 5+ ,6
1 7 8 9
int + 1,while- ; ts?&are root>ts?&are>n20,while-n&m + 70 printf-23!>t3.f>t3!>n2 n&m s?rt-n&m0 n&m @ n&m0, n&m55,6
n&mber s?&are root s?&are 1.41 4 1.8 94 .// 1NMN> F !define M2MPK> int main(#oid) double grade, studentNotal, student#erage; int m, n;
for(m = &; m 1= M2>NMN>; m%%) studentNotal = 0.0; for(n = &; n 1= M2MPK>; n%%)
printf("nter uiHGrade!d for student!d$n", n, m); scanf("lf", 'grade); studentNotal %= grade; * student#erage = studentNotal / M2MPK>; printf("Nhe a#erage for student!d is .f$n", student#erage);
*
sstem("pause"); return 0;
*
Exercise:Do!ify the above program s&ch that it also comp&tes an! !isplays the class average.
8
-
8/10/2019 Lab05-RepetitionStatements
8/8
Laboratory )ass
Task 1: sing sentinel controlle! loop -while or for loop0 write a program that rea!s a ra!i&s fromthe &ser an! !isplays the area an! circ&mference of the circle with that ra!i&s. )his tas is repeate!&ntil the &ser types /.
%ss&me that the ra!i&s is in centimeters. se a name! constant A with val&e.149
Sample r&n:
Task 2: sing a while loop write a C program to !isplay a positive integer n&mber type! by the&ser in reverse.
Sample r&n:
Iint: se remain!er an! !ivision by 1/.
Task 3:sing nested for loops write a C program to print the pattern shown in the sample r&nsbelow on the screen.
Sample r&ns:
Task 4:sing a do-whileloop write a C program to chec that the &ser has type! a letter. )heprogram m&st not stop &ntil the &ser types a lowercase or &ppercase letter.
Note:se getchar( or fflush(stdinto sip the new line character after rea!ing each character.
Sample r&ns:
=