FEDERAL COLLEGE OF AGRICULTURAL PRODUCE TECHNOLOGY, KANO
DEPARTMENT OF COMPUTER SCIENCE
Title: Programming Language Using C
Code: COM 221
Level: ND1
Semester: Second
Session: 2022/2023
LAB Practical Manual
Week One
computer is a machine that performs a variety of tasks according to specific instructions.
It is a data processing machine which accepts data via an input device and its processor
manipulates the data according to a program.
Types of Computer Programs
1. System Programs
2. Application Program
1. System Program
Program that are needed to keep all the hardware and software systems running together smoothly
Examples: Operating Systems like Linux, Windows, Unix, Solaris, MacOS.
2. Application Program
Application is set program instructions that direct the computer to perform specific task Example:
Microsoft Excel, Microsoft Word, Microsoft Power Corel draw, Browser etc.
Compiler:
Compiler is a software which used to translate high-language (programming language) like Java,
C, C++, C# etc. Source codes into lower-level language (machine code/bytecode) for directing the
computer to do task.
Example as follows:
1
1. Java Compiler e.g Eclipse, Jgrasp, NetBeans etc
2. C e,g Codeblocks, Visual Studio
3. C#
Source Code
(Program)
C Language
Compiler Data
Output
Data Input
Figure 1.0: C Compilation Process
2
EXECUTING/RUNNING C LANGUAGE SOURCE CODE:
Figure 2.0: Compiler Environment
Build and Run Program
Then, go to Build > click on Build and Run to run (execute) the program
To start running C program Source code
Click on Start Button and select Code blocks
Click on Build > click on Build and Run to run (execute) the
program
The program output should look like this below:
Hello World! Press any key to continue
Week Two:
3
Program 1
Write a C programming source code that print “Hello World!
Source Code:
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
Output:
Hello World!
Program 2:
Write a C programming source code that print “Hello World! and I am learning C
Source Code:
#include <stdio.h>
int main() {
printf("Hello World!");
printf("I am learning C.");
return 0;
Output:
Hello World! I am learning C.
Program 3
Write a program that print integer value
4
Source Code:
#include <stdio.h>
int main() {
int myNum;
myNum = 15;
printf(“%d”,myNum):
return(0); }
Output:
15
Week Three:
Program 4
Write a program that will print three different data types include integer, float and character values
in c compiler.
#include <stdio.h>
Int main()
int myNum = 5 ; // Integer (wholenumber)
float myFloatNum = 5.99; // Floating point number
char myLetter = 'D'; // Character
printf("%d\n", myNum); // Print variables
printf("%f\n", myFloatNum);
printf("%c\n", myLetter);
Return(0); }
Output:
5.99
5
D
Week Four:
Program 5
write a program that will declares more one variable on one line
Source Code:
#include <stdio.h>
int main () {
int x = 5, y = 6, z = 50;
printf("%d", x + y + z);
return(0);
Output:
61
Program 6
Write a C program that will print both text and variable
Source Code
#include <stdio.h>
int main (){
int myNum = 5;
printf("My favorite number is: %d", myNum);
return(0);
Output:
My favorite number is: 5
Program 7
Write a program that print different types in a single printf() function
6
Source Code:
#include <stdio.h>
int main (){
int myNum = 5;
char myCharacter = ‘D’;
printf("%d is an integer number and %c is a character\n", myNum, myCharacter);
return(0);
Output:
5 is an integer number and D is a character
Exercise 2
Write c program that calculate the sum and the average of four student’s scores of English, Maths,
Biology and Physics as 60, 70, 50 and 40.
i. Print the score of each subject
Week Five:
How to use constant Data value
Program 8
Write a program that will compute the area and circumference of the circle.
Source Code:
#include <stdio.h>
int main (){
int radius;
radius=5
float area, circumference;
const float pi=3.412;
area= pi*radius*radius;
7
circumference=2*pi*radius;
printf(“The area of a circle is \n“; area);
printf(“The circumference of a circle is \n”, circumference);
return (0);
Output:
The area of a circle is 78.5
The circumference of a circle is 31.42
Program 9
Write a program that calculate the area and circumference of the circle to input the radius of a
circle from keyboard
Source Code:
#include <stdio.h>
int main (){
int radius;
float area, circumference;
const float pi=3.412;
printf(“Enter the Radius of the circle :”);
scanf(“%d”,radius);
area= pi*radius*radius;
printf(“Enter the area of a circle is \n“; area);
circumference=2*pi*radius;
printf(“The circumference of a circle is \n”,circumference);
return(0); }
Output:
Enter the Radius of the circle :153.958
8
The area of a circle is 78.5
The circumference of a circle is 43.988
Week Six:
Program 10
Write c program that perform arithmetic operation with different arithmetic operators
Source Code:
#include <stdio.h>
int main () {
int num1 = 1550;
int num2 = 750;
int add = num1 + num2;
int difference = num1 - num2;
int multiple = num1 * num2;
int div = num1 / num2;
printf('Sum of ',num1 ,'and' ,num2 ,'is %d:',add);
printf(“Difference of ',num1 ,'and' ,num2 ,'is %d:”,dif);
printf(“Product of' ,num1 ,'and' ,num2 ,'is %d:”,mul);
printf(“Division of ',num1 ,'and' ,num2 ,'is %d:”,div);
printf(“Modulus of ',num1 ,'and' ,num2 ,'is %d:”,modulus);
return(0);
Output:
Sum of ',num1 ,'and' ,num2 ,'is :'2300,
Difference of ',num1 ,'and' ,num2 ,'is :',800)
Product of' ,num1 ,'and' ,num2 ,'is :',1,162,500)
9
Division of ',num1 ,'and' ,num2 ,'is :',2.0666)
Program 11
Write a program that compute the exponential and modulus of num1 and num2 of 5 and 2 ,
num1=5 num2=2 pow=num1**num2 mod=num1%num2
print(“Num1 to the Power of Num2 is:”,pow)
print(“Num1 modulus Num2 is:”,mod)
Output:
Num1 to the Power of Num2 is:25
Num1 modulus Num2 is:1
Exercise 3
Write a source code that perform arithmetic operation of the following operators:
(i) Addition (+) (ii) Subtraction (-) (iii) Multiplication (x) (iv) Division
(v) Exponential (vi) Modulus
Week Seven:
Program 12
Write a program concatenate and print the following string:
s1 = “Hello”;
s2 =”World!”;
Source Code:
#include <stdio.h>
int main(){
char s1=”Hello”;
char s2=”World!”;
printf(“%s”, s1+s2);
return(0);
}
10
Output
HelloWorld!
Program 13
Write a computer program using c language to print “Hello word!” with white space.
#include <stdio.h>
int main(){
char greetings[] = "Hello World!";
printf("%s", greetings);
return(0);
Output:
Hello World!
Exercise:
i. Write a python source code that concatenate the following string ‘F,’ ‘C’,’ ‘A’, ‘P’ and ‘T’.
ii. Write a program that concatenate and print the following string (i), (ii), (iii) and (iv):
i) WELCOME ii) TO iii) FCAPT iv) KANO
Week Eight
Program 14
Develop a computer program using C language to print numbers from 1 to 8 using do-while syntax
#include <stdio.h>
Int main (0){
int i = 0;
do {
printf("%d\n", i);
i++;
}
11
while (i < 8);
Return(0);
Output:
Program 15
Write C program source code that will print serial numbers from 1 to 10 using for loop
Source code:
#include <stdio.h>
int main(){
int i;
for (i = 1; i <= 10; i++) {
printf("%d The serial numbers are:\n", i);
return(0);
Output:
3
12
4
10
Exercise:
1. Use a for loop to write program that will print "Yes" 10 times
Week 9
Program 16
Write a program that change the Array index of 200 to replace 300 integer value and print it.
Source Code:
#include <stdio.h>
int main (){
int myNumbers[] = {200, 500, 750, 100};
printf("%d The First Array index are:\n", myNumbers[0]);
printf("%d The Second Array index are:\n", myNumbers[1]);
printf("%d The Third Array index are:\n", myNumbers[2]);
printf("%d The Fourth Array index are:\n", myNumbers[3]);
return(0);
Output:
The First Array index are:200
The Second Array index are:500
The Second Array index are:750
13
The Fouth Array index are:100
Program 17
#include <stdio.h>
int main (){
int myNumbers[] = {200, 500, 750, 100};
myNumbers[0] = 400;
printf("%d The new Array Number is now : ", myNumbers[0]);
return(0);
Output:
The new Array Number is now:400
Exercise:
1. write a program that can create array stored of 20,30,60,80,10 and 200 and print all their index.
Week 10
Source code:
Program 18
#include <stdio.h>
void myFunction() {
printf("This is COM121 course for ND 1 students ");
int main() {
myFunction(); // call the function
return(0);
Outputs
" This is COM121 course for ND 1 students”
14
Program 19
Write a C program that compute the power of number using math function to returns the value of x
to the power of y.
Source code:
#include <stdio.h>
#include <math.h>
int main () {
printf("%d X to the power of y is, “ ", pow(4, 3));
Return(0);
Output:
X to the power of y is 64
Week Eleven
Program 20
Write a program to execute the square root and round off numbers, use math functions
Source Code:
#include <stdio.h>
#include <math.h>
int main () {
printf("The Square Root of 16 is %f :\n", sqrt(16));
printf(" The rounds off number upwards to its nearest integer is %f ", ceil(1.4));
printf("The rounds off number downwards to its nearest integer is %f", floor(1.4));
return (0);
Output:
The Square Root of 16 is:4
15
The rounds off number upwards to its nearest integer 1.4 is 2.000000
The rounds off number downwards to its nearest integer 1.4 is 1.000000
Program.
16