10/03/2017

Hackerrank Solution For Day 17: More Exceptions

Problem:- Write a Hackerrank Solution For Day 17: More Exceptions or Hacker Rank Solution Program In C++ For " Day 17: More Exceptions " or Hackerrank 30 days of code Java Solution:Day 17: More Exceptions solution or Hackerrank solution for 30 Days of Code Challenges or Hackerrank 30 days of code Java Solution, Day 17: More Exceptions solution, or C/C++ Logic & Problem Solving: Day 17: More Exceptions or Hacker Rank Solution For Day 17: More Exceptions.

Check This:- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.



Hackerrank Solution For Day 17: More Exceptions

Logic:- As we solve day 16 problem this is an extension of that problem. So in this problem we have to do More Exception, Basically in this problem two parameters will be given N and P and we have to calculate N^P as we know that we can not find the root and square root of negative number or in this problem while N and P are positive programs will work fine and if any N and P were Negative than Run Time error occur for handling this type of error(Run Time) we use Handler called Exception. Consider a below Input and output and check the answer in Explanation section.

Explanation:- As the given input first line is a total number of input then First input, a second input, third input and last forth input. First input is 3 (N) and 5 (P) so N^P or 3^5 is equal 243 same for second input 2^4 is equal to 16 But in third input (-1)^(-2) this can't be for avoid this type of error(Run Time Error) we use Try-Catch(Exception Handler). Now if the user enters third input than our program finds a runtime error and throw a control to catch block(print the message in the catch block).

Input

4
3 5
2 4
-1 -2
-1 3

Output

243
16
n and p should be non-negative.
n and p should be non-negative.

Syntax of Try-Catch

try
{
statement......1
statement......2
statement......3
.
.
.
statement......n
}
catch(exception )
{
statements //message
}

Also Check:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Basically, Try- catch is used in a runtime error in a program. Read full about Try-Catch click here


Submit Your Solution Here:- Click Here

Solution:-

#include <cmath>
#include <iostream>
#include <exception>
#include <stdexcept>
using namespace std;

//Write your code here
class Calculator 
{
    public:
    int power(int first, int second) 
    {
        if(first < 0 || second < 0) 
        {
           throw runtime_error("n and p should be non-negative");
        }
        return pow(first, second);
    }
};

int main()
{
    Calculator myCalculator=Calculator();

    int T,n,p;
    cin>>T;

    while(T-->0)
{
      if(scanf("%d %d",&n,&p)==2)
{
          try
{
               int ans=myCalculator.power(n,p);
               cout<<ans<<endl; 
          }
          catch(exception& e)
{
             cout<<e.what()<<endl;
          }
      }
    }
    
}

Output:-


Hackerrank Solution For Day 17: More Exceptions


You May Also Like:-


1. Hackerrank Solution For Day 12: Inheritance

2. Hackerrank Solution For Day 13: Abstract Classes

3. Hackerrank Solution For Day 14: Scope

4. Hackerrank Solution For Day 15: Linked List

5. Hackerrank Solution For Day 18: Queues and Stacks

6. Hackerrank Solution For Day 19: Interfaces

7. Hackerrank Solution For Day 20: Sorting

8. Hackerrank Solution For Day 21: Generics

9. Hackerrank Solution For Day 22: Binary Search Trees


10. Hackerrank Solution For Day 23: BST Level-Order Traversal


1 comment:

  1. The individuals not reading your blogs stuff are missing out much qualitative contents.
    tutorial for c++

    ReplyDelete