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

Lab Report

The document contains a lab report for a C++ programming assignment. It includes 3 programs: 1) a program to reverse an integer and double its reverse, 2) a program to generate prime numbers between 2-30 using nested loops, and 3) a program to calculate the value of e^x using a Taylor series expansion. For each program, it provides the code, input/output examples, and an explanation of how the code works. It also includes a pre-lab program to calculate test score averages, highest/lowest scores from user input.

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)
53 views

Lab Report

The document contains a lab report for a C++ programming assignment. It includes 3 programs: 1) a program to reverse an integer and double its reverse, 2) a program to generate prime numbers between 2-30 using nested loops, and 3) a program to calculate the value of e^x using a Taylor series expansion. For each program, it provides the code, input/output examples, and an explanation of how the code works. It also includes a pre-lab program to calculate test score averages, highest/lowest scores from user input.

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/ 17

#Lab Report

Lab # 01

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.

PROGRAM :

#include<iostream>
using namespace std;
int main()
{
int num,rev=0,rem;
cout<<"Enter the number :"<<endl;
cin>>num;
for(int i=num;i!=0;i=i/10)
{rem=i%10;
rev =rev*10+rem;
}
cout<<"The reversed number is:"<<rev<<endl;
cout<<"The double of reversed number is:"<<rev*2<<endl;
return 0;
}
OUTPUT :
Explantion :
Working Technique:

The program starts by declaring three variables 'num', 'rev', and 'rem'
of type 'int'. 'num' is used to store the input number, 'rev' is used to
store the reversed number, and 'rem' is used to store the remainder
obtained after dividing the number by 10.
The program then prompts the user to enter a number and waits for
the user to input a value using the 'cin' command.
A 'for' loop is used to reverse the digits of the input number. The loop
runs until 'i' is not equal to zero, where 'i' is initialized to 'num'. In
each iteration, the last digit of 'i' is obtained by using the modulo
operator '%' with the value 10, and the obtained remainder is stored in
'rem'. 'i' is then divided by 10 to get the remaining digits of the
number. The reversed number is obtained by multiplying 'rev' by 10
and adding the value of 'rem' to it.
Once the loop is complete, the program prints the reversed number
using the 'cout' command.
The program then multiplies the reversed number by 2 and prints the
result using the 'cout' command.
The program ends with the 'return 0' statement, which indicates
successful termination of the program.
Commands used in the program:
'#include<iostream>' - This command includes the input/output
stream library, which contains the definitions for 'cin' and 'cout'.
'using namespace std;' - This command declares that the program will
use the 'std' namespace, which contains the definitions for 'cout' and
'cin'.
'int num,rev=0,rem;' - This command declares three variables of type
'int' called 'num', 'rev', and 'rem'. 'num' is used to store the input
number, 'rev' is used to store the reversed number, and 'rem' is used to
store the remainder obtained after dividing the number by 10.
'cout<<"Enter the number :"<<endl;' - This command prints the
message "Enter the number :" on the console.
'cin>>num;' - This command waits for the user to input a value and
stores it in the variable 'num'.
'for(int i=num;i!=0;i=i/10)' - This command initializes a 'for' loop
with 'i' set to 'num'. The loop runs until 'i' is not equal to zero, and in
each iteration, 'i' is divided by 10.
'{rem=i%10;rev =rev*10+rem;}' - This command gets the last digit of
'i' by using the modulo operator '%' with the value 10 and stores it in
'rem'. The reversed number is obtained by multiplying 'rev' by 10 and
adding the value of 'rem' to it.
'cout<<"The reversed number is:"<<rev<<endl;' - This command
prints the reversed number on the console.
'cout<<"The double of reversed number is:"<<rev*2<<endl;' - This
command multiplies the reversed number by 2 and prints the result on
the console.
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

PROGRAM :

#include <iostream>
using namespace std;

int main() {
int n, i, pr;
for(n=2; n<=30; n++) {
pr=0;
for(i=2; i<=n/2; i++) {
if(n%i==0) {
pr=1;
break;
}
}
if(pr==0 && n!=1) { //if the the response (pr=0) and n is
not equal to1 because if
n=1the loop will no work
than print that number.
cout<<n<<""<<endl;
}
}
return 0;
}

