C++ Lab Programs-1
C++ Lab Programs-1
11 Function Overloading 24
12 Operator Overloading 26
13 Single Inheritance 28
14 Multilevel Inheritance 31
15 Multiple Inheritance 34
Aim : To write a C++ program to check whether a number is positive, negative or zero
using if else statement.
ALGORITHM
Step 1:Start
Step 4: Stop.
1
/* program for checking positive negative or zero */
#include<iostream.h>
#include<conio.h>
void main ()
{
int num;
cout << "Enter the number to be checked : ";
cin >> num;
if (num >= 0)
cout << num << " is a positive number.";
else
cout << num << " is a negative number.";
getch();
Output:
Result:
The above program has been successfully compiled and executed.
2
2. Check the given Year is a Leap Year or Not
Aim : To write a C++ program to check whether a given year is leap year or not using if
else statement.
ALGORITHM
Step 1:Start
Step 4: Stop.
3
/* program for checking Leap Year or Not */
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int year;
clrscr();
cout << "Enter the year in yyyy format : ";
cin >> year;
if (year % 4 == 0)
cout << year << " is a leap year";
else
cout << year << " is not a leap year";
getch();
}
Output Screen:
Result:
The above program has been successfully compiled and executed.
4
3. Find the Biggest of Three Numbers
Aim : Write a Program to find the biggest of Three Numbers.
Algorithm
Step 1: Start.
Step 2: Read the Three input numbers number1,number2,number3.
Step 3: Check the following conditions:
Step 4: Stop.
5
/* program for find the biggest of Three Numbers using if-else.*/
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int num1 ,num2, num3;
cout << "Enter the First Number: ";
cin >> n1;
cout<<endl;
cout << "Enter the Second Number: ";
cin >> n2;
cout<<endl;
cout << "Enter the Third Number: ";
cin >>n3;
cout<<endl;
6
Output:
Result:
The above program has been successfully compiled and executed.
7
4. Display Week Day using Switch Case Statement
Aim : Write a Program to Display the week day using Switch Case in C++
Algorithm
Step 1: Start.
Step 2: Enter number of week's day (1-7):
Step 3: Use the Switch Case to display the output based on the given input value.
Step 4: Stop.
8
/* Switch Case Program */
#include<iostream.h>
#include<conio.h>
void main()
{
int dow=2;
cout<<"Enter number of week's day (1-7): ";
cin>>dow;
switch(dow)
{
case 1 : cout<<"\nSunday";
break;
case 2 : cout<<"\nMonday";
break;
case 3 : cout<<"\nTuesday";
break;
case 4 : cout<<"\nWednesday";
break;
case 5 : cout<<"\nThursday";
break;
case 6 : cout<<"\nFriday";
break;
case 7 : cout<<"\nSaturday";
break;
default : cout<<"\nWrong number of day";
break;
}
getch();
9
Output:
Result:
The above program has been successfully compiled and executed.
10
5. Displaying N numbers using For Loop
Algorithm
Step 1: Start.
Step 2: Read the input number and Initialize variable i=0.
Step 3: Check FOR condition.
Step 4: If the condition is true, then Print value of i.
Step 7: If the condition is false, then end for loop.
Step 8: Stop.
11
/* program for Displaying N numbers using FOR LOOP */
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
Result:
The above program has been successfully compiled and executed.
12
6. Sum of N numbers using For Loop
Aim : Write a Program to sum the N numbers using for loop in C++
Algorithm
Step 1: Start.
Step 2: Read the input number and Initialize variable sum=0, i=0.
Step 3: Check FOR condition.
Step 4: If the condition is true, then compute & print the value of sum=sum+i.
Step 7: If the condition is false, then end for loop.
Step 8: Stop.
13
/* program for Sum of N numbers using FOR LOOP */
#include<iostream.h>
#include<conio.h>
void main()
{
int i,n,sum;
sum=0;
clrscr();
Result:
The above program has been successfully compiled and executed.
14
7. Displaying Alphabets Using ASCII Values
Aim : Write a Program to display Alphabets Using ASCII Values using For Loop in C++.
Algorithm
Step 1: Start.
Step 2: Read the input number and Initialize ASCII Value using Char data type.
Step 3: Check FOR condition.
Step 4: If the condition is true, then compute & print the Alphabets.
Step 7: If the condition is false, then end for loop.
Step 8: Stop.
16
8. Displaying Fibonacci Numbers using FOR Loop
17
#include<iostream.h>
#include<conio.h>
void main ()
{
clrscr( );
int n1=0,n2=1,n3,i,number;
cout<<"Display Fib Series using FOR LOOP"<<endl;
cout<<"Enter the number of elements: ";
cin>>number;
cout<<n1<<" "<<n2<<" "; //printing 0 and 1
Result:
The above program has been successfully compiled and executed.
18
9. Find the Power Value of a given number using Inline Function
Aim : To write a C++ to find the power of a given number using
Algorithm
Step 1: Start.
Step 2: Define the variables for Base & Power.
Step 3: Create a Inline Function using key word Inline to display Power of a given number.
Step 4: Stop.
#include<iostream.h>
#include<conio.h>
#include<math.h>
int display_power(int base, int power);
void main()
{
clrscr();
int base,power;
cout<<"Enter the Base Value :";
cin>>base;
cout<<"Enter the Power Value :";
cin>>power;
display_power(base,power);
getch();
}
19
Result:
The above program has been successfully compiled and executed.
20
10. Friend Function
Aim : Write a C++ program to Check the given Character is a Vowel or Not using Friend
Function
ALGORITHM
Step 1: Start
Step 2: Read the Character
Step 3: Use the IF ELSE statement and OR Logical Operator to check the given Character is a
Vowel or not
Step 4 : if (lowercase_vowel || uppercase_vowel) is True Print Given Character is a Vowel
Else Print Given Character is Not a Vowel
Step 4: Stop.
21
/* Friend Function Program */
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
public:
friend void result();
};
void result()
{
char c;
int lowercase_vowel, uppercase_vowel;
cout<<"Enter an alphabet: ";
cin>>c;
lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
if (lowercase_vowel || uppercase_vowel)
cout<<" Given input: "<<c<<" is a vowel";
else
cout<<"Given input: "<<c <<" is a consonant";
}
void main()
{
clrscr();
//student s;
result();
getch();
}
22
Output:
Result:
The above program has been successfully compiled and executed.
23
11. Function Overloading
Aim : Write a C++ program to Area of Circle and Rectangle using Function Overloading.
Algorithm
Step 1: Start.
Step 2: Get the value for radius, length, & width.
Step 3: Create a area()function and pass the parameters radius, length, & width to calucate
the area of Circle and Rectangle using Function Overloading technique.
Step 4 : Display the area of Circle and Rectangle
Step 5: Stop the Program.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void area(int a)
{
float circle=(3.14*a*a);
void main()
{
clrscr();
float r,l,w;
cout<<"Enter the Radious of the Circle :";
cin>>r;
24
area(l,w);
getch();
}
Result:
The above program has been successfully compiled and executed.
25
12. Operator Overloading
Aim : Write a program to Calulate the Area of the Circle using Operator Overloading.
Algorithm:
Step 1: Start.
Step 2: Create Class Circle with the two variables radius and area using Float Data Type.
Step 2: Create the following Two functions inside the Class Circle as follows:
Step 3: Object Initialization and function calling using the dot operator.
Step 4: Stop.
26
#include<iostream.h>
#include<conio.h>
class Circle
{
public:
float radius, area;
void get_radius()
{
cout<<"Enter the Radius of the Circle :";
cin>>radius;
}
void main()
{
clrscr();
Circle Area;
Area.get_radius();
*Area; // * is the Operator and Area is the Object Name of Class Rectangle
getch();
}
Output:
Result:
The above program has been successfully compiled and executed.
27
13. Single Inheritance
Aim : Write a Single Inheritance program to display the Student’s Exam Result as follows:
i. Get the Student Information such as Name, Register No, Marks in Class student_info
ii. Compute Total Mark and Exam Result in Derived Class result
Source Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student_info
{
public:
char name[100], regno [20],dept[20];
int tamil,english,maths,commerce,economics,total;
void get_info()
{
cout<< "Enter the Student Name: ";
gets(name);
cout<< "Enter the Student Register No: "; cin>>regno;
cout<< "Enter the Department Name: ";
gets(dept);
}
void get_mark()
{
cout<< "Enter the Tamil Mark: ";
cin>>tamil;
cout<< "Enter the English Mark: ";
cin>>english;
cout<< "Enter the Maths Mark: ";
cin>>maths;
cout<< "Enter the Commerce Mark: ";
cin>>commerce;
cout<< "Enter the Economics Mark: ";
cin>>economics;
total=(tamil+english+maths+commerce+economics);
}
};
28
class result: public student_info
{
public:
void exam_result()
{
if((tamil>=35) && (english>=35) && (maths>=35) && (commerce>=35) &&
(economics>=35))
{
cout<<"Your are Pass....Congrats";
}
else
{
cout<<"Your are Fail";
}
cout<<endl;
cout<<"------------------------------------------"<<endl;
cout<<"Total Mark is:"<<total;
cout<<endl;
}
};
void main()
{
clrscr();
result r;
r.get_info();
r.get_mark();
r.exam_result();
getch();
}
29
Output:
Result:
The above program has been successfully compiled and executed.
30
14. Multilevel Inheritance
Aim : Write a Multilevel Inheritance program to display the Student’s Exam Result as follows:
iii. Get the Student Name & Regisgter Number in Class student_info
iv. Get the Student Department’s Information in Derived Class department_info
v. Get the Student's Exam Marks Information then calculate & display Result & Total
Mark in Derived Class mark
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student_info
{
public:
char name[100], regno [20];
void get_info()
{
cout<< "Enter the Student Name: ";
gets(name);
cout<< "Enter the Student Register No: "; cin>>regno;
}
};
void get_department()
{
cout<< "Enter the Department Name: ";
gets(dept);
//cout<<endl;
}
};
31
class mark : public department_info
{
public:
int tamil,english,maths,commerce,economics,total;
void get_mark()
{
cout<< "Enter the Tamil Mark: ";
cin>>tamil;
void display()
{
cout<<"--------------------------------------------"<<endl;
cout<<"Student Name is :";
puts(name);
cout<<"Student Register Number:"<<regno<<endl;
cout<<"Department Name:";
puts(dept);
32
};
void main()
{
clrscr();
mark d;
d.get_info();
d.get_department();
d.get_mark();
d.display();
getch();
}
Output:
Result:
The above program has been successfully compiled and executed.
33
15. Multiple Inheritance
Aim : Write a Multiple Inheritance program to display the Student’s Exam Result as follows:
i. Get the Student Name & Regisgter Number, Department Name in Class student_info
ii. Get the Student's Exam Marks Information then calculate Total Mark in Class mark
iii. Compute and Display Student’s Exam Result in Mutilevel Inhertied Derived Class mark
Source Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class student_info
{
public:
char name[100], regno [20],dept[20];
void get_info()
{
cout<< "Enter the Student Name: ";
gets(name);
cout<< "Enter the Student Register No: "; cin>>regno;
cout<< "Enter the Department Name: ";
gets(dept);
}
};
class mark
{
public:
int tamil,english,maths,commerce,economics,total;
void get_mark()
{
cout<< "Enter the Tamil Mark: ";
cin>>tamil;
34
cin>>english;
};
void results()
{
if((tamil>=35) && (english>=35) && (maths>=35) && (commerce>=35) && (economics>=35))
{
cout<<"Your are Pass....Congrats";
}
else
{
cout<<"Your are Fail";
}
35
cout<<endl;
cout<<"------------------------------------------";
}
};
void main()
{
clrscr();
result d;
d.get_info();
d.get_mark();
d.display();
d.results();
getch();
}
Output:
Result:
The above program has been successfully compiled and executed.
36
16. File Handling using Class in C++
Aim: Write a program to create a File that records three Students Name and Register Number
and Display its content using Class in C++
Algorithm:
i. Get the Student Name & Regisgter Number using getdata() function in Class student
ii. Save the received Student's Information into file “student.txt” using getdata() function in
Class student.
Source Code:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
class student
{
public:
char name[30], Reg_No[30],ch;
int i;
void getdata()
{
ofstream fout("Student.txt");
// link student file to output stream object by default
for(i=0;i<3;i++)
{
cout<<"\n Enter Name :\t";
cin.get(name,30);
cout<<"Enter Reg_No: \t";
cin>>Reg_No;
cin.get(ch);
fout<<name<<'\n'<<Reg_No<<'\n';
}
fout.close();
37
}
void displaydata()
{
// read from file
ifstream fin("student.txt");
fin.seekg(0); // to bring the pointer to begining
cout<<endl;
for(i=0;i< 3;i++)
{
fin.get(name,30); fin.get(ch);
fin>>Reg_No;
fin.get(ch);
cout<<"student name:\t"<<name;
cout<<"\t Reg_No:"<<Reg_No<<'\n';
}
fin.close();
}
};
void main()
{
clrscr();
student s;
s.getdata();
s.displaydata();
getch();
}
Result:
The above program has been successfully compiled and executed.
38
Output:
Result:
The above program has been successfully compiled and executed.
39