Lab 01
Lab 01
Control Structures: while / for Loop: Reverse integer, prime number & exp(x)
Lab Objectives:
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
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
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
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_____________________________________________________________________________________________
_________________________________________________________________________________________