Sunteți pe pagina 1din 4

Principles of C Programming

COLLEGE OF INFORMATION TECHNOLOGY PRINCIPLES OF C PROGRAMMING Structured Programming - Repetition WORKSHOP OBJECTIVES: By the end of this lab session, you should be able to: 1. Apply input validation to the program 2. Solve and write C programs using repetition control structure.

BASIC PROGRAM SKELETON For every C program that you write (either for lab exercises or lab assignments), please use the following template: /************************************************************* Author: Your name Date: date Description: basic description of what your program does what does it solve? Input: list of input required by your program Output: list of output produced by your progtam *************************************************************/ #include <stdio.h> void main(void) {
Write your program here

Principles of C Programming

Exercises (Please do ALL):


1. Using any repetition structure of your choice, write a program that will print the countdown from 5 to 0. Output sample: 5 4 3 2 1 0 Boomm!! $input (int); $count (int) = $input-1; Kau scanf (input) Then printf input-1 ($count) 2. Modify the above program so that instead of printing the countdown of 5, your program will print the countdown of any number (n) given by the user. For example: Output sample: Please enter a number: 8 8 7 6 5 4 3 2 1 Boomm!! Jawapan dia, buat formula jugak untuk printf (n-1) If n=0 printf(BOOM!!); Write a program that reads a positive integer n and then computes and prints the sum of all integers between 1 and n. Jawapan dia, buat formula for array: (buat formula)
3.

Principles of C Programming

4.

Enhance the above program so that your program will reads a positive integer n and then computes and prints the sum of all integers between 1 and n that is divisible by 3. Include appropriate input validation in your program.

If n<=0 { printf(Please enter positif integers); } Else { printf(Cou nting down $count, $count); }

5. Write a program that reads positive floating point numbers using repetition controlstructure until the user terminates the program by entering zero. Your program should determine and print the smallest and largest of the supplied numbers. Please include appropriate input validation in your program. Kau scanf dari user dalam format float (number perpuluhan) Then kau cari mana satu yang besar (buat decision statement) Pastu printf paling besar $palingbesar , $palingkecik

then Then if input == 0; { Return 0; } Sample output:

Principles of C Programming

6.

Write a program that reads two positive integers corresponding to two year values, ensures that the first year value is less than the second, and then determines and outputs all year values for leap years. A leap year is a year that can be evenly divided by 4, unless it is a centennial, in which case it must be evenly divided by 400. For example, 1600 and 1992 are leap years, whereas 1700 and 1998 are not (because 1700 even if it can be evenly divided by 4, it can not be evenly divided by 400).

Ni pun sama, scan je dari user, if $valuepertama >= $valuekedua kau printf (error, , value pertama mestilah lebih kecil bla bla); System ni ialah system cari tahun lompat antara tahun berapa ke tahun berapa so buatlah formula Kau kena set array senarai tahun2 lompat

S-ar putea să vă placă și