

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Various c programming exercises focused on loops, including while, do-while, for, and nested loops. Students will practice writing and understanding code that uses these structures to iterate through arrays and perform calculations.
Typology: Lab Reports
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Todayโs lab has us playing with loops.
Problem 0:
Give the output from the following code:
a) int count = 0 ; while ( count < 5 ) { p r i n t f ( โ%d\nโ , count ) ; count++; } p r i n t f ( โ%d\nโ , count ) ;
Output:
b) int count = 5 ; int i = 0 ; while ( count > 0 ) { p r i n t f ( โ%d %d\nโ , i , count ) ; i ++; count โโ; } p r i n t f ( โ%d %d\nโ , i , count ) ;
Output:
c) int count = 5 ; int i = 0 ; while ( count > 0 ) { i = 0 ; while ( i < count ) { p r i n t f ( โ%d %d\nโ , i , count ) ; i ++; } count โโ; } p r i n t f ( โ%d %d\nโ , i , count ) ;
Output:
d) int count = 5 ; int i ; while ( count > 0 ) { for ( i =0; i < 5 ; i ++) { p r i n t f ( โ%d %d\nโ , i , count ) ; }
CPS 196: Introduction to C (Summer 2009) Page 1
count โโ; } p r i n t f ( โ%d %d\nโ , i , count ) ;
Output:
Problem 1: Convert the following program from using while-loop to use a for-loop:
int i =0 ; int j ; while ( i < 5 ) { j = 0 ;
while ( j < 5 ) { p r i n t f ( โ%d x %d = %d\nโ , i , j , i โ j ) ; j ++; } i ++; }
Problem 2 : Write a program that computes a grade average in several steps:
(1) First the program asks the user for the number of grades to enter. (2) Second the program prompts the user for the each grade individually. (3) Third the program displays the average of the numbers that are entered.
Problem 3: Write a program that gets an integer,n, from the user and then calculates n! or n factorial which is: n ร (n โ 1) ร (n โ 2)... ร 1
Problem 4: Write a program that acts as a simple printing calculator (Exer- cise: 4.0) The program should accept input in the form: number operator. Where valid operators are: +, -, *, /, %, S, E. Were E means exit and S means to set the accumlator to typed in number. The arithmetic operators are performed on the con- tents of the accumulator with the number that was keyed in acting as the second operand. Check for division by zero and unknown operators.
CPS 196: Introduction to C (Summer 2009) Page 2