Lab 21

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

CLO 2P

POLITEKNIK SULTAN IDRIS SHAH


JABATAN KEJURUTERAAN ELEKTRIK
DEC20012 - PROGRAMMING FUNDAMENTALS
Title Introduction to C, Operators and Expressions
Practical
Work 2i

Course DEP 2A / DEP 2B / DTK 2A / DTK 2B


Session June 2019

Direction: Complete the practical works. Consult with your lecturer for any
problem encountered.

OBJECTIVES :

Upon completion of this practical, students should be able:


1. To apply output statements in simple C program.
2. To apply calculations by using operators and expressions.
3. To implement mathematical calculations in simple C program.
4. To implement mathematical calculations using the function in the main function.

EQUIPMENTS :

1. Personal computer / Laptop


2. Dev-C++ software

Lab 2.1 Opening, Compiling and Running Your First Program

Procedures:

2.1.1 Open Dev-C++ Program.


2.1.2 To start a new project click File > New > Project.
2.1.3 A New project pop up screen will appear. Under the Basic tab, select Empty
project and checked the C Project option button.
2.1.4 Rename the project as Practical1 and click Ok.
2.1.5 Right Click at the newly created Practical1 project name and choose New
file.

// This is the first program that just writes out a simple message

#include <stdio.h> // needed to perform C I/O


int main () { printf(“Hello World\n”);
printf(“This is my first C program”);
system(“pause”); return 0;
}

1 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2


CLO 2P

2.1.6 Write the above program and save it as Practical11. To compile, click on
Execute and choose Compile. Alternatively the program can be compiled by
using Ctrl + F9 hotkey.
2.1.7 Run the program and capture the codes, Compile progress and output.
To Run, simply click on Execute > Run. Alternatively hit the Ctrl + F10.

Lab 2.2 Now You Try!

Procedures:

2.2.1 By applying the steps involved in Lab 2.1 till 2.2, solve the following task.
2.2.2 Write your codes by using Dev-C++ software and check for any errors.
2.2.3 Rewrite your codes in the spaces provided.

Task A (Construct a simple C program)

Write a program that display the following output.

Welcome to C programming language!

Answer:

2 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2


CLO 2P

Task B (Compile and execute program)

Write a program that will display your name and registration number in a new
line. Use escape sequence character in your command.

Hello World, I like to learn C Programming! Kadijah


Binti Ismail
17DTK07F2020

Answer:

Task C (Escape sequence in simple C program)

Write a program that will display the following output by using escape
sequence characters (\n, \t, \”) in your program.
Hello World!
My course name is:
“DEC20012 Programming Fundamentals”

Answer:

3 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2


CLO 2P

Task D (Display output in specified format)

Write a program by only using the printf() function to produce the following
picture on your screen. Use escape sequence character in your program.

+--^----------,--------,-----,--------^-,
| ||||||||| `--------' | O
`+---------------------------^----------|
`\_,---------,---------,--------------'
/ XXXXXX /'| /'
/ XXXXXX / `\ /'
/ XXXXXX /`-------'
/ XXXXXX /
/ XXXXXX /
(________( `------'

Answer:

4 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2


CLO 2P

Task E (Apply input statements in simple C program)

Write the following program. Observe how scanf() function are used to get
input

#include <stdio.h> void


main()
{ char
name[100]; int
age;

printf("Enter Your Name:"); scanf("%s", &name);


printf("Enter Your Age:"); scanf("%d", &age);
printf("Your name is %s and your age is %d", name,
age); getchar(); getchar();
}

Answer:

[80 Marks]
5 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2
CLO 2P

Questions

1. Based on the given program:

&include <stdio.c> void


main()
( printf("Welcome\tto\t"): printf("Electrical
Engineering Department.\n"); printf(‘Good
Luck!!!\n"); getchar();
}

a. Circle the errors. (2.5 Marks)


b. Write the correct program. (2.5 Marks)
#include <stdio.h>
main()
{
printf(“Welcome\t to\t”);
printf(“Electrical Engineering Department.\n”);
printf(“Good Luck!!!\n”);
getchar();
}

c. Trace the output. (3 Marks)

Output:

6 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2


CLO 2P

2. Fill in the blank with the expected output for the following program. (4 marks)
#include <stdio.h>
int main()

{ char c =
‘A’;
char name[20] =
“C_Programming”; float f = 1.2;
int num = 15;
printf(“Character is %c \n”, c);
printf(“Name is %s \n” , name);
printf(“Float value is %.1f \n”, f);
printf(“Integer value is %d\n” , num);
Getchar(); return 0;
}

Output:

Character is _______A_______

Name is __C Programming___


Float value is _____1.2______
Integer value is ____15______

3. State TWO advantages of comments. (2 marks)

I ) Makes the code easily understandable by other programmers

II ) Helps remember we used a specific command , method , or function in the code

7 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2


CLO 2P

4. The following program generates three error messages as below:


#include<stdio.h>
int main()
int c;
{
printf("Hello!\n");
printf("Enter a number\n");
scanf("%d",@c);
printf("Number=%d",c);
getchar( );
getchar( );
return 0;
}

a. Rewrite the program to fix the errors. (3 marks)

Output

Prepared by: Checked by: Approved by:

MOHD ZEID BIN ABU BAKAR


PENSYARAH
JABATAN KEJURUTERAAN ELEKTRIK
POLITEKNIK SULTAN IDRIS SHAH

8 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 2

You might also like