Lab Exercise 1

advertisement
INFO 2020_LabExer8
1
Lab Exercise 8
Chapter: Repetition
1.
Write a program to print the even numbers between 1 till 50. You have to write
three program using three method as well and there are;
a) for
save as LE8Q1a.c
b) while
save as LE8Q1b.c
c) do/while
save as LE8Q1c.c
2.
Write a program to print the odd numbers from 1 till 100. You have to write three
program using three method as well and there are;
a) for
save as LE8Q2a.c
b) while
save as LE8Q2b.c
c) do/while
save as LE8Q2c.c
3.
Write a program that sums a sequence of integers. Assume that the first integer
read with scanf specifies the number of values remaining to be entered. Your
program should read one value each time scanf is executed. A typical input
sequence might be;
5
100 200 300 400 500
where the 5 indicate that the subsequent 5 values are to be summed and the
sequence of numbers will be added with 100. Save your program in filename
LE8Q3.c
4.
Write a program that calculates and prints the average of several integers.
Assume the last value with scanf is the sentinel value -1. A typical input sequence
might be;
10
8
11
7
9
-1 (end)
The above numbers indicate that the average of all the values entered except -1 is
to be calculated. Save your program in filename LE8Q4.c
5.
Write a function program that prints the following patterns below. Use for loops
to generate the patterns. Numbers for row and column will input by user.
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
(Pattern A)
Save your program in filename LE8Q5.c
C Language
Zulfa Zakaria
2
INFO 2020_LabExer8
6.
Write a C program that will allow user to input ten integers continuously. The
user can input both positive and negative number. Then your program will
count how many negative integers and calculate average for positive integers
only. Apply a good programming writing that has comments, spacing and
indentation. The output of your programs as follows;
Input an integer: 2
Input an integer: 2
Input an integer: 2
Input an integer: 2
Input an integer: 2
Input an integer: -22
Input an integer: -100
Input an integer: -882
Input an integer: -2
Input an integer: -44
Total number of negative integer: 5
Average for positive integer: 5.00
Noted that the filename is LE8Q6.c
7.
Write a C program that will calculate salary for ten workers. Assume that the
minimum hour is 5 and maximum hour is 50. Then your program will
automatically calculate salary where as;
salary = hour * rate * 30;
(rate is RM10.00 per hour)
Apply a good programming writing that has comments, spacing and indentation.
Produce the output as follows;
StaffID
1
2
3
4
5
6
7
8
9
10
C Language
Zulfa Zakaria
Hour
5
10
15
20
25
30
35
40
45
50
Salary
RM1500.00
RM3000.00
RM4500.00
RM6000.00
RM7500.00
RM9000.00
RM10500.00
RM12000.00
RM13500.00
RM15000.00
3
INFO 2020_LabExer8
Write a C program that converts gallons to liters. The program should display
gallons from 10 to 20 in one gallon increments and the corresponding liter
equivalents. Use the relationship that 1 gallon contains 3.785. Save your program
in filename LE8Q7.c. At the end of your coding, the following output should be
displayed;
Gallons
10
11
12
…..
…..
19
20
Liters
37.85
41.635
45.42
….
….
….
75.7
Note: Apply a good programming writing that has comments, spacing and
indentation in your program. You can apply either for, while or do/while concept.
8.
Write a C program that converts feet to meters. The program should display feet
from 3 to 30 in three-foot increments and the corresponding meter equivalents.
Use the relationship that 1 meter is equivalent to 3.28 feet. Save your program in
filename LE8Q8.c. At the end of your coding, the following output should be
displayed;
Feet
3
6
9
12
15
18
21
24
27
30
Meters
0.915
1.830
2.744
…..
…..
…..
….
….
….
….
Note: Apply a good programming writing that has comments, spacing and
indentation in your program. You can apply either for, while or do/while concept.
C Language
Zulfa Zakaria
4
INFO 2020_LabExer8
9.
Produce a table of the numbers 0 through 20 in increments of 2, with their
squares and cubes. Save your program in filename LE8Q9.c. Your program
should display the following output;
NUMBER
0
2
4
6
….
….
….
…
18
20
SQUARE
0
4
16
36
….
….
….
….
324
400
CUBE
0
8
64
216
….
….
….
….
5832
8000
Note: Apply a good programming writing that has comments, spacing and
indentation in your program. You can apply either for, while or do/while concept.
Calculate square = number * number
Calculate cube = number * number * number
10.
Produce a table of the temperature for Celsius degrees start from 5 through 50
in increments of 5. Save your program in filename LE8Q10.c. Your program
should display the following output;
Degrees (Celsius)
5
10
15
….
….
Degrees(Fahrenheit)
41.00
50.00
59.00
…..
…..
50
122.00
Note: Apply a good programming writing that has comments, spacing and
indentation in your program. You can apply either for, while or do/while concept.
Calculate Fahrenheit = 32 + Celsius (180/100)
C Language
Zulfa Zakaria
INFO 2020_LabExer8
5
For the following questions, trace the output manually;
11.
What does the following program print? You must trace the flow of this program.
#include <stdio.h>
int main ( )
{
int count =1;
while ( count <= 10 )
{
printf (“%s\n”, count % 2 ? “****” : “!!!!!!!!” );
++ count;
}//while
return 0;
}//main
12.
What does the following program print? You must trace the flow of this program.
#include <stdio.h>
int main ( )
{
int row = 10, column;
while ( row > = 1 )
{
column = 1;
while (column <= 10 )
{
printf ( “%s”, row % 2 ? “<” : “>”);
++column;
}//second while
--row;
printf ( “\n” );
}//first while
return 0;
}//main
C Language
Zulfa Zakaria
Download