You are on page 1of 52

All Lab Tasks

Submitted to:
Ahsan Maqsood
Submitted by:
Uns Yaseen
Reg#:
FA22-BSE-046
Course:
PF LAB
DATE:
19-04-2023

LAB 1 AND LAB 2


PYTHON:
PROGRAM 1:
for i in range(1,11):
for j in range (1,11):
print(i*j,end="")
print("\n")
SOL:
[Running] python -u "e:\table.py"
12345678910

2468101214161820

36912151821242730

481216202428323640

5101520253035404550

6121824303642485460

7142128354249566370

8162432404856647280

9182736455463728190

102030405060708090100

[Done] exited with code=0 in 0.122 seconds


PROGRAM 2:
print("HELLO TO THE WORLD OF PYTHON")
SOL:
[Running] python -u "e:\table.py"
HELLO TO THE WORLD OF PYTHON

[Done] exited with code=0 in 0.106 seconds


C++
PROGRAM 1:
#include<iostream>
using namespace std;
int main()
{
cout<<"hello to the world of c++ \n";
return 0;
}
SOL:
Hello to the world of c++

--------------------------------
Process exited after 0.01862 seconds with return value 0
Press any key to continue . . .
PROGRAM 2:
#include<iostream>
using namespace std;
int main(){
int first_number,second_number,sum;
cout<<"Enter the two integers:";
cin>>first_number>>second_number;
sum=first_number+second_number;
cout<<first_number<<"+"<<second_number<<"="<<sum;
return 0;
}
SOL:
Enter the two integers:34
34
34+34=68
--------------------------------
Process exited after 3.242 seconds with return value 0
Press any key to continue . . .
PROGRAM 3:
#include<iostream>
using namespace std;
int main(){
string Name= "Bilal Akram";
cout<<"Name"<<"="<<Name<<endl;
string Registration_NO="FA22-BSE-027";
cout<<"Registration_NO"<<":"<<Registration_NO<<endl;
float CGPA=3.2;
cout<<"CGPA"<<"="<<CGPA<<endl;
return 0;
}
SOL:
Name=Bilal Akram
Registration_NO:FA22-BSE-027
CGPA=3.2

--------------------------------
Process exited after 0.02146 seconds with return value 0
Press any key to continue . . .
PROGRAM 4:
#include<iostream>
using namespace std;
int main(){
cout<<"*"<<endl;
cout<<"**"<<endl;
cout<<"***"<<endl;
cout<<"****"<<endl;
cout<<"*****"<<endl;
return 0;
}
SOL:
*
**
***
****
*****

--------------------------------
Process exited after 0.06663 seconds with return value 0
Press any key to continue
PROGRAM 5:
#include<iostream>
using namespace std;
int main(){
cout<<" *"<<endl;
cout<<" **"<<endl;
cout<<" ***"<<endl;
cout<<" ****"<<endl;
cout<<"*****"<<endl;
return 0;
}
SOL:
*
**
***
****
*****

--------------------------------
Process exited after 0.0184 seconds with return value 0
Press any key to continue
PROGRAM 6:
#include<iostream>
using namespace std;
int main(){
float pi=3.14,r,Area;
cout<<"enter the value of r:";
cin>>r;
Area=pi*r*r;
cout<<pi<<"*"<<r<<"*"<<r<<"="<<Area;
return 0;
}
SOL:
enter the value of r:8
3.14*8*8=200.96
--------------------------------
Process exited after 9.647 seconds with return value 0
Press any key to continue . . .
PROGRAM 7:
#include<iostream>
using namespace std;
int main(){
float pi=3.14,r,Area;
cout<<"enter the value of r:";
cin>>r;
Area=4.0*pi*r*r;
cout<<4<<"*"<<pi<<"*"<<r<<"*"<<r<<"="<<Area;
return 0;
}
SOL:
enter the value of r:3
4*3.14*3*3=113.04
--------------------------------
Process exited after 1.694 seconds with return value 0
Press any key to continue . . .
PROGRAM 8:
#include<iostream>
using namespace std;
int main(){
float pi=3.14,r,volume;
cout<<"enter the value of r:";
cin>>r;
volume=(4.0 / 3.0) * 3.14 * r * r * r;
cout<<volume;
return 0;
}
SOL:
enter the value of r:3
113.04
--------------------------------
Process exited after 1.741 seconds with return value 0
Press any key to continue . . .
PROBLEM 9:
#include<iostream>
using namespace std;
int main(){
float C,F;
cout<<"enter the Temperature in Fahranheit:";
cin>>F;
C=(F-32.0)/1.8;
cout<<"C"<<"="<<C<<" DEGREE CELCIUS";
return 0;

}
SOL:
enter the Temperature in Fahranheit:110
C=43.3333 DEGREE CELCIUS
--------------------------------
Process exited after 5.393 seconds with return value 0
Press any key to continue . . .
ASSIGNMENT NO 2
Q#1:
Write a C++ program that makes a right triangle borders with * using nested
loops.
SOL:

