Sie sind auf Seite 1von 4

Principles of Programming (C Lab)

Structured Programming - Repetition


WORKSHOP OBJECTIVES:
By the end of this lab session, you should be able to:
1. Apply input alidation to the program
!. "ole and #rite C programs using repetition control structure.
Program example 1 : for loop
#include <stdio.h>
/* A simple program to print numbers from 1 to 5*/
main()
{
int i
for(i ! 1 i <! 5 i ! i " 1){
printf(#number $d %n&' i) /*print the number */
(
(
Program Example 2: While loop
/* )lass a*erage program +ith counter controlled repetition */
#include <stdio.h>
void main ( ) {
int counter, marks, total, average ;
total = 0;
counter = 1;
while (counter <= 10 ) {
rint! (" #nter mark $ %t");
scan!("&d",'marks);
total = total ( marks;
counter ((;
)
average = total*10;
rint! ("%n +lass average is $ &d%n", average);
)
1
Principles of Programming (C Lab)
Program Example 3 :
# include <stdio.h>
void main () {
int c, n,total,g;
!loat avg;
char name,-0.;
c=1;
total=0;
rint! ("/0me $ %t");
gets1s(name);
rint!("%n /o o! su23ect taken $%t");
scan!("&d",'n);
rint!("%n 44444444444444444444444444444444444444444%n%n");
!or(c=1;c<=n;c((){
rint!("5arks !or su23ect &d $%t",c);
scan!("&d",'g);
total= total(g;
)
avg=(!loat)total*n;
rint!("%n444444444444444444444444444444444444444444%n%n");
rint!("%n &s 6our average marks is &!%n",name,avg);
)
Program Example 4 : Nested Loop
#include <stdio.h>
void main () {
int col, row;
col =1;
row = 1;
!or (col=1; col <=7; col(()
{
!or (row=1;row<=col;row(() {
rint!("4");
)
rint!("%n");
) )
,
Principles of Programming (C Lab)
Exercises (Please do ALL):
1. $sing any repetition structure of your choice, #rite a program that #ill print the countdo#n
from % to &.
Ou!u sa"!le:
#
$
%
&
'
(
Boo""))
!. 'odify the aboe program so that instead of printing the countdo#n of %, your program #ill
print the countdo#n of any number (n) gien by the user. (or e)ample:
Ou!u sa"!le:
Please e*er a *u"+er: ,
,
-
.
#
$
%
&
'
Boo""))
*. +rite a program that reads a !osii/e integer n and then computes and prints the sum of all
integers bet#een 1 and n.
,. 'odify ans#er for -1, this time around, calculate and print the sum of all integers bet#een 1
and n that is divisible by 3. .nclude appropriate input alidation in your program.
%. +rite a program that reads the e)am mar/s for ten students and prints the number of passes
and failures (mar/s more than ,& is a pass).
0. +rite a program that reads positie floating point numbers using repetition control1structure
until the user terminates the program by entering 2ero. 3our program should determine and
print the s"alles and lar0es of the supplied numbers. Please include appropriate input
alidation in your program.
"ample output:
-
Principles of Programming (C Lab)
4. +rite a program that reads t#o positie integers corresponding to t#o year alues, ensures
that the first year alue is less than the second, and then determines and outputs all year
alues for leap years. A leap year is a year that can be eenly diided by ,, unless it is a
centennial, in #hich case it must be eenly diided by ,&&. (or e)ample, 10&& and 155! are
leap years, #hereas 14&& and 1556 are not (because 14&& een if it can be eenly diided by
,, it can not be eenly diided by ,&&).
"ample 7utput:
.

Das könnte Ihnen auch gefallen