You are on page 1of 7

FUNDAMENTALS OF PROGRAMMING IN C++ LAB MANUAL - ANAND

LAB CONTENTS
Compilation process of C++
Displaying message using cout
Declaring and initializing variables with different data types
Declaring constant identifiers and use in the program
Assigning values to variables
Accepting values from users through cin
Practicing operators and expressions
Practicing control structures (sequential, if, if...else, nested if... else, switch, for loop, while loop, do while loop and
Nested loops)
Arrays and functions
Files

Compilation process of C++


F1=Help; Ctrl F1= Context sensitive Help; F2=Save; F3=Load; F10cc=Compile; F10rr=Run; Alt F5=Output
Window;
Displaying message using cout
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
cout<<"Hai, Welccome to C++ Lab";
getch();
}
Declaring and initializing variables with different data types
#include<iostream.h>
#include<conio.h>
int a=4,b=8,c=3;
float d=3.2,e=8.52;
char f='m',g='n';
void main()
{ clrscr();
cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;
cout<<"d="<<d<<" e="<<e<<endl;
cout<<"f="<<f<<" g="<<g;
getch();
}
Declaring constant identifiers and use in the program
#include<iostream.h>
#include<conio.h>
#define N 20
#define PI 3.14
1

#define message "Welcome friend"


const t=10;
const f=2.34;
const char a='t';
const char* name="Anand";
void main()
{ clrscr();
cout<<"N="<<N<<"
PI="<<PI<<"
message="<<message<<endl;
cout<<"t="<<t<<" f="<<f<<" a="<<a<<"
name="<<name<<endl;
getch();
}
Assigning values to variables
#include<iostream.h>
#include<conio.h>
int a,b,c;
float d,e;
char f,g;
void main()
{ clrscr();
a=4;b=c=3;
d=3.2;e=7.1;
f='f';g='k';
cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;
cout<<"d="<<d<<" e="<<e<<endl;
cout<<"f="<<f<<" g="<<g;
getch();
}
Accepting values from users through cin

#include<iostream.h>
#include<conio.h>
int age;
char name[30];
char sex;
float height;
void main()
{ clrscr();
cout<<"Enter your name,age,sex and
height"<<endl;
cin>>name>>age>>sex>>height;
cout<<endl;
cout<<"Your name is "<<name<<endl;
cout<<"Your age is "<<age<<endl;
cout<<"Your sex is "<<sex<<endl;
cout<<"Your height is "<<height<<" feet"<<endl;
cout<<"THANK YOU";
getch();
}

Practicing operators and expressions


#include<iostream.h>
#include<conio.h>
int a=10,b=5;
float x,f;
void main()
{ clrscr();
f=1.2;
x=2+a*b-5/2;
cout<<"x="<<x<<endl;
x=2+a*b-(float)5/2;
cout<<"x="<<x<<endl;
x=2+a*b-5.0/2;
cout<<"x="<<x<<endl;
getch();
}
Practicing control structures (sequential, if, if...else, nested if... else, switch, for loop, while loop, do while
loop and Nested loops)