#include <iostream>
using namespace std;
int main() {
int height;
cout << "Enter the height of the triangle: ";
cin >> height;
for (int i = 1; i <= height; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == height) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}

OUTPUT:
Enter the height of the triangle: 10
*
**
* *
* *
* *
* *
* *
* *
* *
**********
Q#2:
Write a C++ program that makes a right triangle borders with * and fill the rest
with (.) using nested loops.
SOL:
#include <iostream>
using namespace std;
int main() {
int size;
cout << "Enter the size of the triangle: ";
cin >> size;
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= i; j++) {
if (j == 1 || j == i || i == size) {
cout << "* ";
} else {
cout << ". ";
}
}
cout << endl;
}
return 0;
}
OUTPUT:
Enter the size of the triangle: 9

*
**
* . .*
*...*
*.....*
*.......*
*........ *
*..........*
**********
ASSIGNMENT NO 3:
Q#1:
Write a function that prints sum of three digits passed to a function through
main.
SOL:
#include <iostream>
using namespace std;
int main() {
int num1, num2, num3;
cout << "Enter three numbers: ";
cin >> num1 >> num2 >> num3;
int sum = num1 + num2 + num3;
cout << "The sum of " << num1 << ", " << num2 << ", and " << num3 << " is
" << sum << endl;
return 0;
}
OUTPUT:
Enter three numbers: 12
13
14
The sum of 12, 13, and 14 is 39

Q#2:
Write a program using function to find out the factorial of a given number.
SOL:
#include <iostream>
using namespace std;
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n-1);
}
}
int main() {
int num;
cout << "Enter a positive integer: ";
cin >> num;
if (num < 0) {
cout << "Error: input is not a positive integer" << endl;
return 1;
}
int result = factorial(num);
cout << num << "! = " << result << endl;
return 0;
}
OUTPUT:
Enter a positive integer: 27
27! = 1484783616
Q#3:
Write a function that prints the square of first 10 digits and return to main.
SOL:
#include <iostream>
using namespace std;
int square(int num) {
return num * num;
}

int main() {
for (int i = 1; i <= 10; i++) {
cout << "The square of " << i << " is: " << square(i) << endl;
}
return 0;
}

