Programming Fundamental2
Programming Fundamental2
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab Page 1
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab Page 2
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Programming Fundamentals
Lab # 1
How to install dev C ++ on your PC?
Procedure:
First of all I turned my PC on.
I opened google chrome and searched for “download Dev C ++”
I opened the first link and clicked on the download button.
Select where the software should be stored.
After these steps the software will start downloading.
After sometime software will be downloaded and installed.
Setup the software on your PC and it will be ready to use.
Conclusion:
The installation of dev C ++ has been successfully done. It was very easy to conduct all the
necessary actions to install dev C ++
Lab Page 3
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab # 2
How to print “Hello World” in Dev C ++?
Open dev C ++ on the computer.
Press ctrl + n to write a new program.
Enter the required library files at the top.
Write the “Main” function.
Write the specific code to print “hello world” in the main function body as shown in
figure 2.
Press F9 to compile the program.
Press f10 to run the program as shown in figure 3.
Lab Page 4
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Conclusion:
The “Hello World” message appeared on the output window. It was an easy program you just
need to learn basics of the language and you can make these kinds of programs very efficiently.
Lab Page 5
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab # 3
Performing DMAS operations:
Open dev c + +.
Press ctrl + n to write new program.
Enter the mandatory library files.
Write the main function.
Declare variables and use scanf function to take values from user.
We perform DMAS operations by simply putting “+” for addition “-“ for subtraction “*”
for multiplication and “/” for division between initialized variables.
Use printf function to show the results in output.
Press F9 to compile the program.
Press F10 to run the program
Figure # 6 “Division”
Lab Page 6
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Conclusion:
In addition, multiplication, and subtraction I entered two numbers of my choice
and took one number from the user and performed the given arithmetic operation
on them respectively and displayed the result.
In the case of division I entered one number myself and took the other from the
user, performed the operation and printed the result on screen.
Lab Page 7
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab # 4
Program to print “hello world” multiple times using for loop:
At first, I opened Dev C ++.
Created a new source file by pressing ctrl + n.
I entered the required library files to write the program.
I wrote the main function to indicate the starting of the program body.
I used for loop to print “hello world” multiple times as shown in fig# 10.
I declared the variable x a value “1” and wrote the condition “x <=10” and made an
increment of 1 according to syntax of for loop as shown in fig# 7.
I compiled the program by pressing F9 and ran it using F10.
Hello world is printed on screen multiple times as shown in figure# 8.
Conclusion:
By using the loops we can do the same task many times without writing the program again and
again that’s how I printed hello world on the screen 10 times using for loop.
Lab Page 8
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab # 5
Program to detect whether the number is even or odd:
At first, I opened Dev C ++.
Created a new source file by pressing ctrl + n.
I entered the required library files to write the program.
I wrote the main function to indicate the starting of the program body.
I took a number from the user using scanf function.
I used if else statement to check that the number is even or odd>
I wrote the condition if (x%2==0) then number is even otherwise it is odd as shown in
figure # 9.
I compiled the program by pressing F9 and ran it using F10.
The result is printed on screen depending upon which number the user entered as shown
in figure# 10.
Conclusion:
Lab Page 9
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
I took a number from the user and checked whether it is even or odd using if else statement. In
result of which it asks me for a number on pressing F10 and after getting the number it tells me if
it is even or odd.
Lab # 6
Lab Page 10
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Figure # 11 “shows
Lab Page 11
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Conclusion:
After compiling the program you will see the value of x and y swapped successfully.
Lab Page 12
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab # 7
Program using switch statement:
At first, I opened Dev C ++.
Created a new source file by pressing ctrl + n.
I entered the required library files to write the program.
I wrote the main function to indicate the starting of the program body
Declare an integer variable x and set its value to 2.
Use a switch statement to check the value of x and print different information based on
the case.
Case 1: Print the name.
Case 2: Print the registration number.
Case 3: Print the section.
Default: Print an error message for an invalid choice.
The default case handles situations where the value of x does not match any of the
specified cases. It prints an error message indicating that the choice is invalid..
Conclusion:
Lab Page 13
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
When you run this program, it will print the registration number since x is set to 2. If you change
the value of x to 4, for example, the default case will be executed, and the program will print the
error message.
Lab # 8
Program to calculate average:
Lab Page 14
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab Page 15
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Conclusion:
Compile and run the program, and it will prompt the user to enter three numbers, calculate their
average, and display the result.
Lab Page 16
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab # 9
How to make while loop in Dev C++?
I opened the Dev C++ software and created the new file by using
shortcut key ctrl+N.
Then, I typed the pre-processor directive #include and header file
<stdio.h>
Then, I defined the starting point and specified about the data type int
main().
Then, I typed the curly brackets and specific library “int’’ and specified
the variable “x”.
Then, I typed the condition for the while loop for increment in the
variable x.
i.e. printf ("This is a test program\n");
x++;
And then gave the instruction of printing the output of the loop on the
screen
Then, I compiled and executed the program simultaneously by
pressing F11 the button on keyboard.
Lab Page 17
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Conclusion:
In this lab, I learnt how to program an increment in while loop for the integer
whose value were ranging from 1-6 on Dev C++ tool. Then I was taught how
to implement the increment and decrement in while loop in modern
programming.
Lab # 10
Lab Page 18
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab Page 19
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab Page 20
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab no. 11
How to implement arrays on Dev C++?
I opened the Dev C++ software and created the new file by using shortcut key ctrl+n.
Then, I typed the pre-processor directive #include and header file <stdio.h>.
Then, I defined the starting point and specified about the data type int main ().
Then, I typed the curly brackets and specific library “int’’ and specified the variables “a”.
Then, I typed the instruction to take the input from user for variable a.
Then, I introduced the switch statement and assigned cases to it.
Then, I compiled and executed the program simultaneously by pressing F11 the button on
keyboard.
Lab Page 21
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab Page 22
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab no. 12
How to implement for loop in arrays on Dev C++?
I opened the Dev C++ software and created the new file by using shortcut key ctrl+n.
Then, I typed the pre-processor directive #include and header file <stdio.h>.
Then, I defined the starting point and specified about the data type int main ().
Then, I typed the curly brackets and specific library “int’’ and specified the variables “a”.
Then, I typed the instruction to take the input from user for variable a.
Then, I introduced the switch statement and assigned cases to it.
Then, I compiled and executed the program simultaneously by pressing F11 the button on
keyboard.
Lab Page 23
National College of Business Administration &
Economics Lahore
SCHOOL OF COMPUTER SCIENCE
Lab Page 24