OUTPUT :
Lab Report:
 Include iostream library for input/output operations
 Use the standard namespace
 Declare three integer variables n, i, and pr
 Use a for loop to iterate from 2 to 30 for n
 Initialize pr to 0
 Use a nested for loop to iterate from 2 to n/2 for i
 Check if n is divisible by i and set pr to 1 if true
 Break out of the loop if pr is 1
 Check if pr is 0 and n is not equal to 1
 If true, print n
 End the for loop
 Return 0 to end the program
Conclusion:
The program successfully generates prime numbers between 2 to 30
using nested loops. The nested loops technique can be used for larger
ranges of numbers as well.
3. Exponential Function

Exponential function or exp (x) = ex is a mathematical function


learned in calculus. ex 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 e x using
for loop.
PROGRAM :

#include <iostream>
#include<cmath>
using namespace std;

int factorial(int n) {
if (n == 0 || n==1) {
return 1;
} else {
return n * factorial(n-1);
}
}

int main() {
int num_terms;
double x, result = 0;
cout << "Enter the value of x: ";
cin >> x;
cout << "Enter the number of terms to add: ";
cin >> num_terms;

for (int i = 0; i < num_terms; i++) {


result += pow(x,i) / factorial(i);
}
cout << "e^" << x << " = " << result << endl;

return 0;
}

OUTPUT :
Lab Report:
 The program includes the following header files:
 iostream (for input/output operations)
 cmath (for math functions like pow())
 The program defines a function named factorial to calculate
the factorial of a given number.
 The main function:
 declares integer num_terms and double x and result variables
 prompts the user to enter the value of x and number of terms
to add
 reads the values entered by the user using cin
 initializes the result variable to 0
 uses a for loop to calculate the value of each term in the Taylor
series and adds it to the result variable
 prints the final result to the console using cout.
 The program returns 0 to indicate successful execution.
Conclusion:
This program successfully calculates the value of e^x using the
Taylor series expansion. The program prompts the user to enter the
value of x and the number of terms to add in the series, and then uses
a for loop to calculate each term and adds it to the result variable. The
program utilizes the pow() and factorial() functions from the cmath
library to perform the required calculations. Finally, the program
prints the result to the console. Overall, this program provides an
efficient and accurate method for calculating the value of e^x using
Taylor series expansion.
Pre-labs:

3 .Write a program that reads in a group of test scores (positive


integers from 1 to 100) from the keyboard and then calculate and
output the average score as well as the highest and lowest score.
There will be a maximum of 100 scores.

PROGRAM :

#include <iostream>
using namespace std;

int main() {
const int total_size = 100;
int scores[total_size];
int num_scores = 0;
double sum=0;
int highest_score = 0;
int lowest_score = 100;
cout << "Enter up to " << total_size << " test scores (1-100):" <<
endl;
cout << "(Enter -1 to stop):" << endl;
while(num_scores < total_size) {
int score;
cin >> score;
if(score == -1) {
break;
}
if(score < 1 || score > 100) {
cout << "Invalid score. Please enter a score between 1 and
100." << endl;
continue;
}
scores[num_scores] = score;
sum += score;
if(score > highest_score) {
highest_score = score;
}
if(score < lowest_score) {
lowest_score = score;
}
num_scores++;
}
double average_score =sum / num_scores;
cout << "Average score: " << average_score << endl;
cout << "Highest score: " << highest_score << endl;
cout << "Lowest score: " << lowest_score << endl;
return 0;}

OUTPUT :

Lab Report:
 The program includes the header file iostream for input/output
operations
 The program declares a constant integer variable named total_size
to define the maximum size of the array of scores
 The program declares an integer array named scores with a size
equal to total_size
 The program declares an integer variable named num_scores to
keep track of the number of scores entered
 The program declares a double variable named sum to keep track
of the sum of all the scores entered
 The program declares integer variables named highest_score and
lowest_score to keep track of the highest and lowest scores entered
respectively
 The program prompts the user to enter test scores and reads them
into the scores array using cin
 The program includes a while loop to ensure that the number of
scores entered does not exceed the maximum size of the scores
array
 Within the loop, the program checks if the score entered is within
the valid range of 1-100 and adds it to the sum variable
 The program also checks for the highest and lowest scores entered
and updates the corresponding variables accordingly
 The loop continues until the maximum number of scores is entered
or the user enters -1 to stop
 The program calculates the average score by dividing the sum by
the number of scores entered and stores it in a double variable
named average_score
 Finally, the program prints the average, highest and lowest scores