OUTPUT:
The square of 1 is: 1
The square of 2 is: 4
The square of 3 is: 9
The square of 4 is: 16
The square of 5 is: 25
The square of 6 is: 36
The square of 7 is: 49
The square of 8 is: 64
The square of 9 is: 81
The square of 10 is: 100
LAB 3
EXERCISE 1:
QUESTION 1:
Convert the English expression "weight is greater than 100" to a C++
expression
PROGRAM # 1
#include<iostream>
using namespace std;
int main(){
int weight=100;
if(weight>=100){
cout<<"weight>"<<weight;}
else{
cout<<"negative input";}
}
OUTPUT:
weight>100
--------------------------------
Process exited after 0.03098 seconds with return value 0
Press any key to continue . . .
QUESTION 2:
Convert the English expression "a is not equal to 0" to a C++ expression.
PROGRAM # 2
#include<iostream>
using namespace std;
int main(){
int a=1;
if(a=1){
cout<<"a is not equal to 0";
}
else{
cout<<"value of a is 0";}
}
OUTPUT:
a is not equal to 0
--------------------------------
Process exited after 0.02025 seconds with return value 0
Press any key to continue . . .
QUESTION 3:
Convert the English expression "apple and orange are not the same" to a C++
expression.
PROGRAM # 3
#include<iostream>
using namespace std;
int main(){
int a=1;
if(a=1){
cout<<"apple and oranges are not the same";
}else{
cout<<"oh my god they are the same";
}
}
OUTPUT:
apple and oranges are not the same
--------------------------------
Process exited after 0.01891 seconds with return value 0
Press any key to continue . . .
QUESTION 4:
Convert the English expression "x is equal to 0" to a C++ expression.
PROGRAM # 4
#include<iostream>
using namespace std;
int main(){
int a=0;
if(a=0){
cout<<"x is equal to 0";
}else{
cout<<"x is not 0";
}
}
OUTPUT:
x is not 0
--------------------------------
Process exited after 0.021 seconds with return value 0
Press any key to continue . . .
EXERCISE 2:
The statements between the {and the} are called a block of statements and if
the logical expression is true, every statement in the block will be executed. If
the expression is false, then none of the statements will be performed. An
example of this form follows, If the hours worked were greater than 40, lines 4,
5, and 6 would be performed.
PROGRAM:
#include<iostream>
using namespace std;
int main(){
float pay,rate,hours,overtime,total_pay;
hours=45;
rate=15;
if(hours>40){
overtime=(hours-40)*0.5*rate;
cout<<"overtime is "<<overtime<<endl;
pay=hours*rate;
}
else{
cout<<"no overtime";
}
cout<<"pay is "<<pay<<endl;}
OUTPUT:
overtime is 37.5
pay is 675

--------------------------------
Process exited after 0.01927 seconds with return value 0
Press any key to continue . . .
EXERCISE 5:
Write a program which reads two integer values. If the first is lesser print the
message up. If the second is lesser, print the message down if they are equal,
print the message equal; if there is an error in reading the data, print a
message containing the word Error.
PROGRAM:
#include<iostream>
using namespace std;
int main(){
int a,b;
cout<<"enter the values of two integers";
cin>>a;
cin>>b;
if(a<b){
cout<<"up"<<endl;
}
if(a>b){
cout<<"down"<<endl;
}

if(a=b){
cout<<"equal"<<endl;
}
}
OUTPUT:
enter the values of two integers = 3
3
equal
--------------------------------
Process exited after 3.833 seconds with return value 0
Press any key to continue . . .
LAB PROGRAM
#1
#include<iostream>
using namespace std;
int main(){
for(int i=1;i<5;++i){
for(int j=1;j<10;++j)
cout<<j<<endl;
}
}
OUTPUT:
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
--------------------------------
Process exited after 0.03261 seconds with return value 0
Press any key to continue . . .
#2
PROGRAM:
#include<iostream>
using namespace std;
int main (){
int radius=10,area;
cout<<"enter the value of r = ";
cin>>radius;
if (radius>=10){
area=radius*radius*3.1416;
cout<<"the area of circle is "<<area;}
else {

cout<<"negative input";
}}
OUTPUT:
enter the value of r = 32
the area of circle is 3216
--------------------------------
Process exited after 2.609 seconds with return value 0
Press any key to continue . . .
#3
PROGRAM:
#include<iostream>
using namespace std;
int main(){
for (int i=1;i<=10;i++){
cout<<i<<"\n\t";
}
}
OUTPUT:
1
2
3
4
5
6
7
8
9
10

