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

C++ Lab Programs-1

The document is an index and detailed guide for various C++ programming exercises, covering topics such as checking positive/negative numbers, leap years, and calculating the area of shapes using function and operator overloading. Each section includes an aim, algorithm, and sample code with successful execution results. It serves as a comprehensive resource for learning fundamental programming concepts in C++.

Uploaded by

shettykrithi424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C++ Lab Programs-1

The document is an index and detailed guide for various C++ programming exercises, covering topics such as checking positive/negative numbers, leap years, and calculating the area of shapes using function and operator overloading. Each section includes an aim, algorithm, and sample code with successful execution results. It serves as a comprehensive resource for learning fundamental programming concepts in C++.

Uploaded by

shettykrithi424
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Index

S.No Program Page No

1 Find the given number is Positive or Negative 1

2 Check the given Year is a Leap Year or Not 3

3 Find the Biggest of Three Numbers 5

4 Display Week Day using Switch Case Statement 8

5 Displaying N numbers using For Loop 11

6 Sum of N numbers using For Loop 14

7 Displaying Alphabets Using ASCII Values 15

8 Displaying Fibonacci Numbers using FOR Loop 17

Find the Power Value of a given number using


9 19
Inline Function

Check the given Character is Vowel or Not using


10 21
Friend Function

11 Function Overloading 24

12 Operator Overloading 26

13 Single Inheritance 28

14 Multilevel Inheritance 31

15 Multiple Inheritance 34

16 File Handling using Class in C++ 37


1. Find the given number is Positive or Negative

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 2: Read the number

Step 3: If the number >0, print positive

If the number<0 print negative

Otherwise print zero

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 2: Read the Year

Step 3: If the Year mod 4==0 then print Leap Year

Else print Not a Leap Year

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:

if(number1>number2 && number1>number3), then print number1 is the biggest number.


if(number2>number1 && number2>number3), then print number2 is the biggest number
else print number3 is the biggest number

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;

if(num1>=num2 && num1>=num3)


{
cout<<"N1 is Biggest Number"<<num1;
}
else if(num2>=num1 && num2>=num3)
{
cout<<"N2 is Biggest Number"<<num2;
}
else
{
cout<<"N3 is Biggest Number"<<num3;
}
getch();
}

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

Aim : Write a Program to display N numbers using C++

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();

cout<<"Enter the Number to Display";


cin>>n;
for(i=0;i<=n;i++)
{
cout<<i<<endl;
}
getch();
}
Output:

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();

cout<<"Enter the Number to Display:";


cin>>n;
for(i=0;i<=n;i++)
{
Sum=sum+i;
}

cout<<"Sum of the Input Number: "<<n<<"is :"<<sum;


getch();

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.

/* Display Alphabets Using ASCII Values */


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
char c,i=65;
cout<<"*********************************"<<endl;
cout << "Alphabets Using ASCII Value " << endl;
cout<<"*********************************"<<endl;
for (c = 97; c <= 122; c++)
{
cout << c << "<----> ";
cout << i<<" ";
i=i++;
}
getch();
}
15
Result:
The above program has been successfully compiled and executed.

16
8. Displaying Fibonacci Numbers using FOR Loop

Aim : Write a Program to display Fibonacci Numbers using FOR Loop.


Algorithm
Step 1: Start.
Step 2: Declare variables n1,n2,n3, i, number Initialize variable n1=0, n2=1. Read Variable
number.
Step 3: Using For Loop Check below condition and Fibonacci Numbers as below:
for(i=2;i<number;++i)
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
Step 7: If the condition is false, then end FOR loop.
Step 8: Stop.

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

for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed


{
n3=n1+n2;
cout<<n3<<" ";
n1=n2;
n2=n3;
}
getch();
}
Output:

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);

inline int display_power(int base,int power)


{
int result=pow(base,power);
cout<<"The Power of Base : "<<base<<" & Power: "<<power<<"="<<result;
return 0;
}

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);

cout<<"Area of the Circle is :"<<circle<<endl;


}

void area(int l, int w)


{
float rectangle=(l*w);

cout<<"Area of the Rectangle is :"<<rectangle<<endl;


}

void main()
{
clrscr();
float r,l,w;
cout<<"Enter the Radious of the Circle :";
cin>>r;

cout<<"Enter the Length of the Rectangle :";


cin>>l;
cout<<"Enter the Width of the Rectangle: ";
cin>>w;
area(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:

i). void get_radius() - to get the radius input


ii). void operator *() - to Calculate and display the area of the circle using Operator
Overloading concept.

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 operator *() //Syntax for Operator Overloading


{
area=3.14 * (radius*radius);
cout<<"Area of the Circle is:"<<area;
}
};

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;
}
};

class department_info:public student_info


{
public:
char dept[20];

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;

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);
}

void display()
{
cout<<"--------------------------------------------"<<endl;
cout<<"Student Name is :";
puts(name);
cout<<"Student Register Number:"<<regno<<endl;
cout<<"Department Name:";
puts(dept);

if((tamil>=35) && (english>=35) && (maths>=35) && (commerce>=35) && (economics>=35))


{
cout<<"Your are Pass....Congrats"<<endl;
}
else
{
cout<<"Your are Fail"<<endl;
}
cout<<"Total Mark is:"<<total;
}

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;

cout<< "Enter the English Mark: ";

34
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);
}

};

class result: public student_info, public mark


{
public:
void display()
{
cout<<"------------------------------------------";
cout<<endl;
cout<<"Student Name is :";
puts(name);
cout<<"Student Register Number:"<<regno<<endl;
cout<<"Department Name:";
puts(dept);
cout<<"Total Mark is:"<<total;
cout<<endl;
}

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

You might also like