Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Lab 03-Programming Fundamentals

The lab manual focuses on programming fundamentals, specifically identifying variables and data types, and writing complete programs to convert user input into required outputs. It covers the declaration and initialization of various data types in C++, along with practical tasks for students to implement these concepts. The manual concludes with multiple programming exercises aimed at reinforcing the learned skills.

Uploaded by

williamrjone01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lab 03-Programming Fundamentals

The lab manual focuses on programming fundamentals, specifically identifying variables and data types, and writing complete programs to convert user input into required outputs. It covers the declaration and initialization of various data types in C++, along with practical tasks for students to implement these concepts. The manual concludes with multiple programming exercises aimed at reinforcing the learned skills.

Uploaded by

williamrjone01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming Fundamentals

Week 03 - Lab Manual

Introduction
Welcome to your favorite programming Lab. In this lab manual, we shall work together to learn
and implement new programming concepts

Skills to be learned:
● Identifying the Variables and Datatypes for any given problem.
● Write a complete program that converts input into the required output.

Let's do some coding.


Skill: Identify the Variables and Datatypes for any given problem.

Introduction
Variables are the containers that are used to store different values. Recall the constraints of
having valid variable names from the class.
● The names can not have spaces
● The names can not start with Numbers
● The names can not have any special Characters

Datatypes are the labels that are associated with each container that are used to store different
values.

The following table lists the data types that are used to store different values.
Datatype Description

int This datatype is used to store integer values.

float This datatype is used to store floating point values.

char This datatype is used to store single-character values.

string This datatype is used to store a string of character values.

Examples:
Valid Example Invalid Examples Description

Skill: Identifying the Variables and Datatypes for any given problem.
Programming Fundamentals
Week 03 - Lab Manual

C++ is a case-sensitive language. Int


and int are two different words and
represent different things.

Spaces are not allowed in the


variable names.

The variable names can not start


with numbers.

Following are a few examples of how to declare, initialize, and assign values for different
types of variables.

Declaring and initializing a string


type variable

Declaring and initializing a character


type variable

Skill: Identifying the Variables and Datatypes for any given problem.
Programming Fundamentals
Week 03 - Lab Manual

Declaring and printing an int type


variable

Declaring and printing a float type


variable

Skill: Identifying the Variables and Datatypes for any given problem.
Programming Fundamentals
Week 03 - Lab Manual

Skill: Write a complete program that converts input into the required output

In class, you have studied variable declaration that is used to declare a variable of fixed size in
memory. Additionally, the assignment operator is used to assign a value to the declared variable.
Look at the attached example for recalling
these concepts.

Variable declaration

Variable Initialization

Tasks
● Declare a string-type variable and assign it your name and print it on the screen.
● Declare an integer type variable and assign it your roll number and print it on the console
screen
● Initialize a float type variable with your aggregate value and print it on the console
screen.
● Initialize a character type variable with your section and print it on the screen.
● Now, write a program where you take all these values and print them on the screen like
below.

Now, let's learn to take input from the user.

consider the following problem.

Task01(WP): Write a program that takes a number from the user(console screen) in dollars
and converts it into rupees. 1 Dollar = 200 rupees
Let’s code this one out.

We need the following:


● A variable for storing the value of one dollar
● A variable for storing the value of value entered by the user
● A variable to store the converted value in rupees
● An expression that converts the dollars into rupees and stores it into the third variable

Skill: Write a complete program that converts input into the required output
Programming Fundamentals
Week 03 - Lab Manual

Can you point out 🔎 the problem for the above code?
Yes, in this program, the input value is not entered by the user rather it is set by the
programmer just like the dollar value.

We need the user to enter the value of dollars that he wants to convert into rupees.
We have just the right command for this.

Syntax:
cin >> variablename;

Let’s put this into code.

We have slightly modified the code and made the above-mentioned changes and now it is
working according to our requirements.
Great Work Students! You have added another skill to your skillset

Conclusion
Variable Variables are the containers that are used to store different values

Data Type Datatype defines the label according to the type of data that is stored in
the variables.

cin >> variable; It is used to take input from the console.

Skill: Write a complete program that converts input into the required output
Programming Fundamentals
Week 03 - Lab Manual

Task 01(CL): Write a C++ program that inputs from the user his name, roll number,
aggregate, and section and prints it on screen.

Task 02(OP): Write a C++ program that converts the weights from lbs (Pounds) to kgs
(Kilograms). 1lb = 0.45 Kgs
Note: The user enters weights in lbs and the program prints it in kgs.

Task 03(OP): Write a C++ program that takes the length and width of the rectangle from the
user and prints its area. Area = length * width

Task 04(CP): Write a C++ program that takes charge (Q) and time (t) as input from the user
and prints the current (I) on the console. Current(I) = Charge (Q)/Time(t)

Skill: Write a complete program that converts input into the required output
Programming Fundamentals
Week 03 - Lab Manual

Task 05(CP): Write a C++ program that takes the name, matric (out of 1100), intermediate(out
of 550), and Ecat (out of 400) marks of the student and print their aggregate score for UET.
Ecat = 50% & intermediate = 40% & Matric = 10%

Task 06(OP): Write a C++ program that takes the megabytes from the user and converts them
into bits and prints the value on the screen. 1MB = 1024 Kb & 1KB = 1024 Bytes & 1Bytes =
8 Bits

Task 07(OP): You are developing a C++ program for a time-tracking application. The
program needs to take an integer input representing hours and convert it to seconds to
accurately record the time in seconds.

Task 08(OP): You are developing a C++ program for an electrical engineering application.
The program needs to calculate power (in watts) given voltage (in volts) and current (in
amperes) as input.

Skill: Write a complete program that converts input into the required output
Programming Fundamentals
Week 03 - Lab Manual

Task 09(OP): You are building a C++ program for a health and wellness application. To
calculate certain health-related statistics, you need to take a user's age in years as input and
convert it into their age in days.

Notes

● Use 365 days as the length of a year for this challenge.


● Ignore leap years and days between last birthday and now.

Task 10(OP): You are developing a C++ program to keep track of a cricket team's
performance in the Asia Cup tournament. The program needs to take the number of wins,
draws, and losses as input and calculate the number of points the cricket team has obtained so
far, based on the following rules:

● Wins get 3 points.


● Draws get 1 point.
● Losses get 0 points.

Task 11(OP): Scientists have discovered that in four decades, the world will EXPLODE! It
will also take three decades to make a spaceship to travel to a new planet that can hold the

Skill: Write a complete program that converts input into the required output
Programming Fundamentals
Week 03 - Lab Manual

entire world population.

You must calculate the number of people there will be in three decades from now.

Make a variable population and take input from the user that is the world population now.
Assume that every month, someone gives birth to n more people. Also take n from the user as
input. Calculate the number of people there will be when the spaceship is created.

Good Luck and Best Wishes !!


Happy Coding ahead :)

Skill: Write a complete program that converts input into the required output

You might also like