--------------------------------
Process exited after 0.0279 seconds with return value 0
Press any key to continue . . .
#4
PROGRAM:
#include<iostream>
using namespace std;
int main(){
for(int i=1;i<=50;++i){
for(int j=1;j<=10;++j){

cout<<""<<i*j<<"";}
{
cout<<endl;
}
}}
OUTPUT:
12345678910
2468101214161820
36912151821242730
481216202428323640
5101520253035404550
6121824303642485460
7142128354249566370
8162432404856647280
9182736455463728190
102030405060708090100
112233445566778899110
1224364860728496108120
13263952657891104117130
14284256708498112126140
153045607590105120135150
163248648096112128144160
1734516885102119136153170
1836547290108126144162180
1938577695114133152171190
20406080100120140160180200
21426384105126147168189210
22446688110132154176198220
23466992115138161184207230
24487296120144168192216240
255075100125150175200225250
265278104130156182208234260
275481108135162189216243270
285684112140168196224252280
295887116145174203232261290
306090120150180210240270300
316293124155186217248279310
326496128160192224256288320
336699132165198231264297330
3468102136170204238272306340
3570105140175210245280315350
3672108144180216252288324360
3774111148185222259296333370
3876114152190228266304342380
3978117156195234273312351390
4080120160200240280320360400
4182123164205246287328369410
4284126168210252294336378420
4386129172215258301344387430
4488132176220264308352396440
4590135180225270315360405450
4692138184230276322368414460
4794141188235282329376423470
4896144192240288336384432480
4998147196245294343392441490
50100150200250300350400450500

--------------------------------
Process exited after 0.1038 seconds with return value 0
Press any key to continue
LAB 8:

Exercise 1:
Write a program in C++ named calculator which can perform +, -, / and *.
Make sure that you have created a separate user defined function for each
operation.

#include<iostream>
using namespace std;
int main(){
char op;
float a,b;
cout << " enter the values of a and b : ";
cin>>a>>b;
cout << " enter the operator = ";
cin>> op;
switch(op){
case '+':
cout << a+b ;
break;
case '-':
cout << a-b ;
break;
case '*':
cout << a*b ;
break;
case '/':
cout << a/b ;
break;
default:
cout << " invalid op ";
} return 0;
}
OUTPUT:
enter the values of a and b : 23
32
enter the operator = +
55
Exercise 2:
Write a function which can determine the grade of subject on the basis of
its input argument.

#include<iostream>
using namespace std;
int main () {
int marks;
cout << " Enter the marks = ";
cin >> marks;
if(marks >= 90) {
cout << 'A';
}
else if(marks >= 80 && marks < 90) {
cout << 'B';
}
else if(marks >= 70 && marks < 80) {
cout << 'C';
}
else if(marks >= 60 && marks < 70) {
cout << 'D';
}
else {
cout << 'F';
}
return 0;
}

OUTPUT:
Enter the marks = 99
A
Question no 1:
Program 1:
#include<iostream>
using namespace std;
int main(){
int area,perimeter,length,breadth;
cout<<"Bilal Akram"<<endl<<"FA22-BSE-027"<<endl;
cin>>length;
cin>>breadth;
area=length*breadth;
cout<<"area ="<<area<<endl;
perimeter=2*(length+breadth);
cout<<"perimeter ="<<perimeter<<endl;
return 0;
}
Output:
Uns Yaseen
FA22-BSE-046
27
37
area =999
perimeter =128

