★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
Lab manual programing
1. Lab Manual Programming Fundamental
2
hh
Lab Manual
Programming Fundamental
Student Name ESHMAL IMAN KHAN
Student Roll # 20636
Department COMPUTER SCIENCE
Batch / Year 2020
3. Lab Manual Programming Fundamental
4
Contents
INTRODUCTION..........................................................................................................................9
Lab Manual # 1 ...........................................................................................................................11
Basic...............................................................................................................................................11
1.1 A typical C++ environment................................................................................................. 12
1.2 Program No 1...................................................................................................................... 13
1.2.1 Out Put of Program No 1 ............................................................................................. 13
1.3 Declare Variables................................................................................................................ 13
1.4 Program No 2 Example Of Assignment Operator .............................................................. 13
1.4.1 Out Put of Program No 2 ............................................................................................. 14
1.5 Arithmetic Operators........................................................................................................... 14
1.5.1 Precedence ................................................................................................................... 14
1.6 Program No 3...................................................................................................................... 15
1.6.1 Code of Program No 3 ................................................................................................. 15
1.7 Program No 4...................................................................................................................... 16
1.7.1 Code of Program No 4 ................................................................................................. 16
1.8 Program No 5...................................................................................................................... 16
1.8.2 Code of Program No 5 ................................................................................................. 17
1.8.3 Out Put of Program No 5 ............................................................................................. 17
1.9 Program No 6...................................................................................................................... 18
Lab Manual # 2 ...........................................................................................................................19
if / if-else Statements ...................................................................................................................19
2.1 Que No 1 Largest value among three.................................................................................. 20
2.1.1 Code Area .................................................................................................................... 20
2.2 Que No 2 Grade Program using nested if else .................................................................... 21
2.3 Que No 3 vowel / Consonant .............................................................................................. 22
2.3.1 Code............................................................................................................................. 22
2.4 Que No 4 Even / Odd.......................................................................................................... 23
2.4.1 Code Area .................................................................................................................... 23
2.5 Que No 5 Capital / Small Letter.......................................................................................... 24
2.5.1 Code Area .................................................................................................................... 24
2.6 Que No 6 Ice / water / steam.............................................................................................. 25
2.6.1 Code Area .................................................................................................................... 25
4. Lab Manual Programming Fundamental
5
Lab Manual # 3 ...........................................................................................................................26
Switch Statement.........................................................................................................................26
3.1 Que No 1 Switch statement................................................................................................. 27
3.1.1 Code Area .................................................................................................................... 27
Lab Manual # 4 ...........................................................................................................................30
For/while Loops ..............................................................................................30
4.1 Example .............................................................................................................................. 31
4.1.1 Output........................................................................................................................... 31
4.2 Example Of While Loop ..................................................................................................... 31
4.2.1 Output........................................................................................................................... 31
4.3 Flow chart of while Loop.................................................................................................... 32
4.4 Factorial Definition............................................................................................................. 32
4.4.1 Code of Factorial Program........................................................................................... 32
4.5 For loop ............................................................................................................................... 33
4.5.1 Example of for loop ..................................................................................................... 33
4.5.2 Write the output of the program step by step............................................................... 33
4.5.3 Final Output ................................................................................................................. 33
4.6 Flow Chart of Table 2 ......................................................................................................... 34
4.6.1 Write a program for the above flow chart........................................................................ 34
4.7 Example .............................................................................................................................. 35
4.8 QUE NO 1 Terminate program on entering zero.......................................................... 35
4.8.1 Code............................................................................................................................. 35
4.9 QUE NO 2 Factorial program .......................................................................................... 36
4.9.1 Code................................................................................................................................. 36
4.10 QUE NO 3 Fibonacci series........................................................................................... 37
4.10.1 Code........................................................................................................................... 37
4.11 Que No 4 . Armstrong number....................................................................................... 38
4.11.1 Code........................................................................................................................... 38
4.12 QUE NO 5 Largest among user defined numbers ..................................................... 39
4.12.1 Code........................................................................................................................... 39
4.13 Nested loops Table 12 *12 .............................................................................................. 40
4.13.1 Code........................................................................................................................... 40
4.14 Series using nested loops .................................................................................................. 41
4.14.1 Code for Series 1........................................................................................................ 41
5. Lab Manual Programming Fundamental
6
4.14.2 Code for series 2 ........................................................................................................ 42
4.14.3 Code for series 3 ........................................................................................................ 43
Lab Manual # 5 ...........................................................................................................................44
Do-while loop...............................................................................................................................44
5.1 do-while loop syntax........................................................................................................... 45
5.1.1 Flow Chart of do-while loop............................................................................................ 45
5.2 QUE 1 Calculate the sum of user defined numbers...................................................... 46
5.2.1 Code............................................................................................................................. 46
5.3 QUE NO 2 Calculate factorial of user defined numbers.............................................. 47
5.3.1 Code............................................................................................................................. 47
5.4 Que 3 Calculator ................................................................................................................ 48
5.4.1 Code............................................................................................................................. 48
Lab Manual # 6 ...........................................................................................................................49
Function ........................................................................................................................................49
6.1 Built In Function................................................................................................................. 50
6.1.1 Out Put ......................................................................................................................... 50
6.2 Write types of Function....................................................................................................... 50
6.3 Write Syntax Of function (Prototype, call & body of function) ......................................... 50
6.4 Que No 1 square () functioin............................................................................................... 51
6.4.1 Code............................................................................................................................. 51
6.5 Que No 2 Finding average using function .......................................................................... 52
6.5.1 Code............................................................................................................................. 52
6.6 Que No 3............................................................................................................................. 53
6.7 Que No 4 finding area of rectangle..................................................................................... 54
6.7.1 Code............................................................................................................................. 54
6.8 Que No 5 Check date program............................................................................................ 55
6.8.1 Code............................................................................................................................. 55
6.9 Que No 6 Leap Year program............................................................................................. 56
6.9.1 code.............................................................................................................................. 56
6.10 Que No 7 Finding largest using if else in function ........................................................... 57
6.10.1Code............................................................................................................................. 57
6.11 QueNo 8............................................................................................................................ 58
6.11.1 Code........................................................................................................................... 58
6.12 Comparison of functions................................................................................................... 59
6. Lab Manual Programming Fundamental
7
6.13 Que No 9 Swap by using function.................................................................................... 59
6.13.1 Code........................................................................................................................... 59
6.14 Provide an Example of Functions Overloading ................................................................ 60
6.15 Que 10 Find Factorial from -1 to 10 using function......................................................... 61
Lab Manual # 7 ...........................................................................................................................62
1-D Arrays / Strings ....................................................................................................................62
7.1 Introduction......................................................................................................................... 63
7.2 Que No 1 Displaying age of persons using array................................................................ 63
7.2.1 Code............................................................................................................................. 63
7.3 Que No 2 Changing values between arrays ........................................................................ 64
7.3.1Code............................................................................................................................... 64
7.4 Que No 3............................................................................................................................. 65
7.4.1 Code............................................................................................................................. 65
7.5 Que No 4............................................................................................................................. 66
7.5.1 Code............................................................................................................................. 66
7.6 Que No 5............................................................................................................................. 67
7.6.1 Code............................................................................................................................. 67
7.7 Que No 6............................................................................................................................. 68
7.7.1Code............................................................................................................................... 68
7.8 Que No7.............................................................................................................................. 69
7.8.1 Code............................................................................................................................. 69
7. 9 Que No 8............................................................................................................................ 70
7.10 Strings ............................................................................................................................... 71
7.10.1 Example of Strings..................................................................................................... 71
7.10.2 Que No 9 Get the name from user ............................................................................. 72
7.11 The String I/O Function gets() & puts()............................................................................ 72
7.12 The String I/O Function gets() & puts()............................................................................ 72
7.13 strcpy & strcmp................................................................................................................. 73
7.13.1 strcpy & strcmp example ........................................................................................... 73
7.14 Que No 9 String Deletion.................................................................................................. 74
Lab Manual # 8 ...........................................................................................................................75
2-D Arrays ...................................................................................................................................75
8.1 Que No 1 Shows sale of Pharmaceutical distribution company......................................... 76
8.1.1 Code............................................................................................................................... 76
7. Lab Manual Programming Fundamental
8
8.2 Que no 2 Adding two matrices ........................................................................................... 77
8.2.1 Code............................................................................................................................. 77
8.4 Que no 3 Printing matrix in reserve .................................................................................... 78
8.4.1 Code............................................................................................................................. 78
8.5 Que no 4 Transpose of a Matrix.......................................................................................... 79
8.5.1Code............................................................................................................................... 79
8.6 Que no 5 Agent Program .................................................................................................... 80
8.6.1 Code............................................................................................................................. 80
Lab Manual # 9 ...........................................................................................................................81
Structures......................................................................................................................................81
9.1 Introduction......................................................................................................................... 82
9.2 Structure Example............................................................................................................... 82
9.2.1 Output........................................................................................................................... 82
9.2.2 Description................................................................................................................... 83
9.3 Que No 1 Area of rooms ..................................................................................................... 84
9.3.1 Code............................................................................................................................. 84
9.4 Que No 2 Phone Number Program ..................................................................................... 85
9.4.1 Code............................................................................................................................. 85
9.5 Que No 3 Employee record................................................................................................. 86
9.5.1 Code............................................................................................................................. 86
9.5 Que No 4 Memory size of a structure ................................................................................. 87
9.5.1 Code............................................................................................................................. 87
9.6 Que No 5 Average Age Program ........................................................................................ 88
9.6.1 Code............................................................................................................................. 88
9.7 Que No 6 Nested Structure ................................................................................................. 89
9.7.1 Code............................................................................................................................. 89
9.8 Que No 7 Access of structure data members with pointer to structure............................... 90
Lab Manual # 10 .........................................................................................................................91
Pointers .........................................................................................................................................91
10.1 Introduction to Pointers......................................................................................................... 92
10.1.1 Example of Pointers ................................................................................................... 92
10.2 Pointer To Arrays.................................................................................................................. 92
10.2 Que No 1 Print the values from array ............................................................................... 93
10.3 Que No 2 Print the values and memory address from an array ........................................ 93
8. Lab Manual Programming Fundamental
9
10.4 Pointer Arithmetic............................................................................................................. 94
10.4.1 Example of Pointer arithmetic ................................................................................... 94
10.4.2 Output......................................................................................................................... 94
10.5 Que No 3 Accessing values by Arithmetic operator......................................................... 95
10.5.1 Code........................................................................................................................... 95
10.6 Que No 4 Moving in array through pointers..................................................................... 96
10.6.1 Code........................................................................................................................... 96
10.7 Pointer Comparison........................................................................................................... 96
10.7.1 Pointer Comparison Example .................................................................................... 97
10.8 Que No 4 Question Max ................................................................................................... 97
10.8.1 Code........................................................................................................................... 97
10.9 Pointer to functions ........................................................................................................... 98
10.9.1 Que No 5 Swap the same values using pointers ............................................................ 98
10.10 Que No 6 Returning more than one values from a function........................................... 99
10.10.1 Code......................................................................................................................... 99
Lab Manual # 11 .......................................................................................................................100
Files..............................................................................................................................................100
11.1 Introduction to Files.........................................................................................................101
11.2 Files of C..........................................................................................................................101
11.3 Files in C++......................................................................................................................102
11.3.1 Open a file.....................................................................................................................102
11.4 Que No 1 Create a text file...............................................................................................103
11.4.1 Code..........................................................................................................................103
11.5 Que No 2 Read from the file............................................................................................104
11.5.1 Code..........................................................................................................................104
11.6 Que No 3 Write data through variable .............................................................................105
11.6.1 code...........................................................................................................................105
11.7 String with Embedded blanks ..........................................................................................106
11.7.1 Code..........................................................................................................................106
Lab Evaluation Summary ....................................................................................................107
9. Lab Manual Programming Fundamental
1
0
INTRODUCTION
The objective of this lab manual is to give students step-by-step examples to become
familiar with programming concepts, design, and coding.
F E AT U R E S
To ensure a successful experience for instructors and students alike, these lab munals
includes the following features:
Lab Objectives—Every lab has a brief description and list of learning
objectives
Materials Required—Every lab includes information on hardware, software, and other
materials you will need to complete the lab
Completion Times—Every lab has an estimated completion time so that you can plan
your activities more accurately
Activity Sections—Labs are presented in manageable sections; where
appropriate, additional Activity Background information is provided to illustrate
the importance of a particular project
Step-by-Step Instructions—Every lab provides steps to enhance technical proficiency;
some labs include Critical Thinking exercises to challenge students
Review Questions—Some labs include reviewquestions to help reinforce concepts
presented in the lab
SOFTWA R E REQUIREMENTS
Computer running Windows 98,Windows Me,Windows 2000, Windows XP
Recommended compiler is MicrosoftVisual C++ .NET or Microsoft Visual Studio
COMPLETING THE LAB ASSIGNMENTS
Some lab assignments require written answers to complete an exercise, while
others are programming assignments that require you to work with a C++ compiler.
Check with your instructor for instructions on completing the written
assignments. For example, you can print pages directly from the appropriate
PDF file, and then write directly on the page.
To complete the programming assignments, use the compiler that your instructor
recommends or requires. Print all the documentation assigned, including
program code, program prompts, input, and output displayed on the screen, input
files, and output files.You can submit your written answers and the printed
documentation with a lab cover sheet for grading. If your instructor requires an
electronic copy of your work, e-mail the completed assignment to your instructor
or include a removable disk with your work.Your instructor will tell you what is
10. Lab Manual Programming Fundamental
10
needed, but be sure to submit the .cpp, .h, and any .srt or .txt files that you
create, as well as any input and output files. Also include your name or ID in the
titles of all your files. To provide program documentation, compile and run your
program, copy the prompts, input, and output (if appropriate), and paste them as
a block comment at the end of your program. Use the Copy and Paste features
of your C++ program development kit to do so. After you paste the comment
in the program, either print the program file from your text editor or submit the
program file to your instructor electronically.
12. Lab Manual # 1 Basic
12
1.1 A typical C++ environment
Editor
Preprocessor
Compiler
Linker
Program is created in the
editor and stored on disk.
Disk
Preprocessor program
processes the code.
Compiler creates object
code and stores it on disk.
Linker links the object
code with the libraries
Loader
Disk
Primary Memory
Loader puts program
in memory.
Primary Memory
CPU takes each
instruction and
executes it, possibly
storing new data
CP
Disk
Disk
Disk
13. Lab Manual # 1 Basic
13
Error(s):
source_file.cpp(3): error C2006: '#include': expected a filename, found
'newline'
source_file.cpp(3): fatal error C1083: Cannot open include file: '': No such
file or directory
Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x64
1.2 ProgramNo 1
#include
<iostream.h> main ( )
{
cout << ― Welcome to ―;
}
1.2.1 Out Put of Program No 1
1.3 Declare Variables
Declare 3 Integer Type & 3 float type Variables.
INT FLOAt
Int a=3 Float f=2.5
Int sum=7 Float g=5.5
Int c=9 Float n=9.9
1.4 ProgramNo 2 Example Of Assignment Operator
#include <iostream>
using namespace std;
int main ()
{
int a, b;
a = 10;
b = 4; a
= b; b
= 7;
cout << "a :";
cout << a;
cout << " b :";
cout << b;
return 0;
}
14. Lab Manual # 1 Basic
14
1.4.1 Out Put of Program No 2
a :4
b :7
1.5 Arithmetic Operators
Write Arithmetic Operators
Division unary operators(++,--)
Multiplication binary operators(+-*/)
Addition
Subtraction
1.5.1 Precedence
Write precedence of the arithmetic operators
Parentheses( ) BRACKET
DIVIDE
MULTIPLY *,/,% (left to right
ADDITION
SUBTRACTION
()
*
/
%
+, -
bracket
Multiplication
Division
Modulus
Addition , subtraction left to right
15. Lab Manual # 1 Basic
15
1.6 ProgramNo 3
Write a program in C++ that display following output
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
***** *****
###############################
###############################
###############################
1.6.1 Code of Program No 3
2 #include <iostream>
3 using namespace std ;
4 int main()
5 {
6 cout<<"***** t t t *****"<<endl;
7 cout<<"***** t t t *****"<<endl;
8 cout<<"***** t t t *****"<<endl;
9 cout<<"***** t t t *****"<<endl;
10 cout<<"***** t t t *****"<<endl;
11 cout<<"***** t t t *****"<<endl;
12 cout<<"***** t t t *****"<<endl;
13 cout<<"***** t t t *****"<<endl;
14 cout<<"***** t t t *****"<<endl;
15 cout<<"***** t t t *****"<<endl;
16 cout<<"***** t t t *****"<<endl;
17 cout<<"***** t t t *****"<<endl;
18 cout<<"###############################"<<endl;
19 cout<<"###############################"<<endl;
20 cout<<"###############################"<<endl;
21 }
16. Lab Manual # 1 Basic
16
21.1 ProgramNo 4
Write a programin C++ which prints following output,
$*********************************************$
$*********************************************$
$*Welcome to *$
$* School of Engineering *$
$* lslamabad. *$
$**********************************************$
$**********************************************$
Code of Program No 4
#include <iostream>
using namespace std;
int main()
{
cout<<"$********************************************$"<<endl;
cout<<"$********************************************$" <<endl;
cout<<"$*welcome to *$"<<endl;
cout<<"$* government post graduate *$"<<endl;
cout<<"$* college Haripur *$"<<endl;
cout<<"$*********************************************$"<<endl;
cout<<"$*********************************************$"<<endl;
}
21.2 ProgramNo 5
Write a program in C++, which takes radius from the user and calculate the area of
sphere i.e
Area=4pr2
(Hint p = 3.1416
Area = 4 * 3.1416 * r * r)
17. Lab Manual # 1 Basic
17
1.8.2 Code of Program No 5
2 #include <iostream>
3 using namespace std;
4 int main()
5 { float r;
6 float x=3.1416;
7 cout<<"enter radius"<<endl;
8 cin>>r;
9 float a=4*x*r*r;
10 cout<<"area ="<<a;
11 }
11.8.2Out Put of Program No 5
12 enter radius
13 2.5
14 area =78.54
#include <iostream>
using namespace std;
int main()
{
int a=1,b=2,c=3,d=4,e=5;
int avg=(a+b+c+d+e)/5;
cout<<"find average of numbers"<<avg;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int maths=11,english=22,urdu=33,science=44,islamiyat=55;
int avg=(maths+english+urdu+science+islamiyat)/5;
cout<<"find average of numbers"<<avg;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int length,width;
cout<<"enter a value of length =";
cin>>length;
cout<<"enter a value of width =";
cin>>width;
int area=length*width;
cout<<"area of rectangle"<<area;
return 0;
19. Lab Manual # 1 Basic
19
21.3 ProgramNo 6
Write a program to find the number of bytes occupied by various data types using
the sizeof operator?
int a;
char b;
float c;
long intd;
bool e;
unsigned int j;
unsigned long k;
21.3.1 Code
#include <iostream>
using namespace std;
int main()
{
cout << "Size of int a : " << sizeof(int) << " byte" << endl;
cout << "Size of char b: " << sizeof(char) << " bytes" << endl;
cout << "Size of float c: " << sizeof(float) << " bytes" << endl;
cout << "Size of long int d: " << sizeof(double) << " bytes" << endl;
cout << "Size of unsigned int j: " << sizeof(unsigned int) << " bytes" << endl;
cout << "Size of unsigned long k: " << sizeof(unsigned long) << " bytes" << endl;
return 0;
}
OUTPUT OF PROGRAM;
Size of int a : 4 byte
Size of char b: 1 bytes
Size of float c: 4 bytes
Size of long int d: 8 bytes
Size of unsigned int j: 4 bytes
Size of unsigned long k: 8 bytes
21. Lab Manual # 2 if/if-else statements
20
2.1 Que No 1 Largestvalue among three
Write a program in C++ that take input of three integer‘s numbers from user. Find
the largest number among three of them.
2.1.1 Code Area
3 #include <iostream>
4 using namespace std;
5
6 int main() {
7 float a,b, c;
8
9 cout << "Enter three numbers: ";
10 cin >> a >> b >> c;
11 if(a >= b && a >=c)
12 cout << "Largest number: " << a;
13
14 if(b >= a && b >= c)
15 cout << "Largest number: " << b;
16
17 if(c >= a && c>= b)
18 cout << "Largest number: " << c;
19
20 return 0;
21 }
Output of program;
Enter three numbers: 10.0 9.9 8.80
Largest number: 10
22. Lab Manual # 2 if/if-else statements
21
21.1 Que No 2 Grade Programusing nested if else
Write a program in C++ using if/else operator with nested statements to find the grade
of a student.
The detail is as follow.
grade >= 90
Grade A grade
>= 80
Grade B grade >=70
Grade C grade >=60
Grade D
21.1.1Code Area
22 #include <iostream>
23 using namespace std;
24
25 int main()
26 {
27 int marks;
28 cout<<"enter your marks";
29 cin>>marks;
30 if (marks>=90 && marks==100)
31 cout<<"grade A";
32 else if (marks>=80&& marks==90)
33 cout<<"grade B";
34 else if (marks>=70&& marks==80)
35 cout<<"grade C";
36 else if (marks>=60&& marks==70)
37 cout<<"grade D";
38 else cout<<"invalid numbers";
39 return 0;
40 }
23. Lab Manual # 2 if/if-else statements
22
40.1 Que No 3 vowel / Consonant
Write a program in C++ to input a single character and print a message― It is
vowel" if it is vowel otherwise print message "It is a "consonant― Use if-else
structure and OR (||) operator only
40.1.1Code
41 #include <iostream>
42 #include <string>
43 using namespace std;
44 int main()
45 {
46 char w;
47 cout<<"Enter an alphabet: ";
48 cin>>w;
49 if(w=='a'||w=='e'||w=='i'||w=='o'||w=='u')
50 cout<<"it is a vowel: "<<w;
51 else(cout<<"it is consunant: ")<<w;
52 }
24. Lab Manual # 2 if/if-else statements
23
52.1 Que No 4 Even / Odd
Write a Program in C++ that take an Integer Value from the user and tell that
the number is EVEN or ODD.
52.1.1Code Area
53 #include <iostream>
54 #include <string>
55 using namespace std;
56 int main()
57 {
58 int r;
59 cout<<"write an integer";
60 cin>>r;
61 if(r%2==0)
62 cout<<("it is a even number");
63 else if (r%2==1)
64 cout<<("it is an odd number");
65 return 0;
66 }
25. Lab Manual # 2 if/if-else statements
24
66.1 Que No 5 Capital / Small Letter
Write a program in C++ that take a single character from the user, and tells it's a
Small Letter or it's a CAPITAL letter using nested if statement only.
66.1.1Code Area
67 #include<iostream>
68 using namespace std;
69 int main ()
70 {
71 char un;
72 cout<<"Enter any character:";
73 cin>>un;
74 if (un>=65 && un<=90)
75 cout<<"Character is a capital letter";
76 else if (un>=97 && un<=122)
77 cout<<"Character is a small letter";
78 else if (un>=48 && un<=57)
79 cout<<"Character is a digit";
80 else if ((un>0 && un<=47)||(un>=58 && un<=64)||
81 (un>=91 && un<=96)||(un>=123 && un<=127))
82 cout<<"Character is a special symbol";
83 return 0;
84 }
Output of program:
Enter any character:E
Character is a capital letter
26. Lab Manual # 2 if/if-else statements
25
84.1 Que No 6 Ice / water/ steam
Make a program in C ++ that tells the form of Water whether it is Ice, Water or
Steam. Display the menu also as under.
Temperature Less than 0 = ICE
Temperature Greater than 0 & Less than 100 = Water
Temperature Greater than 100 = STEAM
84.1.1Code Area
85 #include<iostream>
86 using namespace std;
87 int main()
88 {
89 float temp;
90 cout<<"Enter the temperature"<<endl;
91 cin>>temp;
92 if (temp < 0) cout<<"ICE"<<endl;
93 else
94 if (temp >= 0 && temp <= 100) cout<<"WATER"<<endl;
95 else
96 cout<<"STEAM"<<endl;
97 return 0;
98 }
Output of program:
Enter the temperature:110
STEAM
Enter the temperature :10
WATER
Enter the temperature
-10
ICE
28. Lab Manual # 3 Switch Statements
3.1 Que No 1 Switch statement
Write a program in C++ using switch statement that contain option as under
Enter 1--> To Find Largest Number Among Three Variables.
Enter 2--> To Find ODD or EVEN
Enter 3--> To Find Condition of Water
Enter 4--> To Find Grade Of Student
Detail of Option 3
Temperature Less than 0 = ICE
Temperature Greater than 0 & Less than 100 = Water
Temperature Greater than 100 = STEAM
Detail of option 4
grade >= 90
Grade A grade
>= 80
Grade B grade >=70
Grade C grade >=60
Grade D
3.1.1 Code Area
4 #include<iostream>
5 #include <string>
6 using namespace std;
7 int main()
8 {
9 int option;
10 cout<<"nttEnter the optionn";
11 cout<<"Enetr 1--> To Find Largest Number Among Three Variables.n";
12 cout<<"Enetr 2--> To Find ODD or EVENn";
13 cout<<"Enetr 3--> To Find Condition of Watern";
14 cout<<"Enetr 4--> To Find Grade Of Studentn";
15 cin>>option;
16 switch (option)
17 {
18 case 1:
19 int a,b,c, large;
20 cout<<"Enter First Integer=";
21 cin>>a;
22 cout<<"Enter Second Integer=";
23 cin>>b;
24 cout<<"Enter Third Integer=";
25 cin>>c;
26 if (a > b)
27 large = a;
28 else
29 large = b;
30 if (large > c )
31 cout<<"Largest is ="<<large<<endl;
32 else
33 cout<<"Largest is ="<<c<<endl;
34 break;
35 case 2:
29. Lab Manual # 3 Switch Statements
27
36 int value;
37 cout<<"Enter an Interger value ";
38 cin>>value;
39 if (value % 2 == 0)
40 cout<<"Your number is EVENn";
41 else
42 cout<<"Your number is ODDn";
43 break;
44 case 3:
45 int t;
46 cout<<"Temperature Less than 0 = ICE n"
47 <<"Temperature Greater than 0 & "
48 <<"Temperature Less than 100 = Watern"
49 <<"Temperature Greater than 100 = STEAMn";
50 cout<<"nnttPlease enter the Temperature=";
51 cin>>t;
52 if ( t <= 0 )
53 cout<<"Form of water is "ICE''n"<<endl;
54 else if( t > 0 && t < 100 )
55 cout<<"Form is "WATER''n"<<endl;
56 else if ( t >= 100 )
57 cout<<"Form of water is "steam"n";
58 break;
59 case 4:
60 int grade;
61 cout<<"Enter the grade of student=";
62 cin>>grade;
63 if ( grade >= 90 )
64 cout<< " Grade A n";
65 else if (grade >= 80 )
66 cout<<" Grade B n";
67 else if ( grade >=70 )
68 cout<<" Grade C n";
69 else if (grade >=60)
70 cout<<" Grade D n";
71 else
72 {
73 cout<<" Grade F n";
74 cout<<" You have to take the classes againn";
75 cout<<" Work Hard To Get Good Graden";
76 }
77 break;
78 default:
79 cout<<"You Entered Invalid Optionn";
80 cout<<"Enetr A Valid Optionn";
81 break;
82 return 0;
83 }
84 }
32. Lab Manual # 4 For / While loop
30
Lab Manual # 4
For/while Loops
33. Lab Manual # 4 For / While loop
31
4.1 Example
int sum ;
sum = 1+2+3+4+5+.............................+10;
cout << sum ;
Output
5 #include <iostream>
6 #include <string>
7 int main()
8 {
9 int sum ;
10 sum = 1+2+3+4+5+10;
11 cout << sum ;
12 }
Output
In function 'int main()':
8:17: error: expected primary-expression before '...' token
WITHOUT ERROR;
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sum ;
sum = 1+2+3+4+5+6+7+8+9+10;
cout << sum ;
}
OUTPUT;
55
12.1 Example Of While Loop
int sum , number
; sum = 0 ;
number = 1 ;
while ( number <= 1000 )
{
sum = sum + number ;
number = number + 1 ;
}
cout << ― The sum of the first 1000 integer starting from 1 is ‖ << sum ;
12.1.1 Output
The sum of the first 1000 integer starting from 1 is 500500
34. Lab Manual # 4 For / While loop
32
12.2 Flow chart of while Loop
12.3 Factorial Definition
n! = n*(n-1)*(n-2)*(n-3)…………*3*2*1 Out Put
12.3.1Code of Factorial Program
int number ;
int factorial ;
factorial = 1 ;
cout << ―Enter the number of
Factorial‖ ; cin >> number ;
while ( number >= 1 )
{
factorial = factorial * number
; number = number – 1 ;
}
cout << ―Factorial is‖ << factorial ;
output;
enter any number=
6
factorial of the 6is 720
35. Lab Manual # 4 For / While loop
33
12.4 For loop
for ( initialization condition ; termination condition ; increment condition )
{
statement ( s ) ;
}
12.4.1Example of for loop
int counter ;
for( counter = 0 ; counter < 10 ; counter = counter + 1
) cout << counter;
12.4.2Write the output of the program stepby step
13 Initialization : counter=0
14 Check condition is true
15 0
16 Increment : counter=counter+1
17 Counter=0+1
18 Check condition is true then print
19 1
20 Increment Counter=1+1 Check condition is true then print
21 2
22 Increment Counter=2+1
23 Check condition is true
24 3
25 Increment Counter=3+1
26 Check condition is true
27 4
28 Increment Counter=4+1
29 Check condition is true
30 5
31 Increment Counter=5+1
32 Check condition is true
33 6
34 Increment Counter=6+1.
35 Check condition is true
36 7
37 Increment Counter=7+1.
36. Lab Manual # 4 For / While loop
34
38 Check condition is true
39 8
40 Increment Counter=8+1.
41 Check condition is true
42 9
43 Increment Counter=9+1
44 .Check condition is false then exit from for loop
45 exit
45.1.1Final Output
0123456789
37. Lab Manual # 4 For / While loop
35
45.2 Flow Chart of Table 2
40. Lab Manual # 4 For / While loop
38
45.3 QUE NO 2 Factorial program
Write a Program in C++ that calculate the factorial of a user defined number, using for
loop
45.3.1Code
46 #include <iostream>
47 using namespace std;
48 int main()
49 {
50 unsigned int n;
51 unsigned long long factorial = 1;
52 cout << "Enter a positive integer: ";
53 cin >> n;
54 for(int i = 1; i <=n; ++i)
55 {
56 factorial *= i;
57 }
58 cout << "Factorial of " << n << " = " << factorial;
59 return 0;
60 }
41. Lab Manual # 4 For / While loop
39
60.1 QUE NO 3 Fibonacci series
Write a Program in C++ that shows The Fibonacci series
1 1 2 3 5 8 13 ….. Using wile loop
60.1.1 Code
61 #include <iostream>
62 using namespace std;
63 int main()
64 {int n,f,f1=-1,f2=1;
65 cout<<" Enter The Number Of Terms:";
66 cin>>n;
67 cout<<" The Fibonacci Series is:";
68 while(n>0)
69 {
70 f=f1+f2;
71 f1=f2;
72 f2=f;
73 cout<<" n"<<f;
74 n--;
75 }
76 return 0;
77 }
OUTPUT OF A PROGRAM
The Fibonacci Series is:
The Fibonacci Series is:
0
1
1
2
3
5
8
13
42. Lab Manual # 4 For / While loop
40
77.1 Que No 4 . Armstrong number
Write a Program in C++ that check the user defined number is Armstrong or not.i.e 153
is an Armstrong number
(1)3
+ (5) 3
+ (3)3
1 + 125 + 27 = 153
77.1.1 Code
78 #include <iostream>
79 using namespace std;
80 int main()
81 {
82 int num, r, sum=0, temp_num;
83 cout << "Enter number to check Armstrong number ";
84 cin >> num;
85 temp_num = num;
86 while(num > 0)
87 {
88 r = num % 10;
89 sum = sum + (r * r * r);
90 num = num / 10;
91 }
92 if(temp_num == sum)
93 cout << "Entered number is Armstrong Number." << endl;
94 else
95 cout << "Entered number is not Armstrong Number." << endl;
96 return 0;
97 }
OUTPUT OF PROGRAM
Enter number to check Armstrong number 370
Entered number is Armstrong Number.
Enter number to check Armstrong number 156
Entered number is not Armstrong Number.
43. Lab Manual # 4 For / While loop
41
97.1 QUE NO 5 Largestamong user defined numbers
Write a Program in C++ that takes Integer values from user, and then find the largest
number among all the Integers, and display the largest number on the screen. Use
While loop to control the Input, Terminate the Program on entering zero. Find the
Largest Integer Using If Statement.
97.1.1 Code
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a, b, c;
6. cout<<"enter a number";
7. cin>>a;
8. cin>>b;
9. cin>>c;
10. while (a!=0 || b!=0 || c!=0)
11. {
12. if(a >= b && a >=c)
13. cout << "Largest number: " << a;
14. if(b >= a && b >= c)
15. cout << "Largest number: " << b;
16. if(c >= a && c>= b)
17. cout << "Largest number: " << c;
18. }
19. cout<< "you enter 0";
20. }
49. Lab Manual # 5 do-while loop
45
5.1 do-while loop syntax
do
{
statements ;
}
while ( condition ) ;
5.1.1 Flow Chart of do-while loop
50. Lab Manual # 5 do-while loop
46
5.2 QUE 1 Calculate the sum of user defined numbers
Develop a program in C++ that take integer type values from user & calculate the
sum from ‗0‘ to number entered, using do while loop.
Number = 5
1+2+3+4+5 = 15
5.2.1 Code
6 #include <iostream>
7 using namespace std;
8 int main()
9 {
10 int n=1,sum=0 ;
11 cout<<"Enter a number"<<endl;
12 cin >>n;
13 do
14 {
15 cout<<n<<endl;
16 sum=++n;
17 cout<<sum<<endl;
18 }
19 while(n>=0)
20 //getch();
21 //return 0
22 }
51. Lab Manual # 5 do-while loop
47
22.1 QUE NO 2 Calculate factorialof user defined numbers
Develop a program in C++ that calculate the factorial of a given number. Use for loop to
calculate the factorial, & do – while loop to perform the operation as many times as user
127 #include <iostream>
128 #include <string>
129 using namespace std;
130 int main()
131 {
132 int rows;
134 cout << "Enter number of rows: ";
135 cin >> rows;
137 for(int i = 1; i <= rows; ++i)
138 {
139 for(int j = 0; j <= i; ++j)
140 {
141 cout << i++;
142 }
143 cout << "n";
144 }
145 return 0;
23 #include <iostream>
24 using namespace std;
25 int main()
26 {
27 int num;
28 char ch;
29 cout<<"Enter a number to find factorial"<<endl;
30 cin>>num;
31 do{
32 for(int i=0;i<=num; i++)
33 int f=f*i;
34 cout<<"factorial="<<"f"<<endl;
35 cout<<"enter y for contuniue and n for exit"<<endl;
36 cin>>ch;
37 }
38 while (ch=='y');
39 return 0 ;
40 }
52. Lab Manual # 5 do-while loop
48
40.1 Que 3 Calculator
Create the equivalent of a four-function calculator. The program should Ask the user to
enter a number, an operator, and another number (10 + 20), using floating point.It
should then carry out the specified arithmetical operation: adding, subtracting,
multiplying, or dividing the two numbers. Use switch statement to select the operation.
Finally, display the Result.
40.1.1Code
41 enter two no66
42 88
43 enter the operator+-*/+
44 66+88=154enter x when run again, enter y when exitx
45 enter two no88
46 99
47 enter the operator+-*/*
48 88*99=8712enter x when run again, enter y when exit
Code;
#include <iostream>
#include <string>
using namespace std;
int main()
{
float n1,n2;
char op,np;
do
{
cout<<"enter two no";
cin>>n1>>n2;
cout<<"enter the operator+-*/";
cin>>op;
switch(op)
{
case '+':
cout<<n1<<"+"<<n2<<"="<<n1+n2;
break;
case '-':
cout<<n1<<"-"<<n2<<"="<<n1-n2;
break;
case '*':
cout<<n1<<"*"<<n2<<"="<<n1*n2;
break;
case '/':
cout<<n1<<"/"<<n2<<"="<<n1/n2;
break;
default:
cout<<"Invalid operator";
}
cout<<"enter x when run again, enter y when exit";
cin>>np;
}
while (np=='x'||np=='X');
return 0;
}
54. Lab Manual # 6 Functions
50
6.1 Built In Function
#include <math> // defines the sqrt()
function #include <iostream.h>
int main()
{ // tests the sqrt() function: for
(int x=0; x <=9; x++)
cout << "t" << x << "t" << sqrt(x) << endl;
}
6.1.1 Out Put
0 0
1 1
2 1.41421
3 1.73205
4 2
5 2.23607
6 2.44949
7 2.64575
8 2.82843
9 3
6.1 Write types of Function
Built in function
User defined function
6.2 Write Syntax Of function (Prototype, call & body of function)
7 #include <iostream>
8 #include <math.h>
9 using namespace std;
10 int multiplication (int a,int b,int c)
11 {
12 int multiply;
13 multiply = a*b*c;
14 return (multiply);
15 }
16 int main ()
17 {
18 int z;
19 z = multiplication (8,11,9);
20 cout <<"The result of a,b and c is "<< z;
21 return 0 ;
22 }
55. Lab Manual # 6 Functions
51
22.1 Que No 1 square () functioin
Write a program in C++ that has a function of square (). The user should enter
the number and the code should display the square of the given number.
22.1.1Code
23 #include <iostream>
24 #include <math.h>
25 using namespace std;
26 int square (int a)
27 {
28 int square;
29 square = a*a;
30 return (square) ;
31 }
32 int main()
33 {
34 int e, a;
35 cout<<"Enter a number: ";
36 cin >> a;
37 e = square(a);
38 cout <<"the square of a: "<< e;
39 return 0 ;
40 }
Output of program;
Enter a number: 11
the square of a: 121
56. Lab Manual # 6 Functions
52
40.1 Que No 2 Finding average using function
Get three integers numbers from user, Pass them to function, Add them and find
the average in function body, Display the Average in function body
40.1.1Code
41 #include <iostream>
42 #include <math.h>
43 using namespace std;
44 int average (int a,int b, int c)
45 {
46 int avg;
47 avg = (a+b+c)/3;
48 return (avg);
49 }
50 int main()
51 {
52 int a,b,c,d;
53 cout<<"Enter three numbers for average ";
54 cin >> a>>b>>c;
55
56 d=average(a,b,c/3);
57
58 cout <<"the average of no"<< d;
59 return 0 ;
60 }
57. Lab Manual # 6 Functions
53
60.1 Que No 3
Write a code that take numbers from user and displays its cube. The Code
should reads integers and prints their cubes until the user inputs the sentinel
value 0. Each integer read should be passed to the cube() function by the call
cube(n). The value returned by the function should replaces the expression
cube(n) and then should be passed to the output object cout.
60.1.1Code
61 #include <iostream>
62 #include<math.h>
63 #include<string>
64 using namespace std;
65 int cube(int a)
66 {
67 int cb;
68 cb=a*a*a;
69 return cb;
70 }
71 int main()
72 {
73 int a,h;
74 do
75 {
76 cout<<"enter a no to find the cube";
77 cin>>a;
78 cout<<"cube="<<cube(a)<<"n enter 1 to continue or 0 to exit"<<h;
79 cin>>h;
80 }
81 while(h!=0);
82 return 0;
83 }
84 Output;
enter a no to find the cube6
cube=216
enter 1 to continue or 0 to exit01
enter a no to find the cube5
cube=125
enter 1 to continue or 0 to exit11
enter a no to find the cube4
cube=64
enter 1 to continue or 0 to exit10
Exit code: 0 (normal program termination)
`
58. Lab Manual # 6 Functions
54
84.1 Que No 4 finding area of rectangle
Write a function that finds the area of the rectangle on providing length and width.
Get Length & width from user in main() Call the function area() Calculate the
length and return the area Display the result in main()
84.1.1Code
85 #include <iostream>
86 #include <math.h>
87 using namespace std;
88 int area_of_rectangle (int x ,int y)
89 {
90 int multiply;
91 multiply = x*y;
92 return (multiply);
93 }
94 int main()
95 {
96 int a, x, y ;
97 cout <<"Enter the lengh ";
98 cin >> x ;
99 cout <<"Enter the width " ;
100 cin >> y ;
101 a = area_of_rectangle (x,y);
102 cout<<"Area of rectangle= "<<a;
103 //getch();
104 return 0 ;
105 }
Output of a program
Enter the lengh 55
Enter the width 50
Area of rectangle= 2750
59. Lab Manual # 6 Functions
55
105.1 Que No 5 Check date program
Get month ,day & year from user in main() Call the function printDate(int, int, int), Put a
check in printDate() using if statement (month < 1 || month > 12 || day < 1 || day > 31 ||
year < 0) & if it violates the rule display ―Must Enter a Valid Date”, Using switch
Statement get the month, Day and year are displayed normally, Termination should be
on entering 0 in months
105.1.1 Code
106 #include <iostream>
107 using namespace std;
108 int print date (int x , int y , int z )
109 {
110 if (month>12||month<1)
111 cout <<"enter valid month"<<;
112 if (day > 31||day <1)
113 cout <<"enter valid day"<<;
114 if (year<0)
115 cout <<"enter valid year"<<;
116 }
117 int main ()
118 {
119 long int month , day , year ;
120 cout <<"enter month"<<;
121 cin >> month >>;
122 cout <<"enter day"<<;
123 cin >>day>>;
124 cout <<"enter year"<<;
125 cin >>year>>;
126 int z=print date (x,y,z)
127 cout <<z<<;
128 return 0 ;
129 }
Que No 6 Leap Year program
A leap year is a year in which one extra day (February 29) is added to the regular calendar.
Most of us know that the leap years arethe years that are divisible by 4. For example, 1992 and
1996 are leap years. Most people, however, do not know that there is an exception to this rule:
centennial years are not leap years. For example, 1800
and 1900 are not leap years. Furthermore, there is an exception to the exception: centennial
years which are divisible by 400 are leap years. Thus, the year 2000 is a leap year.
Make a program thatfull fills the above criteria using functions
The program should terminate on entering 0
The return type should be Boolean i.e bool isLeapYear(int);
60. Lab Manual # 6 Functions
56
129.1.1 code
130 #include <iostream>
131 #include <math>
132 #include <string>
133 using namespace std;
134 bool leap_year(int year);
135 int main()
136 {
137 int year;
138 bool leap_year(int year);
139 cout << "Please input the year in question: ";
140 cin >> year;
141 if (leap_year == false)
142 {
143 cout << "The year is not a leap year. ";
144 }
145 else
146 {
147 cout << "The year is a leap year. ";
148 }
149 return 0;
150 }
151 bool leap_year(int year)
152 {
153
154 if (year % 4 == 0)
155 {
156 bool leap_year = true;
157 }
158 else if (year % 400 == 0)
159 {
160 bool leap_year = true;
161 }
162 else if (year % 100 == 0)
163 {
164 bool leap_year = false;
165 }
166 else
167 {
168 bool leap_year = false;
169 }
170
171 if (bool leap_year = false)
172 {
173 return false;
174 }
175 else
176 {
177 return true;
178 }
179 }
61. Lab Manual # 6 Functions
57
179.1Que No 7 Finding largestusing if else in function
Write a program in C++ that take two numbers from user and find the largest
among two using function.
The Conditions are if
(a==b)
"A, B are the same“ a = b, (values)
else if (a < b)
"A & B are not same “ a != b(values)
" A is less than B “ a < b (values)
code
else
"A is Greater than B “ a > b (values)
##include<iostream>
using namespace std;
void largest(int ,int );
main()
{
int n1,n2;
cout<<"enter two numbersn";
cin>>n1>>n2;
largest(n1,n2);
}
void largest(int n1,int n2)
{
if(n1==n2)
cout<<n1<<"="<<n2<<endl;
else if(n1<n2)
cout<<n1<<"<"<<n2<<endl;
else
cout<<n1<<">"<<n2<<endl;
}
62. Lab Manual # 6 Functions
58
179.2QueNo 8
Develop a program in C++ that has function printTempOpinion() which prints "Cold" on
if the temperature is below 10, "OK" if the temperature is in the range 20 -> 30,"Hot" if
the temperature is above 30.
179.2.1Code
180 #include <iostream>
181 using namespace std;
182 void printTemperature(int temp)
183 {
184 if (temp<10)
185 cout <<"print the temp that is cold";
186 else
187 if (temp>=20 || temp<=30)
188 cout<<"print the temp that is ok";
189 else
190 cout<<"print the temp that is hot";
191 }
192 int main()
193 {
194 int temp;
195 cout<<"enter the temp";
196 cin>>temp;
197 int z=temp;
198 printTemperature(z);
199 return 0;
200 }
63. Lab Manual # 6 Functions
59
200.1Comparisonof functions
Difference between Passing By Value Versus Passing By Reference
200.2Que No 9 Swap by using function
Develop a program that swap the two values using function by reference
i.e After swap
a = 22.2 a = 44.4
b = 44.4 b = 22.2
200.2.1Code
64. Lab Manual # 6 Functions
60
201 #include <iostream>
202 using namespace std;
203 void swap(float ,float );
204 int main()
205 {
206 float a = 22.2,b = 44.4;
207 cout << "a = " << a << ", b = " << b << endl;
208 swap(b,a);
209 cout << "b = " << b << ", a = " << a << endl;
210 }
211 void swap(float x, float y)
212 {
213 float temp = x;
214 x = y;
215 x = temp;
216 }
68. Lab Manual # 7 1-Dimensional Array / Strings
63
7.1 Introduction
They are special kind of data type In C++ each array has
name
data type
size
They occupy continuous area of memory
7.2 Que No 1 Displaying age of persons using array
Write a program in C++ that take age of five persons and then just display the age
of each person by using arrays.
7.2.1 Code
#include<iostream>
using namespace std;
int main()
{
int marks [7];
int total=0,i;
cout<< "Please enter marks of five students:n"<<endl;
for (i=0; i<7; i++)
{
cin >> marks[i];
}
for (i=0; i<7; i++)
{
total= marks[i];
}
float classavg = total/7;
for(int j=0; j<7; j++)
{
cout<<"Students # "<< j+1<< " scored "<<marks[j]<<" marks";
}
69. Lab Manual # 7 1-Dimensional Array / Strings
64
7.3 Que No 2 Changing values between arrays
Develop a Program that takes array elements from user and then transfer
those elements to another array. Size of array will be 10
7.3.1Code
#include<iostream>
using namespace std;
int main()
{
int arr1[100], arr2[100];
int i, n;
cout<<"nnCopy the elements one array into another array :n"<<endl;
cout<<"Input the number of elements to be stored in the array :"<<endl;
cin>>n;
cout<<"Input elements in the array :n"<<endl;
for(i=0;i<n;i++)
{
cout<<"element "<<endl;
cin>>arr1[i];
}
for(i=0; i<n; i++)
{
arr2[i] = arr1[i];
}
cout<<"nThe elements stored in the first array are :n"<<endl;
for(i=0; i<n; i++)
{
cout<<"arr1[i]"<<endl;
}
cout<<"nnThe elements copied into the second array are :n"<<endl;
for(i=0; i<n; i++)
{
cout<<" arr2[i]"<<endl;
}
cout<<"(nn)"<<endl;
}
70. Lab Manual # 7 1-Dimensional Array / Strings
65
7.4 Que No 3
Develop a program that takes 5 array elements from user. Swap position [2]
element with position [4] element.
7.4.1 Code
#include<iostream>
using namespace std;
int main()
{
int source[5];
int i,temp;
cout<<"n enter 10 array element"<<endl;
for(i=0;i<5;i++)
{
cin>>source[i];
}
temp=source[2];
Arr[2]=source[4];
Arr[4]=temp;
{
cout<<"swapping psition [2] elements with [4] elements"<<source[i]<<endl;
}
return 0;
}
71. Lab Manual # 7 1-Dimensional Array / Strings
66
7.5 Que No 4
Write a program to input data into two different arrays and then to add the two arrays
and store the result in the third array.
7.5.1 Code
#include <iostream>
using namespace std;
int main()
{
int x[5], y[5] ,z[5];
for(int i=0; i<5; i++)
{
cout<<"enter "<<i+1<<" element of first array "<<endl;
cin>>x[i];
cout<<"enter "<<i+1<<" element of second array
"<<endl;
cin>>y[i];
z[i]=x[i]+y[i];
}
for(int k=0; k<5; k++)
{
cout<<"The "<<k+1<<" element of third array is
"<<z[k]<<endl;
}
return 0;
}
73. Lab Manual # 7 1-Dimensional Array / Strings
68
Que No 6
Write a program that takes input from user and checks if the word is Palindrome or Not
7.7.1Code
#include<iostream>
using namespace std;
int main()
{
int n, reverse = 0, temp;
cout<<"Enter a number to check if it is a palindrome or notn";
cin>>n;
temp = n;
while( temp != 0 )
{
reverse = reverse * 10;
reverse = reverse + temp%10;
temp = temp/10;
}
if ( n == reverse)
cout<<n<<"is a palindrome number.n";
else
cout<<n<<"is not a palindrome number.n";
return 0;
}
Output:
Enter a number to check if it is a palindrome or not
44544
44544is a palindrome number.
74. Lab Manual # 7 1-Dimensional Array / Strings
69
7.6 Que No7
Find the Average of 10 numbers passed to an array, using function. The Array should
be controlled by while loop.
7.8.1 Code
#include <iostream>
using namespace std;
void average(int avg[10])
{
float sum=0;
for(int i=0; i<10; i++)
{
sum = sum + avg[i];
}
cout<<sum/10<<endl;
}
int main()
{
int number[10];
int i=0;
while(i<10)
{
cin>>number[i];
i++;
}
average(number);
return 0;
}
ReplyForward
75. Lab Manual # 7 1-Dimensional Array / Strings
70
7. 9 Que No 8
Write a Program in C++ that do the bubble sorting in
Array. Hint
Array Elements are entered by user in ―main function‖, the elements are
user Defined. Terminate on “zero”
Make a separate function “sort()” to do the bubble sort.
Pass array elements to function “sort()”.
Function should sortthe array elements.
Displaythe sorted array elements in main.
7.9.1 Code
#include < iostream>
Using namespace std;
void bubble_sort(long [], long);
int main()
{
long array[100], n, c, d, swap;
cout<<"Enter number of elementsn";
cin>>n;
cout<<"Enter"<<n<<"integersn";
for (c = 0; c < n; c++)
cin>>array[c];
bubble_sort(array, n);
cout<<"Sorted list in ascending order:n";
for ( c = 0 ; c < n ; c++ )
cout<<array[c]<<"n";
return 0;
}
void bubble_sort(long list[], long n)
{
long c, d, t;
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (list[d] > list[d+1])
{
t = list[d];
list[d] = list[d+1];
list[d+1] = t;
}
}
}
}
76. Lab Manual # 7 1-Dimensional Array / Strings
71
7.10 Strings
A string constant is a one-dimensional array of characters terminated by a null ( ‗0‘ ).
For example,
char name[ ] = { 'H', 'A', 'E', 'S', 'L', 'E', 'R', '0' } ;
7.10.1 Example of Strings
/* Program to demonstrate printing of a string */
main( )
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while ( i <= 7 )
{
cout<<name[i];
i++ ;
}
}
/* Program to demonstrate printing of a string */
main( )
{
char name[ ] = "Klinsman" ;
int i = 0 ;
while (name[i] !=‘0‘)
{
cout<<name[i];
i++ ;
}
}
77. Lab Manual # 7 1-Dimensional Array / Strings
72
7.10.2 Que No 9 Get the name from user
Write a program that gets the name from user and then print back on the screen.
#include <iostream>
using namespace std;
int main()
{
string firstName, lastName;
cout << "Enter your name: " << endl;
cin>>firstName>>lastName;
cout<<firstName +" "+ lastName<<endl;
return 0;
}
7.11 The String I/O Function gets()& puts()
cin & cout are not so versatile because they ignore charters after white space, so
to over come this problem C/C++ uses puts & gets function from library file
<stdio.h>
7.12 The String I/O Function gets()& puts()
Write a program that gets the name from user and then print back on the screen using
gets and puts functions.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
char name[100];
cout << "Enter your name: " << endl;
gets(name);
puts(name);
return 0;
}
79. Lab Manual # 7 1-Dimensional Array / Strings
74
7.13 strcpy & strcmp
Strcmp;
strcmp() is a built-in library function and is declared in <string.h> header file. This
function takes two strings as arguments and compare these two strings
lexicographically.
Syntax::
int strcmp(const char *leftStr, const char *rightStr );
strcmp() compares the two strings lexicographically means it starts
comparison character by character starting from the first character until the
characters in both strings are equal or a NULL character is encountered.
Strcpy;
strcpy() is a standard library function in C/C++ and is used to copy one string to
another. In C it is present in string.h header file and in C++ it is present
in cstring header file.
Syntax:
char* strcpy(char* dest, const char* src);
Parameters: This method accepts the following parameters:
dest: Pointer to the destination array where the content is to be copied.
src: string which will be copied.
7.13.1 strcpy & strcmp example
81. Lab Manual # 7 1-Dimensional Array / Strings
76
Strcmp
#include<iostream>
#include <string>
using namespace std;
int main ()
{
int result;
char name[50];
char name2[50];
int strcmp;
cout<<"enter your name"<<endl;
cin.get(name,50);
cin.ignore();
cout<<"enter second name"<<endl;
cin.get(name2,50);
result= strcmp(name,name2)
if (result==0)
cout<<"name are equal"<<endl;
if (result==-1)
cout<<"first name is less than second"<<endl;
if (result==1)
cout<<"first name is greater than second"<<endl;
return 0;
}
22.10Que No 9 String Deletion
Write a program in C++ that take string from user and then enter the position to delete.
Make a separate function for string deletion
#include<iostream>
#include<string>
using namespace std;
int main()
{
int i,a[5],no,pos;
cout<<"Enter Data in Array: "<<endl;
for(i=0;i<5;i++)
{
cin>>a[i];
}
cout<<"nnStored Data in Array: ";
for(i=0;i<5;i++)
{
cout<<a[i]<<endl;
}
cout<<"nnEnter poss. of Element to Delete: "<<endl;
cin>>pos;
if(pos>5)
{
82. Lab Manual # 7 1-Dimensional Array / Strings
77
cout<<"nnThis value is out of range: ";
}
else
{
--pos;
for(i=pos;i<=4;i++)
{
a[i]=a[i+1];
}
cout<<"nnNew Data in Array: ";
for(i=0;i<4;i++)
{
cout<<a[i];
}
}
return 0;
}
Output;
Enter Data in Array:
1
2
3
4
5
Stored Data in Array: 1
2
3
4
5
Enter poss. of Element to Delete:
4
New Data in Array: 1235
84. Lab Manual # 8 2-Dimensional Array
76
8.1 Que No 1 Shows sale of Pharmaceuticaldistribution company
A Distributor of a Pharmaceutical Company has 4 Districts, for supply the medicine. He
requires a program that can display the sales of all his Districts. Write a Program in C++
Using two Dimensional Array that shows the Following Output. The Program should
display the Sale, Districts wise and up to Months i.e
8.1.1 Code
9 #include<iostream>
10 using namespace std;
11 int main()
12 {
13 const int districts=4;
14 const int months=3;
15 int d,m;
16 double sales[districts][months];
17 for(d=0; d<districts; d++)
18 {
19 for(m=0; m<months; m++)
20 {
21 cout<<"Enter The Sales Of District="<<d+1<<" ";
22 cout<<",Month"<<m+1<<"=";
23 cin>>sales[d][m];
24 }
25 }
26 cout<<"nnn"<<endl;
27 cout<<"nnThe Sale Of Districts Is n";
28
29 for(d=0; d<districts; d++)
30 {
31 cout<<"n District # "<<d+1<<" sales :";
32 for(m=0; m<months; m++)
33 {
34 cout<<sales[d][m]<<"t";
35 }
36 }
37 cout<<endl;
38 return 0;
39 }
40 Output;
Enter The Sales Of District=1 ,Month1=14
Enter The Sales Of District=1 ,Month2=12
Enter The Sales Of District=1 ,Month3=10
Enter The Sales Of District=2 ,Month1=8
Enter The Sales Of District=2 ,Month2=6
Enter The Sales Of District=2 ,Month3=4
Enter The Sales Of District=3 ,Month1=2
Enter The Sales Of District=3 ,Month2=2
Enter The Sales Of District=3 ,Month3=4
Enter The Sales Of District=4 ,Month1=6
Enter The Sales Of District=4 ,Month2=8
Enter The Sales Of District=4 ,Month3=10
The Sale Of Districts Is
District # 1 sales :14 12 10
District # 2 sales :8 6 4
District # 3 sales :2 2 4
District # 4 sales :6 8 10
85. Lab Manual # 8 2-Dimensional Array
77
8.1 Que no 2 Adding two matrices
Write a program in C++ that take two matrices and then Add them After inserting two
matrices first display the both matrices and then add them and show the result. i.e
8.1.1 Code
9 #include <iostream>
10 using namespace std;
11 int main()
12 {
13 int r, c, a[10][10], b[10][10], sum[10][10], i, j;
14 cout << "Enter number of rows (between 1 and 10): ";
15 cin >> r;
16 cout << "Enter number of columns (between 1 and 10): ";
17 cin >> c;
18 cout << endl << "Enter elements of 1st matrix: " << endl;
19 for(i = 0; i < r; ++i)
20 for(j = 0; j < c; ++j)
21 {
22 cout << "Enter element a" << i + 1 << j + 1 << " : ";
23 cin >> a[i][j];
24 }
25 cout << endl << "Enter elements of 2nd matrix: " << endl;
26 for(i = 0; i < r; ++i)
27 for(j = 0; j < c; ++j)
28 {
29 cout << "Enter element b" << i + 1 << j + 1 << " : ";
30 cin >> b[i][j];
31 }
32 for(i = 0; i < r; ++i)
33 for(j = 0; j < c; ++j)
34 sum[i][j] = a[i][j] + b[i][j];
35 cout << endl << "Sum of two matrix is: " << endl;
36 for(i = 0; i < r; ++i)
37 for(j = 0; j < c; ++j)
38 {
39 cout << sum[i][j] << " ";
40 if(j == c - 1)
41 cout << endl;
42 }
43 return 0;
44 }
45 Output;
Enter number of rows (between 1 and 10): 2
Enter number of columns (between 1 and 10): 2
Enter elements of 1st matrix:
Enter element a11 : 1234
Enter element a12 : 5678
Enter element a21 : 4321
Enter element a22 : 8765
Enter elements of 2nd matrix:
Enter element b11 : 1357
Enter element b12 : 2468
Enter element b21 : 7531
Enter element b22 : 8642
Sum of two matrix is:
2591 8146
11852 17407
86. Lab Manual # 8 2-Dimensional Array
78
8.4 Que no 3 Printing matrix in reserve.
Enter the values in a matrix and print it in reverse Column order
8.4.1 Code
9 #include <iostream>
10 using namespace std;
11 int main()
12 {
13 int a[3][3],i,j;
14 for(i=0;i<3;i++)
15 {
16 for(j=0;j<3;j++)
17 {
18 cout<<"Enter number :";
19 cin>>a[i][j];
20 }
21 }
22 for(i=2;i>=0;i--)
23 {
24 for(j=2;j>=0;j--)
25 {
26 cout<<a[i][j]<<"t";
27 }
28 cout<<endl;
29 }
30 return 0;
31 }
32 Output;
Enter number :1
Enter number :2
Enter number :3
Enter number :4
Enter number :5
Enter number :6
Enter number :7
Enter number :8
Enter number :9
9 8 7
6 5 4
3 2 1
87. Lab Manual # 8 2-Dimensional Array
79
Que no 4 Transpose ofa Matrix
Write a Program in C++ that Display the Transpose of a Matrix.
8.4.1 Code
9 #include <iostream>
10 using namespace std;
11 int main()
12 {
13 int a[10][10],
14 trans[10][10], r, c, i, j;
15 cout << "Enter rows and columns of matrix: ";
16 cin >> r >>c;
17 cout << endl << "Enter elements of matrix: " << endl;
18 for (i = 0; i < r; ++i)
19 {
20 for (j = 0; j < c; ++j)
21 {
22 cout << "nEnter elements a" << i + 1 << j + 1 << " : ";
23 cin >> a[i][j];
24 }
25 }
26 cout << endl << "Entered Matrix: " << endl;
27 for (i = 0; i < r; ++i)
28 {
29 for (j = 0; j < c; ++j)
30 {
31 cout << " " << a[i][j];
32 if (j == c - 1)
33 cout << endl << endl;
34 }
35 }
36 for (i = 0; i < r; ++i)
37 {
38 for (j = 0; j < c; ++j)
39 {
40 trans[j][i] = a[i][j];
41 }
42 }
43 cout << endl << "Transpose of Matrix: " << endl;
44 for (i = 0; i < c; ++i)
45 {
46 for (j = 0; j < r; ++j)
47 {
48 cout << " " << trans[i][j];
49 if (j == r - 1)
50 cout << endl << endl;
51 }
52 }
53 return 0;
54 }
55 Output;
88. Lab Manual # 8 2-Dimensional Array
80
Enter rows and columns of matrix: 2
3
Enter elements of matrix:
Enter elements a11 : 1
Enter elements a12 : 2
Enter elements a13 : 3
Enter elements a21 : 4
Enter elements a22 : 5
Enter elements a23 : 6
Entered Matrix:
1 2 3
4 5 6
Transpose of Matrix:
1 4
2 5
3 6
89. Lab Manual # 8 2-Dimensional Array
81
8.4 Que no 5 Agent Program
Write a program in C++, which take Agent code (123,258,..) and Traveling expense
(Rs = 5000, 6000,…) of the agent. Find the agent who had spent most money in all,
Display the agent code and amount after searching in 2 D Array.
8.4.1 Code
9 #include<iostream>
10 using namespace std;
11 int main()
12 {
13 int ant[10][2];
14 cout<<”Enter agent code and expensen”<<endl
15 for(int i=0;i<10;i++)
16 {
17 for(int j=0;j<2;j++)
18 {
19 cin>>ant[i][j];
20 }
21 Cout<<endl;
22 }
23 int large=ant[0][1],x=0;
24 for(int i=0;i<10;i++)
25 {
26 for(int j=0;j<2;j++)
27 {
28 if(ant[i][j]>large)
29 {
30 x=i;
31 large=ant[i][j];
32 }
33 }
34 }
35 Cout<<”Code = “ant[x][0]<<endl<<”Expence =”ant[x][1];
36 Return 0;
37 }
91. Lab Manual # 9 Structures
82
9.1 Introduction
A Structure is a collection of simple variables. The Variables in a structure can be of different
types. Some can be int, some can be float, and so on.
The data items in a structure are called the members of the structure.
The structure is a kind of blue print specifying what information is necessary for a single
part.
9.2 Structure Example
struct part
{
int modelnumber;
int partnumber;
float cost;
};
int main ()
{
part part1, part2;
part1.modelnumber =
1111; part1.partnumber =
111; part1.cost = 111.11;
part2.modelnumber = 222;
part2.partnumber = 2222;
part2.cost = 222.222;
cout<<"nModel of Part1 = "<<part1.modelnumber;
cout<<"nPart of part 1 = "<<part1.partnumber;
cout<<"nCost of part1 = "<<part1.cost<<endl;
cout<<"nModel of part2 = "<<part2.modelnumber;
cout<<"nPart of part2 = "<<part2.partnumber;
cout<<"nCost of part2 = "<<part2.cost<<endl;
return 0;
}
9.2.1 Output
Model of Part1 = 1111
Part of part 1 = 111
Cost of part1 = 111.11
Model of part2 = 222
Part of part2 = 2222
Cost of part2 = 222.222
92. Lab Manual # 9 Structures
83
9.2.2 Description
The structure definition serves only as a blueprint for the creation of variables of type
part. It does not itself create any structure variables; that is, it does not set aside any
space in memory or even name any variables. This is unlike the definition of a simple
variable, which does set aside memory.
93. Lab Manual # 9 Structures
84
9.3 Que No 1 Area of rooms
Write a program in C++ that shows the area of 3 room's. Using Structure namely
"distance". Take input of feet & inches from user for variable d1 (feet & inches), assign
variable d2 = {10, 5.25} values. Now add feet and inches of d1 & d2 and store in d3.
Display d1 (feet & inches) d2 (feet & inches) d3 (feet & inches) separately. Put
Condition if d1 & d2 inches increase by 12 it become a foot.
9.3.1 Code
#include<iostream>
using namespace std;
struct Distance
{
int feet;
float inches;
};
int main()
{
Distance d1,d3;
Distance d2= {11, 6.25};
cout<<"n Enter feet:"<<endl;
cin>>d1.feet;
cout<<"Enter inches:"<<endl;
cin>>d1.inches;
d3.inches= d1.inches + d2.inches;
d3.feet=d1.feet+ d2.feet;
if(d3.inches>=12.0)
{
( d3.inches=d3.inches-(12.0);
d3.feet++;
}
cout<< d1.feet<<"---"<<d1.inches<<" + ";
cout<<d2.feet<<"---"<<d2.inches<<" = ";
cout<<d3.feet<<"---"<<d3.inches;
}
94. Lab Manual # 9 Structures
85
9.4 Que No 2 Phone Number Program
A phone number, such as 212- 767-8900, can be thought of as having three parts: the
area code (212), the exchange (767), and the number (8900). Write a program that
uses a structure to store these three parts of a phone number separately. Call the
structure phone. Create two structures variables of type phone. Initialize one, and have
the user input a number for the other one. Then display both numbers. The interchange
might look like this:
9.4.1 Code
#include<iostream>
using namespace std;
struct phone
{
int code,exchange,number;
}
p1,p2;
int main()
{
cout<<"Enter code, exchange and number n"<<endl;
cin>>p1.code>>p1.exchange>>p1.number;
p2.code=92;
p2.exchange=555;
p2.number=8826;
cout<<"p1 = "<<p1.code<<"-"<<p1.exchange<<"-"<<p1.number<<endl;
cout<<"p2 = "<<p2.code<<"-"<<p2.exchange<<"-"<<p2.number<<endl;
}
95. Lab Manual # 9 Structures
86
9.5 Que No 3 Employee record
Create a structure called emp that contains three members,
int id, char name[100], float sal.
Ask the user to fill in data for three employees and then display information
for each employee.
Hint
Variable of struct emp will be array
Use while / for loop to control array
9.5.1 Code
#include<iostream>
using namespace std;
struct emp
{
int id;
char name[100];
float sal;
};
void main ()
{
emp ob1[3];
int c=0;
while(c<3)
{
cout<<"Enter ID of Employee "<<c+1<<" = ";
cin>>ob1[c].id;
cout<<"Enter name of Employee "<<c+1<<" = ";
cin>>ob1[c].name;
cout<<"Enter salary of Employee "<<c+1<<" = ";
cin>>ob1[c].sal;
c++;
cout<<"nn";
}
c = 0;
while (c<3)
{
cout<<"nId of emp "<<c+1<<" = "<<ob1[c].id;
cout<<"nName of emp "<<c+1<<" = "<<ob1[c].name;
cout<<"nSalary ofemp "<<c+1<<" = "<<ob1[c].sal;
c++;
cout<<"nn";
}
}
96. Lab Manual # 9 Structures
87
9.5 Que No 4 Memory size of a structure
Write a program using ―sizeof()‖ function that calculate the size of structure
9.5.1 Code
10 #include<iostream>
11 using namespace std;
12 struct emp
13 {
14 int id;
15 char name[1];
16 float sal;
17 }
18 a;
19 struct phone
20 {
21 int code,exchange,number;
22 }
23 b;
24 struct distance
25 {
26 int feet;
27 float inches;
28 }
29 e;
30 int main()
31 {
32 cout<<sizeof(a)<<endl;
33 cout<<sizeof(b)<<endl;
34 cout<<sizeof(e)<<endl;
35 return 0;
36 }
37 Output;
12
12
8
97. Lab Manual # 9 Structures
88
37.5 Que No 5 Average Age Program
Write a program to calculates the average age and average GPA of a class having
10 students. Also determine the grade of the class and the student with max GPA.
Use a student structure and manipulate it to get the desired result.
37.5.1Code
38 #include<iostream>
39 #include<string>
40 using namespace std;
41 struct student
42 {
43 int regNo;
44 float gpa;
45 }
46 int main()
47 {
48 int n;
49 student std[10];
50 for (n=0; n<10; n++)
51 {
52 cout<<"Enter student Reg#n"<<endl;
53 cin>>std[n].regNo;
54 cout<<"Enter Gpan"<<endl;
55 cin>>std[n].gpa;
56 };
57 for (n=0; n<10; n++)
58 {
59 cout<<std[n].regNo<<endl;
60 cout<<std[n].gpa<<endl;
61 }
98. Lab Manual # 9 Structures
89
61.5 Que No 6 NestedStructure
Write a program that contains nested structure
61.5.1Code
62 #include<iostream>
63 using namespace std;
64 struct Address
65 {
66 char HouseNo[25];
67 char City[25];
68 char PinCode[25];
69 };
70 struct Employee
71 {
72 int Id;
73 char Name[25];
74 float Salary;
75 struct Address Add;
76 };
77 void main()
78 {
79 int i;
80 Employee E;
81 cout << "ntEnter Employee Id : "<<endl;
82 cin >> E.Id;
83 cout << "ntEnter Employee Name : "<<endl;
84 cin >> E.Name;
85 cout << "ntEnter Employee Salary : "<<endl;
86 cin >> E.Salary;
87 cout << "ntEnter Employee House No : "<<endl;
88 cin >> E.Add.HouseNo;
89 cout << "ntEnter Employee City : "<<endl;
90 cin >> E.Add.City;
91 cout << "ntEnter Employee House No : "<<endl;
92 cin >> E.Add.PinCode;
93 cout << "nDetails of Employees"<<endl;
94 cout << "ntEmployee Id : " << E.Id<<endl;
95 cout << "ntEmployee Name : " << E.Name<<endl;
96 cout << "ntEmployee Salary : " << E.Salary<<endl;
97 cout << "ntEmployee House No : " << E.Add.HouseNo<<endl;
98 cout << "ntEmployee City : " << E.Add.City<<endl;
99 cout << "ntEmployee House No : " << E.Add.PinCode<<endl;
100 }
101. Lab Manual # 10 Pointers
92
10.1 Introduction to Pointers
int *myptr ;
myptr is pointer to an integer
10.1.1 Example of Pointers
#include<iostream.h>
void main ()
{
int var1 = 10;
int var2 = 20;
int var3 = 30;
cout<<&var1<<endl
<<&var2<<endl;
int *ptr;
ptr = &var1;
cout<<ptr<<endl;
ptr = &var2;
cout<<ptr<<endl;
}
10.2 Pointer To Arrays
#include<iostream.h>
void main ()
{
int array[5] = {31,54,77,52,93};
for(int j =0; j<5; j++)
{
cout<<array[j]<<endl;
}
}
#include<iostream.h>
void main ()
{
int array[5] = {31,54,77,52,93};
int* ptr;
ptr = array;
for(int j =0; j<5; j++)
{
cout<<*(ptr++)<<endl;
}
}
102. Lab Manual # 10 Pointers
93
10.2 Que No 1 Print the values from array
Write a program that prints the values from an array using pointer variable. The array
is given below
int y [ 10 ]= {6,2,3,12};
#include"iostream"
using namespace std;
int main()
{
int y[10]={6,2,3,12};
int *ptr=y;
for(int i=0;i<4;i++)
{
cout<<*(ptr++)<<endl;
}
}
10.3 Que No 2 Print the values and memory address from an array Write a
program that prints the values from an array using pointer variable. The array is
given below
int y [ 10 ]= {6,2,3,12};
#include<iostream>
using namespace std;
int main()
{
int y[10]={6,2,3,12};
int *ptr=y;
for(int i=0;i<4;i++)
{
cout<<"Value = "<<*(ptr++)<<" Address = "<<ptr<<endl;
}
104. Lab Manual # 10 Pointers
95
10.5 Que No 3 Accessing values by Arithmetic operator
Write a program that displays the values using pointer variable from an array
given below using Arithmetic Increment operator .
int y[5]={22,33,44,55,66};
10.5.1 Code
11 #include<iostream>
12 using namespace std;
13 int main()
14 {
15 int arr[5] = {22,33,44,55,66};
16 int *ptr;
17 int j=0;
18 ptr = arr;
19 while(j < 5)
20 {
21 cout<<*(ptr++)<<endl;
22 j++;
23 }
24 return 0;
25 }
105. Lab Manual # 10 Pointers
96
25.2 Que No 4 Moving in array through pointers
Write a program that display only 6th
element of an array given below using pointers.
int y [10] ={11,22,33, 44,55,66,77,88,99,110}
25.2.1 Code
26 #include<iostream>
27 using namespace std;
28 int main ()
29 {
30 int array[10]={11,22,33,44,55,66,77,88,99,110};
31 int *ptr;
32 ptr=&array[5];
33 cout<<"The 6th Element of array is : "<<*ptr;
34 return 0;
35 }
35.2 Pointer Comparison
if ( y1 > y2 )
if ( y1 >= y2 )
if ( y1 == y2 )
if ( *y1 > *y2 )
106. Lab Manual # 10 Pointers
97
35.2.1 Pointer Comparison Example
int y [10]={11,22,33,44,55,66,77,88,99,110} ;
int *y1, *y2;
y1= &y[0];
y2= &y[3];;
cout <<"n Y1= "<<*y1;
cout <<"n Y2= "<<*y2;
if (*y1 < *y2)
cout<<"nY1 is Smaller"<<endl;
else
cout<<"nY2 is smaller"<<endl;
35.3 Que No 4 QuestionMax
Write a program that take two numbers an input from user. Find the maximum from both
of them using the dereference pointer comparison
35.3.1 Code
36 #include"iostream"
37 using namespace std;
38
39 main()
40 {
41 int *p1=new int,*p2=new int;
42 cout<<"Enter two numbers n";
43 cin>>*p1>>*p2;
44 if(*p1>*p2)
45 cout<<*p1<<" is Larger";
46 else
47 cout<<*p2<<" is Larger";
48 }
49
107. Lab Manual # 10 Pointers
98
49.2 Pointer to functions
main( )
{
int a = 10, b = 20 ;
swapv ( a, b ) ;
cout<<”na =”<<a<<” b= ”<<b;
}
swapv ( int x, int y )
{
int t ; t
= x ; x
= y ; y
= t ;
cout<<”nx = ”<<x<<” y = ”<<y;
}
The above given code is swapping the values without pointers.
49.2.1 Que No 5 Swap the same values using pointers.
50 #include <iostream>
51 using namespace std;
52 void swap(int *x, int *y)
53 {
54 int temp;
55 temp = *x;
56 *x = *y;
57 *y = temp;
58 }
59 int main()
60 {
61 int a=10;
62 int b=20;
63 cout << "Before Swapping : a =" << a << ", b =" << b << "n"; swap(&a, &b);
64 cout << "After Swapping : a =" << a << ", b =" << b << "n"; return 0;
65 }
108. Lab Manual # 10 Pointers
99
65.2 Que No 6 Returning more than one values from a function
Write a program that gets the radius from user, pass radius to a function areaperi() and
function areaperi() returns ―area‖ and ―perimeter‖ by reference
Using a call by reference intelligently we can make a function return more than
one value at a time, which is not possible ordinarily.
65.2.1 Code
66 #include<iostream>
67 using namespace std;
68 void areaperi(int r, float *peri, float *a)
69 {
70 *peri = 2*3.14*r;
71 *a = 3.14*r*r;
72 }
73 int main()
74 {
75 int radius;
76 float area, perimeter;
77 cout<<"Enter the radius : ";
78 cin>>radius;
79 areaperi(radius, &perimeter, &area);
80 cout<<"Perimeter is : "<<perimeter<<endl;
81 cout<<"Area is : "<<area<<endl;
82 return 0;
83 }