You are on page 1of 3

EC1450 – Fundamentals of Programming

Faculty of Engineering
B.Sc. Eng. (Hons) in Engineering– Year 1 Semester 2

Laboratory Experiment 02

Following sample code prints, numbers in a range in the ascending order. The user can define the
minimum and the maximum limits of the range.

#include <stdio.h>

void main () {

int num = 0,i;


int min = 0, max = 0;
int b;

printf("Lab 01 sample code\n");


printf("-----------------------------\n");

printf("Enter the range,\nMinimum:");


scanf("%d",&min); //gets an user input to variable min
printf("Maximum:");
scanf("%d",&max); //gets an user input to variable max

//print table headers


printf("The integer numbers between %d - %d,\n",min,max);
printf("Generated_Number\t|\n");
printf("----------------\t|\n");

//a loop to print numbers from min - max with an increment of


1
//i=min -> initialize to the first value
//i=max -> initialize to the last value
//i++ -> increment i by 1
for(i=min; i<=max; i++){
printf("\t%4d\t\t|",i); //print the i value
//till the maximum entered value.

//write your code in here

printf("\n");
}
printf("\n");
}
Complete all the tasks given in the sheet and demonstrate it to an instructor. You should

Page 1 of 3
print all the results in the same console window. Refer Fig. 01, you may develop your own
code to obtain the same result.

Fig. 01

Task 01

Compile the code given above and observe the output. Give different input values and
understand the code.

Task 02

Divide all the generating numbers by 3 (three). Obtain the remainder of each.
Display your results in two different columns.

Task 03

Modify your code to display the current number is odd or even.

Hint: even numbers can be divided by 2 and the remaining value will be zero whilst even
numbers won’t.
You may use the example code given below.

#include<stdio.h>

Page 2 of 3
void main () {
int a,b;
// example of ternary operator
b = 5;
a = 10;
(a == b) ? printf("a and b are equal"): printf( "a and b are
not equal");
}

Page 3 of 3

You might also like