Question no 2:
Program 2:
#include <iostream>
using namespace std;
int main() {
int i, j;
for (i = 1; i <= 27; i++) {
for (j = 1; j <= 37; j++) {
cout << j << " ";
}
cout << endl;
}
return 0;
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37
Question no 3:
Program 3:
#include<iostream>
using namespace std;
int main(){
double km,miles,distance_in_km;
cout<<"enter the distance in km ="<<endl;
cin>>distance_in_km;
miles=distance_in_km*1.6;
cout<<"miles ="<<miles<<endl;
return 0;
}
Output:
enter the distance in km =
27
miles =43.2
Question no 4:
Program 4:
#include<iostream>
using namespace std;
int main(){
int a,b;
cin>>a;
cin>>b;
cout<<(a&b)<<endl;
cout<<(a|b)<<endl;
return 0;
}
Output:
27
37
1
63
LAB Task 09
Question No.01
Write a program using this function (using a similar layout as
you did in the factorial program earlier), run it and consider its behavior.
Test various calls to this function and watch closely, use the debugger to step
through so you can visualize the changes. Write all your results and
observations in your lab report.
Solution:
#include <iostream>
Using namespace std;

Int sum(int a, int b) {


Int result = a + b;
Return result;
}
Int main() {
Int num1, num2, result;

Cout << “Enter two numbers: “;


Cin >> num1 >> num2;

Result = sum(num1, num2);


Cout << “Sum: “ << result << endl;

Return 0;
}

The given code demonstrates a simple program that exchanges the values of
two variables using reference parameters. Here’s a breakdown of the code’s
behavior:

1. The program defines a function called `swap` that takes two integer
references as parameters. This function is responsible for swapping the values
of the variables.
2. In the `main` function, two integer variables `a` and `b` are initialized with
the values 5 and 9, respectively.
3. The program then displays a message to indicate its purpose and prints the
initial values of `a` and `b`.
4. The `swap` function is called, passing `a` and `b` as arguments. This will
exchange their values.
5. After the swap function returns, the program displays the final values of `a`
and `b`, showing that the values have been successfully exchanged.
Show what would be written by the following calls to the recursive function
Puzzle
Cout << Puzzle(20,5);
Cout << Puzzle(3,6);
Cout << Puzzle(8,8);

To modify the program to use pointers instead of reference parameters, you


can make the following changes:

```cpp
#include <iostream>

Using namespace std;

Void swap(int* x, int* y);

Int main()
{
Int a = 5;
Int b = 9;

Cout << “This program exchanges 2 values using pointers.” << endl;
Cout << “Values before the exchange:” << endl;
Cout << “a= “ << a << “ b= “ << b << endl;

Swap(&a, &b); // Pass the addresses of ‘a’ and ‘b’ to the swap function
Cout << “Values after the exchange:” << endl;
Cout << “a= “ << a << “ b= “ << b << endl;
}

// function for swapping values using pointers


Void swap(int* x, int* y)
{
Int temp;

Temp = *x;
*x = *y;
*y = temp;
}
```

In the modified code, the `swap` function now takes two integer pointers as
parameters. Inside the `swap` function, the values are swapped using pointers
by dereferencing them with the `*` operator.

In the `main` function, when calling the `swap` function, the addresses of `a`
and `b` are passed using the `&` operator.

The rest of the program remains the same, and the output should be identical
to the previous version, demonstrating that the values of `a` and `b` are
successfully exchanged using pointers.

To determine the output of the recursive function `Puzzle` for the given
function calls, we need to know the implementation of the `Puzzle` function.
Since the implementation is not provided, I will assume a simple
implementation where `Puzzle` returns the sum of its two parameters. Here’s
an example:

```cpp
#include <iostream>
Using namespace std;

Int Puzzle(int a, int b) {


If (a == 0 || b == 0) {
Return 0;
} else {
Return a + b;
}
}

Int main() {
Cout << Puzzle(20, 5) << endl;
Cout << Puzzle(3, 6) << endl;
Cout << Puzzle(8, 8) << endl;
Return 0;
}
```

In this implementation, if either `a` or `b` is 0, the function returns 0.


Otherwise, it returns the sum of `a` and `b`. Let’s analyze the outputs for the
given function calls:
1. `cout << Puzzle(20, 5);`
- The function is called with `a = 20` and `b = 5`.
- Since both `a` and `b` are non-zero, the function returns their sum: `20 + 5 =
25`.
- The output will be `25`.

2. `cout << Puzzle(3, 6);`


- The function is called with `a = 3` and `b = 6`.
- Since both `a` and `b` are non-zero, the function returns their sum: `3 + 6 =
9`.
- The output will be `9`.

3. `cout << Puzzle(8, 8);`


- The function is called with `a = 8` and `b = 8`.
- Since both `a` and `b` are non-zero, the function returns their sum: `8 + 8 =
16`.
- The output will be `16`.
Output:
```
25
9
16
```
LAB TASK 10
Question No.01
Write a program to sort an array of 10 integers. Input and sorting must be done
using loop.
Solution:
#include <iostream>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]{
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;}
}}}
int main() {
int array[10];
std::cout << "Enter 10 integers: ";
for (int i = 0; i < 10; i++) {
std::cin >> array[i];
}
bubbleSort(array, 10);
std::cout << "Sorted array: ";
for (int i = 0; i < 10; i++) {
std::cout << array[i] << " ";
}
std::cout << std::endl;
return 0;
}
Output:
Enter 10 integers: 76
556
56
34
45
45
65
78
98
23
Sorted array: 23 34 45 45 56 65 76 78 98 556