to the console using cout.
Conclusion: This program is an efficient and accurate way to
calculate the average, highest and lowest scores from a list of test
scores. It uses an array to store the scores entered and checks if each
score is within the valid range of 1-100. It also uses a loop to ensure
that the maximum number of scores is not exceeded and calculates the
sum of all the scores entered. Additionally, the program keeps track of
the highest and lowest scores entered and calculates the average score.
The final output provides valuable information about the performance
of students in the test.
Lab Exercise:

Develop a C++ program to sort the elements of any array using


bubble sort algorithm.

PROGRAM :

#include <iostream>
using namespace std;
int main()
{ int array[5], i, j, swap;
for (i = 0; i < 5; i++)
{ cout << "Enter value for array: ";
cin >> array[i];
}
cout << "The original values of array are: " << endl;
for (i = 0; i < 5; i++)
{ cout << array[i] << " ";
}
for (i = 0; i < 5; i++)
{
for (j = 0; j < 4; j++)
{
if (array[j] > array[j + 1])
{
swap = array[j];
array[j] = array[j + 1];
array[j + 1] = swap;
}
}
}
cout << "\nThe bubble sorted array is: " << endl;
for (i = 0; i < 5; i++)
{
cout << array[i] << " ";
}
return 0;
}

OUTPUT :

Lab Report:
 Include the necessary header files.
 #include <iostream>

 Define the main function.
 int main()

 Declare an integer array of size 5 and declare other necessary
variables.
 int array[5], i, j, swap;

 Use a for loop to take input values for the array.
 for (i = 0; i < 5; i++)
 {
 cout << "Enter value for array: ";
 cin >> array[i];
 }

 Display the original values of the array using a for loop.
 cout << "The original values of array are: " << endl;
 for (i = 0; i < 5; i++)
 {
 cout << array[i] << " ";
 }

 Use nested for loops to perform the bubble sort algorithm.
 for (i = 0; i < 5; i++)
 {
 for (j = 0; j < 4; j++)
 {
 if (array[j] > array[j + 1])
 {
 swap = array[j];
 array[j] = array[j + 1];
 array[j + 1] = swap;
 }
 }
 }

 Display the sorted array using a for loop.
 cout << "\nThe bubble sorted array is: " << endl;
 for (i = 0; i < 5; i++)
 {
 cout << array[i] << " ";
 }

 Return 0 to indicate the program has completed successfully.
 return 0;
Conclusion:In this program, we used the bubble sort algorithm to sort
an array of integers in ascending order. The program takes user input
for the array, displays the original values of the array, performs the
bubble sort algorithm, and displays the sorted array. The program
successfully sorts the array in ascending order using the bubble sort
algorithm.

Develop a C++ program to sort the elements of any array using


insertion sort algorithm.

PROGRAM :

#include<iostream>
using namespace std;
int main()
{
int array[5],i,j,swap, min; // declare variable min
for (i=0;i<5;i++)
{
cout<<"Enter Value for Array :"<<endl;
cin>>array[i];
}
cout<<"The original value in array is :";
for(i=0;i<4;i++)
{ cout<<array[i];
min=i; // initialize min to i
for(j=i+1;j<5;j++)
{
if(array[j]<array[min])
{
min=j;
}
}
if(min!=i)
{
swap=array[i];
array[i]=array[min];
array[min]=swap;
}
}
cout<<"\n The insertion sorted array is :"<<"\n";
for(i=0;i<5;i++)
{
cout<<array[i]<<" ";
}
return 0;
}

OUTPUT :
Lab Report:
 The program starts by including the necessary header files and
declaring the main function.
 It declares an integer array named 'array' of size 5 and integer
variables 'i', 'j', 'swap', and 'min'.
 It takes input from the user for the array using a for loop and stores
it in the array.
 It then displays the original array using another for loop.
 It then enters a nested for loop where it initializes the variable
'min' to the current value of 'i'.
 It then compares the value of array[j] with array[min], where
j=i+1, and if array[j] is smaller than array[min], it assigns j to min.
 It then checks if min is not equal to i, it swaps the values of
array[i] and array[min].
 Finally, it displays the sorted array using a for loop.
Conclusion:
This program implements the insertion sort algorithm to sort an array
of integers. The insertion sort algorithm is a simple and efficient
algorithm for sorting small arrays. The algorithm works by iteratively
comparing each element in the array with the elements before it and
placing it in its correct position in the sorted array.

You might also like