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

EX NO:8

DATE :24.04.2024

EXCEPTIONS HANDLING

AIM:
1. Write a C++ program to calculate theSquare root of a given number. EnsureThat your
program handles exceptional Cases such as taking the square root of a Negative
number gracefully using exceptionHandling.
2. Write a C++ program to determine if a Person is eligible to vote based on theirAge.
The program should prompt the user To enter their age. If the entered age isLess than
18, the program should throw a Std::invalid_argument exception with theMessage
“Error: You must be 18 or older to Vote.”.However, for ages between 16 and18, the
program should display a warning Message saying “You are not yet eligibleTo vote,
but you can pre-register to voteOnce you turn 18.”. For ages 18 or older, theProgram
should display the message “You Are eligible to vote!”.
3. Write a C++ program that calculates theRatio of each element in an array to theSum
of all elements in the array. Handle theCase where the sum of elements is zero, And
output an error message instead of Attempting division

PROGRAM :
A)

#include<iostream>
Using namespace std;
Class per
{
Public:
Double sqroot(double sq)
{
If(sq<0)
Throw “square root of the neagative can’t be calculated”;
Else
Return (sq*sq);
}
Int div(int n,int d)
{
If(d==0)
Throw “Denominator can’t be zero “;
Else
Return (n/d);
}
};
Int main()
{
Per I;
Double num,sq;
Int n,d,r;
Try
{
Cout<<”enter the number to find the square root :”;
Cin>>num;
Sq=i.sqroot(num);
Cout<<”square root is “<<sq<<endl;
Cout<<”enter the numerator and denominator :”;
Cin>>n>>d;
R=i.div(n,d);
Cout<<”answer is “<<r<<endl;
}
Catch(const char *msg)
{
Cout<<msg;
}
Return 0;
}
OUTPUT 1 :
Enter the number to find the square root :-3
Square root of the neagative can’t be calculated

B)

#include<iostream>
Using namespace std;
Class voter
{
Private:
String name;
Int age;
Public:
Void setname(string a)
{
Name=a;
}
Void setage(int b)
{
If(b>=16&&b<18)
Throw “You are not yet eligible to vote, but you can pre-register to vote once you turn 18”;
If(b<16)
Throw “std::invalid_argument exception with the message, Error: You must be 18 or older
to vote”;
Else
Cout<<”You are eligible to vote!”;
}
};
Int main()
{
Voter p1;
String na;
Int ag;
Try
{
Cout<<”Enter the name : “;
Cin>>na;
P1.setname(na);
Cout<<”Enter the age : “;
Cin>>ag;
P1.setage(ag);
}
Catch(const char *msg)
{
Cout<<msg;
}
}
OUTPUT 2 :

Enter the name : Gopal


Enter the age : 17
You are not yet eligible to vote, but you can pre-register to vote once you turn 18

C)

#include<iostream>
Using namespace std;
Class number
{
Private:
Int a[20],si;
Public :
Void array(int *a1,int n)
{
Si=n;
For(int i=0;i<n;i++)
{
A[i]=a1[i];
}
}
Void div()
{
Int sum=0;
For(int i=0;i<si;i++)
{
Sum+=a[i];
}
If(sum==0)
Throw “Division operation is not possible,because sum of array element is zero”;
Else
{
// int b[20];
Cout<<”ratios are : “<<endl;
For(int i=0;i<si;i++)
{
Cout<<a[i]<<”/”<<sum<<”=”<<a[i]/sum<<endl;
}

}
}
};
Int main()
{
Int arr[20],n;
Cout<<”Enter the array size :”;
Cin>>n;
Cout<<”enter the array elements :”<<endl;
For(int i=0;i<n;i++)
{
Cin>>arr[i];
}
Number n1;
Try
{
N1.array(arr,n);
N1.div();
}
Catch(const char *msg)
{
Cout<<msg;
}
}
OUTPUT 3 :

Enter the array size :4


Enter the array elements :
-4
2
2
0
Division operation is not possible,because sum of array element is zero

RESULT :
Thus a c++ program is built to implement Exception handling is compiled and executed
successfully

You might also like