Question No.01:
Write a C Program that does the following: 1. Declares a C-
String called ‘m1’ and initializes it with text “Programming is great fun!”. 2.
Uses C-function puts() to print this string. 3. Asks the user to enter a String
named ‘m2’ (Hint: Use gets() function for this.) 4. Concatenates the two strings
and stores the result in ‘m3’. For example if the user enters m2 as “Not
Really!”, m3 should be “Programming is great fun! Not really!” 5. Inserts the
user entered array (m2) into m1 after “Programming is ...” For the above
example, the resultant String would become “Programming is Not really! great
fun!”
Solution:
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
int main() {
char m1[MAX_LENGTH] = "Programming is great fun!";
char m2[MAX_LENGTH];
puts(m1);
printf("Enter a string: ");
gets(m2);
char m3[MAX_LENGTH];
strcpy(m3, m1);
strcat(m3, " ");
strcat(m3, m2);
char *pos = strstr(m1, "great fun!");
if (pos != NULL) {
memmove(pos + strlen(m2), pos, strlen(pos) + 1);

strncpy(pos, m2, strlen(m2));


}

printf("Concatenated string: %s\n", m3);


printf("Resultant string: %s\n", m1);

return 0;
}

Question No.02:
Write a program that converts a string like "124" to an integer 124.
Solution:
#include <stdio.h>
int stringToInt(const char* str) {
int result = 0;
int sign = 1;
int i = 0;
if (str[0] == '-') {
sign = -1;
i = 1;
}
while (str[i] != '\0') {
int digit = str[i] - '0';
result = result * 10 + digit;
i++;
}

return result * sign;


}

int main() {
const char* str = "124";
int number = stringToInt(str);

printf("String: %s\n", str);


printf("Integer: %d\n", number);
return 0;
}
Question No.03:
Write a program that replaces two or more consecutive blanks in a
string by a single blank. For example, if the input is
“Grim return to the planet of apes!!”
the output should be
“Grim return to the planet of apes!!”

Solution:
#include <stdio.h>
#include <string.h>

#define MAX_LENGTH 100

void removeExtraBlanks(char* str) {


int i, j;
int length = strlen(str);
int foundBlank = 0;

for (i = 0, j = 0; i < length; i++) {


if (str[i] == ' ') {
if (!foundBlank) {
str[j++] = str[i];
foundBlank = 1;
}
} else {
str[j++] = str[i];
foundBlank = 0;
}
}

str[j] = '\0';
}

int main() {
char str[MAX_LENGTH] = "Grim return to the planet of apes!!";

printf("Original string: %s\n", str);

removeExtraBlanks(str);

printf("Modified string: %s\n", str);

return 0;
}

