0% found this document useful (0 votes)
33 views

Lab 01

This document provides instructions for a programming lab assignment on control structures like while and for loops. The objectives are to explain loop basics, apply loop syntax, design programs using loops, and solve repetitive problems with loops. Examples of problems include reversing integers, finding prime numbers, and calculating the exponential function. Students are asked to write code to solve these three problems using loops, prompting the user for input and performing the calculations. The document also reviews some basic programming concepts like data types, Boolean values, bytes, and integer types.

Uploaded by

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

Lab 01

This document provides instructions for a programming lab assignment on control structures like while and for loops. The objectives are to explain loop basics, apply loop syntax, design programs using loops, and solve repetitive problems with loops. Examples of problems include reversing integers, finding prime numbers, and calculating the exponential function. Students are asked to write code to solve these three problems using loops, prompting the user for input and performing the calculations. The document also reviews some basic programming concepts like data types, Boolean values, bytes, and integer types.

Uploaded by

MUHAMMAD FAROOQ
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming Fundamentals Lab Lab 01

Name __________________________ Roll no. ______________________

Control Structures: while / for Loop: Reverse integer, prime number & exp(x)

Lab Objectives:

After performing this lab, the students should be able to


explain the basics of for loops and while loops
apply the syntaxes of loop structures
design programs using loop structures
solve (hard) problems of repetitive nature using loop structures
Background:
Loop structures called for loops and while loops are covered as some basic control structures. The loop can be used
to do repetitive calculations. Though human beings can do calculation or processing or repetitive nature by hand, it
is much slower and tedious for the human being. Good and solid logic of loop structures can be used to solve such
problems in a very efficient way.
For example, in order to compute the sum of 1 + 2 + 3 + … + 100, we can do easily using the following code
snippet:
int sum = 0;
for (int j = 1; j <= 100; j++)
sum += j;
There are two control structures used often: the for loop and the while loop (and do while as a different way of
while).The syntaxes of these two loop structures are as follows:
for (initialize loop variables; loop terminator; loop variable update)
{
statements in the block or one statement
}
Not all three components of the for loop are necessary, in fact for ( ; ; ) is a valid statement. The loop variable update
can be increments of loop variable(s) or decrements; there can be more than one loop variable in the same for loop
separated by comma (,) and there can also be nested for loops of for loop inside for loop etc. When there is only one
statement in the block, the enclosing braces {} can be omitted. The following are a few valid for loop statements.
The loop structure can be used to do the following problems encountered often in the first programming course
(usually considered challenging for the students new to programming):
 Reverse an integer
 Compute the prime number
 Compute a function such as exp (x) (exponential function).
Lab Assignments:
1. Reverse an integer

Problem statement: Given an integer n such as 1367. Write a (C++) program that computes the inverse of this
integer as 7631 and also the double of the inverse 15262. Also, when the input changes to a different number like
2467, it should compute the inverse 7642 and twice that as 15284; and when the input changes to 5 digits number
12345, it computes 54321 and 108642.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________

1
Programming Fundamentals Lab Lab 01
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________

2. Compute prime number

A prime number p > 1 is a positive integer which has 1 and itself as the only factors. Prime numbers have special
properties that are different from non-prime (also called composite) numbers > 1. For example p |ab => p | a or p | b
(this property is not true for nonprime numbers, for example 4 | 2 * 2 but 4 does not divide 2 since 4 is not a prime
number). The list of prime numbers from 2 to 30 is: 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29. To determine if an integer
n > 1 is a prime number, theoretically we need to verify that for i = 2, 3, 4, 5, .., up to n -2, none of them divides n.
Problem statement:
Write down a C++ code for generating prime number between 0 and 30
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________

3. Exponential Function

Exponential function or exp (x) = ex is a mathematical function learned in calculus. e x can be represented as a
power series ex = 1 + x + x2 / 2! + x3 / 3! + … + xn / n! + …
Problem statement:
Write down a C++ code for calculating e x using series expansion. Prompt the user to specify the number of terms of
series that it wants to add.
Hint: It is possible to write a computer program to compute ex using for loop.
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________

2
Programming Fundamentals Lab Lab 01
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_________________________________________________________________________________________

Revision of Basic Ideas


 What is the final value (in C++) of the following expression?
(5 - 16 / 2 * 3 + (3 + 2 / 2) - 5)________________________________
 How would the following expression be written in C++? 2x + 34
______________________________________________________________________________________
__________________________________________________
 _________________________________________data types only have two values: true and false.
 One byte consists of ____________________________bits.
 _________________________________is a data type that only holds numbers with no fractional
component.

You might also like