C Programs

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1.

C++ program to find the factorial of a given number

#include<iostream.h>
#include<conio.h>
int fact(int n);
int main()
{
int n;
cout<<”Enter the number”<<endl;
cin>>n;
cout<<”The factorial of a given Number is”<<fact(n)<<endl;
getch();
return 0;
}
int fact(int n)
{
if (n==0||n==1)
return 1;
else
return n* fact(n-1);
}

Output:
Enter the number 5
The factorial of a given Number is 720
2.Write a c++ program to implement the constructor and destructor
concept.

CODING:

//Implementation of destructors
#include<iostream.h>
#include<conio.h>
int count=0;
class alpha
{
public:
alpha()
{
count++;
cout<<"\n\t Number of objects created: "<<count;
}
~alpha()
{
cout<<"\n\t "<<count<<"th object is destroyed";
count--;
}};

void main()
{
clrscr();
cout<<"\n\nEntering main function\n";
alpha a1,a2,a3,a4;
{
cout<<"\n\nEntering Block1 \n";
alpha a5;
}
{
cout<<"\n\nEnter Block2 \n";
alpha a6;
}
cout<<"\nRe-entering main\n";
getch();
}
OUTPUT:
Entering main function

Number of objects created: 1


Number of objects created: 2
Number of objects created: 3
Number of objects created: 4

Entering Block1

Number of objects created: 5


5th object is destroyed

Enter Block2

Number of objects created: 5


5th object is destroyed
Re-entering main
4th object is destroyed
3th object is destroyed
2th object is destroyed
1th object is destroyed
3.Example to illustrate the concept of C++ function overloading

#include <iostream.h>
using namespace std;
void display ( ) //function with no arguments
{
int a = 3;
cout << a << endl;
}
void display (int a ) //function with one integer argument
{
cout << a << endl;
}
void display (double a ) //function with one floating argument
{
cout << a << endl;
}
void display(int a, float b) //function with one integer and one floating
arguments
{
cout<< a << " , " << b << endl;
}

int main()
{
display(); //function call with no arguments
display(5); //function call with one integer argument
display(2.3); //function call with one floating argument
display(5,4.0); //function call with one integer and one floating arguments
return 0;
} //end of program

Output

3
5
2.3
5,4
4. To develop a c++ program to implement Exception Handling using
try, throw and catch - keywords.

CODING:

#include<iostream.h>
int main()
{
int x,a,b;
cout<<"\nEnter the values of a & b:"<<endl;
cin>>a>>b;
x=a-b;
try
{
if(x!=0)
cout<<"\n\tThe value of (a/x) is "<<(a/x)<<endl;
else
throw 0;
}
catch(int i)
{
cout<<"\n\tThe exception caught is "<<i<<"\n";
}
return 0;
}
OUTPUT:

Enter the values of a & b:


12 6

The value of (a/x) is 2


Press any key to continue

Enter the values of a & b:


5
5

The exception caught is 0


Press any key to continue
5.C++ Program to implement multiple inheritance.
#include<iostream.h>
#include<conio.h>
class Student
{
protected:
int rno, m1, m2;
public:
void getdata()
{
cout<<"Enter the roll no:";
cin>>rno;
cout<<"Enter the two subjects marks:";
cin>>m1>>m2;
}
};
class sports
{
protected:
int sm;
public:
void getsm()
{
cout<<"\nEnter the sports mark:";
cin>>sm;
}
};
class statement: public Student, public sports
{
int tot,avg;
public:
void display()
{
tot = (m1+m2+sm);
avg = tot/3;
cout<<"\nRoll No:"<<rno;
cout<<"\nTotal:"<<avg;
}
};
int main()
{
statement S;
S.getdata();
S.getsm();
S.display();
getch();
return 0;
}

Output:
Enter the roll no 1

Enter the two subjects marks 89 78

Enter the sports mark: 75

Roll No 1

Total 80

You might also like