C Programs
C Programs
C Programs
#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
Entering Block1
Enter Block2
#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:
Output:
Enter the roll no 1
Roll No 1
Total 80