Sie sind auf Seite 1von 7

Lahore University of Management and Sciences

Lab-1 Manual

for

Introduction to Programming
(CS200)

LAB GUIDELINES
 Make sure you leave the lab after your work has been graded. Any ungraded work will not be
graded afterwards. Last 20 mins are dedicated for evaluation purposes.
 For every lab, there will be a folder created on LMS. You must submit your work in the
respective folder during the lab time, you and only you are responsible for your submissions.
 You will be allowed to discuss the questions in the first half of the lab session for a few
questions. After that, there will be a portion of lab where you cannot converse and must work
for yourself. No discussion is allowed in later time period.
 You should do your work with utmost clarity and precision. Do not waste your time trying to
do something you do not understand. Ask Lab instructors for help, that is what they are there
for.
 Any legitimate cheating case can and will be reported to Disciplinary Committee without any
leniency.
 Please follow the lab antiquates and follow code of conduct in the session.

Wednesday, September 11, 2019 CS-200 Lab-1


OBJECTIVES
 Introduction to Visual studio: Installation, Creating Project
 Basic C++: Variables, Loops, Functions, Switches
 Datatypes: int, float, long, char, String

REVISION
 Given a positive integer, write a C++ Program to Compute its factorial.
#include <iostream>
using namespace std;
int main()
{
int number = 0; int factorial = 1;
cout << "Enter a positive integer: ";
cin >> number;
if (number >= 0)
{
for (int i = 1; i <= number; i++)
{
factorial *= i; // factorial = factorial * i;
}
cout << "Factorial of " << number << " = " << factorial;
}
else
{
cout << "You entered a negative number";
} return 0;
}

Input Output
1 1
5 120
9 True
40 False

Wednesday, September 11, 2019 CS-200 Lab-1


LAB ACTIVITY
 What will be the output of the following program?
#include <iostream>
using namespace std;
void main()
{
int i, j;
int charVal = 'A';
for (i = 5; i >= 1; i--)
{
for (j = 0; j < i; j++)
cout << charVal + j << endl;
}
}

 What will be the output of following program?


 Input: 3 1 5 10 5 55 -2 5 33 9 -1
#include <iostream>
using namespace std;
void main()
{
int number = 0;
int maximum = 0;
cout << "Enter a Number: ";
cin >> number;
cout << "\n";
if (number != 0)
{
maximum = number;
}
while (number != -1)
{
cout << "Enter a Number: ";
cin >> number;
cout << "\n";
if (number > maximum)
{
maximum = number;

}
cout << "Largest number in the given sequence: " << maximum;
}

Wednesday, September 11, 2019 CS-200 Lab-1


VISUAL STUDIO 2017 PROJECT CREATION

Step 1: Download Visual Studio 2017 exe file from https://visualstudio.microsoft.com/downloads/.


After it has finished downloading, execute it.
Step 2: During installation Select Desktop Development with C++. You can select other
options as well but for this course we only need this one.

Step 3: After the installation has completed, run the Visual Studio and created a new project by going to
File->New->Project.

Wednesday, September 11, 2019 CS-200 Lab-1


Step 4: Select Windows Console Application, give a name to the project, and select the
location. Then press “OK”

Step 5: You will see the following window. To execute the program, Go to Debug->Start
Without Debugging. Or alternatively, you can press “Ctrl+F5”

Wednesday, September 11, 2019 CS-200 Lab-1


Step 7: A console will appear where you can observe the output of your code.

LAB EXERCISES

Question#1: [Marks: 25] Est. Time: 30 mins

Write a C++ program that takes integers as input from the user. The input should
stop only when all the conditions below are met:

 The user has entered more than 10 numbers.


 Number 5 has entered at least once.
 Sum of given numbers is greater than 100.

Final output: Total number of inputs, sum of numbers and total how many times
number 5 was entered as input.

Note: User can input negative integers as well.


Example: If user enters the number: 50, 5, 10, 8, -7, 50, -30, 24, 89, 1, 0.
The output should be:
Total number of inputs: 11

Wednesday, September 11, 2019 CS-200 Lab-1


Sum of numbers: 200
No. of times 5 was entered: 1

Question#2: [Marks: 30] Est. Time: 40 mins

The ancient Greeks classified numbers geometrically. For example, a number was
called “triangular” if that number of pebbles could be arranged in a symmetric
triangle as shown in the figure below. The first ten triangular numbers are 1, 3, 6,
10, 15, 21, 28, 36, and 45. Write a C++ program which contains a function that takes
as input a number N and prints whether it is triangular or not. You are also required
to write the driver code i.e. main function.

Note: You do not have to create the shapes you just have to state whether the number
is triangular or not. Your program should run on following test samples.

Input Output
0 True
11 False
1431 True
900 False

Question#3: [Marks: 45] Est. Time: 50 mins

Write a function, reverseDigit, that takes an integer as a parameter and returns the
number with its digits reversed. Your program should fulfil all test cases below:
Input Output
12345 54321
5600 65
7008 8007
532 235

You are also required to write the driver code for the function i.e. main function.
Hint: Make use of quotient and remainder.

Wednesday, September 11, 2019 CS-200 Lab-1

Das könnte Ihnen auch gefallen