LAB 12
Task 1: Write a C Program that does the following:
1. Declares a C-Structure called ‘car’.
2. It should contain the following elements
a) A char array of size 20 to hold the make of the car e.g. Suzuki.
b) A char array of size 20 to hold the model of the car e.g. Alto
c) An integer capacity to store the seating capacity.
d) A floating-point number to hold the weight of the empty vehicle.
3. Then declare 3 variables of this type of structure.
4. Let the user populate the elements of these variables.
5. Print these structures
Solution:
#include <iostream>
#include <string>
using namespace std;
struct Car {
string make;
string model;
int capacity;
float weight;
};
int main() {
Car car1, car2, car3;
cout << "Enter details for Car 1:" << endl;
cout << "Make: ";
getline(cin, car1.make);
cout << "Model: ";
getline(cin, car1.model);
cout << "Capacity: ";
cin >> car1.capacity;
cout << "Weight: ";
cin >> car1.weight;
cin.ignore(); // Ignore the newline character
cout << "Enter details for Car 2:" << endl;
cout << "Make: ";
getline(cin, car2.make);
cout << "Model: ";
getline(cin, car2.model);
cout << "Capacity: ";
cin >> car2.capacity;
cout << "Weight: ";
cin >> car2.weight;
cin.ignore(); // Ignore the newline character
cout << "Enter details for Car 3:" << endl;
cout << "Make: ";
getline(cin, car3.make);
cout << "Model: ";
getline(cin, car3.model);
cout << "Capacity: ";
cin >> car3.capacity;
cout << "Weight: ";
cin >> car3.weight;
cin.ignore(); // Ignore the newline character
cout << "Car 1:" << endl;
cout << "Make: " << car1.make << endl;
cout << "Model: " << car1.model << endl;
cout << "Capacity: " << car1.capacity << endl;
cout << "Weight: " << car1.weight << endl;
cout << "Car 2:" << endl;
cout << "Make: " << car2.make << endl;
cout << "Model: " << car2.model << endl;
cout << "Capacity: " << car2.capacity << endl;
cout << "Weight: " << car2.weight << endl;
cout << "Car 3:" << endl;
cout << "Make: " << car3.make << endl;
cout << "Model: " << car3.model << endl;
cout << "Capacity: " << car3.capacity << endl;
cout << "Weight: " << car3.weight << endl;
return 0;
}
Task 2 a: Write a C program that declares an array of structures of type ‘car’
from previous example and asks the user to fill in the elements. Once all the
structures have been initialized, the program should present a menu to the
user so that he can print a certain structure or modify its elements.
Solution:

Task 2 b: There is a structure called employee that holds information like


employee code, name, date of joining. Write a program to create an array of
the structure and enter some data into it. Then ask the user to enter current
date. Display the names of those employees whose tenure is 3 or more than 3
years according to the given current date.
Solution:
#include <iostream>
#include <string>
#define MAX_CARS 3
struct Car {
string make;
string model;
int capacity;
float weight;
};
void printCar(const Car& car);
void modifyCar(Car& car);
int main() {
Car cars[MAX_CARS];
for (int i = 0; i < MAX_CARS; i++) {
cout << "Enter details for Car " << i + 1 << ":" << endl;
cout << "Make: ";
getline(cin, cars[i].make);
cout << "Model: ";
getline(cin, cars[i].model);
cout << "Capacity: ";
cin >> cars[i].capacity;
cout << "Weight: ";
cin >> cars[i].weight;
cin.ignore();
cout << endl;
}
int choice;
int carIndex;
do {
cout << "Menu:" << endl;
cout << "1. Print car details" << endl;
cout << "2. Modify car details" << endl;
cout << "3. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
cout << endl;
switch (choice) {
case 1:
cout << "Enter the car number (1-" << MAX_CARS << "): ";
cin >> carIndex;
cin.ignore(); r
if (carIndex < 1 || carIndex > MAX_CARS) {
cout << "Invalid car number!" << endl;
} else {
printCar(cars[carIndex - 1]);
}
break;
case 2:
cout << "Enter the car number (1-" << MAX_CARS << "): ";
cin >> carIndex;
cin.ignore(); // Consume the newline character
if (carIndex < 1 || carIndex > MAX_CARS) {
cout << "Invalid car number!" << endl;
} else {
modifyCar(cars[carIndex - 1]);
}
break;
case 3:
cout << "Exiting program..." << endl;
break;
default:
cout << "Invalid choice! Please try again." << endl;
break;
}
cout << endl;
} while (choice != 3);
return 0;
}
void printCar(const Car& car) {
cout << "Make: " << car.make << endl;
cout << "Model: " << car.model << endl;
cout << "Capacity: " << car.capacity << endl;
cout << "Weight: " << car.weight << endl;
}
void modifyCar(Car& car) {
cout << "Enter new make: ";
getline(cin, car.make);
cout << "Enter new model: ";
getline(cin, car.model);
}

You might also like