#include<iostream.h>
#include<conio.h>
int o,f,s,i,j;
void main()
{ clrscr();
do
{ cout<<endl;
cout<<"CONTROL STRUCTURES\n\n";
cout<<"1.Sequential\n";
cout<<"2.If\n";
cout<<"3.If else\n";
cout<<"4.Nested If else\n";
cout<<"5.Switch\n";
cout<<"6.For loop\n";
cout<<"7.While loop\n";
cout<<"8.Do while loop\n";
cout<<"9.Nested loops\n";
cout<<"10.Exit\n";
cout<<"Enter your option: ";
cin>>o;
switch(o)
{ case 1: cout<<"Example for sequential execution"<<endl;
cout<<"One\n";
cout<<"Two\n";
cout<<"Three\n";
break;
case 2: cout<<"Example for if statement"<<endl;
cout<<"Enter first number ";
cin>>f;
cout<<"Enter second number ";
cin>>s;
if(f>s) cout<<f<<" is big\n";
if(s>f) cout<<s<<" is big\n";
break;
case 3: cout<<"Example for if else statement"<<endl;
cout<<"Enter first number ";
cin>>f;
cout<<"Enter second number ";
cin>>s;
if(f>s) cout<<f<<" is big\n";
else cout<<s<<" is big\n";
break;
case 4: cout<<"Example for nested if else\n";
cout<<"Enter first number:";
cin>>f;
cout<<"Enter second number:";
4

cin>>s;
if(f>s) cout<<f<<" is big";
else if(s>f) cout<<s<<" is big";
else cout<<"Both are same";
break;
case 5: cout<<"Example for switch\n";
cout<<"This program has switch statement within
do loop\n";
cout<<"So, this program itself serves as an
example for switch\n";
break;
case 6: cout<<"Exanple for for loop\n";
for(i=1;i<=10;++i)
cout<<i<<" ";
cout<<endl;
for(i=10;i>=1;--i)
cout<<i<<" ";
break;
case 7: cout<<"Exanple for while loop\n";
i=1;
while(i<=10)
{cout<<i<<" ";++i;}
cout<<endl;
i=10;
while(i>=1)
{cout<<i<<" ";--i;}
break;
case 8: cout<<"Example for do loop\n";
cout<<"This program has switch statement within
do loop\n";
cout<<"So, this program itself serves as an
example for do loop\n";
break;
case 9: cout<<"Example for nested loops\n";
for(i=1;i<=5;++i)
{ cout<<"i="<<i<<" ";
for(j=1;j<=i;++j)
cout<<"j="<<j<<" ";
cout<<endl;
}
break;
case 10: return;
default: cout<<"Please choose between 1 and 10 only";
}
5

}while(1);
}
Arrays and functions
//Matrix Addition
#include<iostream.h>
#include<conio.h>
int a[10][10],b[10][10],aplusb[10][10],r1,r2,r3,c1,c2,c3,i,j;
void readmatrix(int &r,int &c,int a[][10])
{ cout<<"Enter matrix dimension\n";
cin>>r>>c;
cout<<"Enter matrix elements\n";
for(i=1;i<=r;++i)
for(j=1;j<=c;++j)
cin>>a[i][j];
}
void addmatrices()
{
if((r1!=r2)||(c1!=c2))
{cout<<"Matrix addition not possible";getch();return;}
for(i=1;i<=r1;++i)
for(j=1;j<=c1;++j)
aplusb[i][j]=a[i][j]+b[i][j];
r3=r1;c3=c1;
}
void printmatrix(int r,int c,int aplusb[][10])
{ for(i=1;i<=r;++i)
{for(j=1;j<=c;++j)
cout<<aplusb[i][j]<<" ";
cout<<"\n";
}
}
void main()
{ clrscr();
readmatrix(r1,c1,a);
readmatrix(r2,c2,b);
addmatrices();
cout<<"The resultant matrix is \n";
printmatrix(r3,c3,aplusb);
getch();
}

Exercises
6

For each of the problems below, develop a C++ program.


1. Receive a number and determine whether it is odd or even.
2. Obtain two numbers from the keyboard, and determine and display which (if either) is the larger
of the two numbers.
3. Receive 3 numbers and display them in ascending order from smallest to largest.
4. Add the numbers from 1 to 10 and display the sum.
5. Add the even numbers between 0 and any positive integer number given by the user.
6. Find the average of two numbers given by the user.
7. Find the average, maximum, minimum, and sum of three numbers given by the user.
8. Find the area of a circle where the radius is provided by the user.
9. Swap the contents of two variables using a third variable.
10. Swap the content of two variables without using a third variable.
11. Read 10 integers from the keyboard in the range 0 -100, and count how many of them are larger
than 50, and display this result.
12. Take an integer from the user and display the factorial of that number
13. A prime number is an integer greater than one and divisible only by itself and one. The first seven
prime numbers are 2, 3, 5, 7, 11, 13, and 17. Write a program that displays all the prime numbers
between 1 and 100.
14. Write a C++ application that can compute the letter grade of a student after accepting the student's
mid and final mark. The program should only accept mid result [0-40] and final [0- 60]. If the
data entered violates this rule, the program should display that the user should enter the mark in
the specified range. The program is also expected to run until the user refuses to continue.
15. Develop a calculator program that computes and displays the result of a single requested
operation. E.g. if the input is
15 * 20, then the program should display 15 * 20 equals 300 If the operator is not legal, as in the
following example
24 ~ 25 then the program displays ~ is unrecognized operator. As a final example, if the
denominator for a division is 0, as in the following input: 23/0 then the program should display
the following: 23 / 0 can't be computed: denominator is 0.
16. Write a function called isEven() that uses the remainder operator(%) to determine whether an
integer is even or not.
17. Modify our calculator (problem 15) using four functions sum(),product(),quotient() and
difference().
18. Write a C++ program that accepts 10 integers from the user and finally displays the smallest value
and the largest value.
19. Write a C++ program for linear search.
20. Write a C++ program for Selection sort.
21. Write a program which reads two matrix and multiply them if possible.
22. Write a C++ program that accepts a word from the user and then displays the word after reversing
it.
23. Write a C++ program to generate Febinocii Series.
24. Roots of a quadratic equation ax2+bx+c=0 can be computed by using the formula
b b24 ac
x=
. Write a c++ program to compute roots of quadratic equation.
2a
25. If you add diagonal elements in a square matrix, you will get trace of that matrix. Write a C++
program to find trace of a square matrix.
***

You might also like