Tuesday, 3 October 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

Extreme Recommended:- If this post is beneficial for you and you want Updates for New post then please like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date for a new post or if you have any Query you can ask there with lots of coders also suggest to your Friends to join and like our page, so we can help our community, and don't forget to Subscribe. Enter your Email and click to subscribe.

Tip:- Copy the colored code or full code and paste it into hacker rank editor.All solution provided here are in C++ (CPP) if any reader wants these solutions in C, and Java comments below or email me with your query like " day n solution in C / C++ / Java. Check the end of the post solutions with the full explanation

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


Ghanendra Yadav
Ghanendra Yadav

I Am Ghanendra Yadav Owner Of This Website. I Love Programming, Web Developing, Logo Making, Banner And Website designing. Currently I Am Studying In NIT Warangal And Also Working With WSDC( As Web Application Developer ) In NIT Warangal For Any Query Mail Me I Will Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment