Tuesday 28 June 2016

Hackerrank , 30 Days of Code Challenges ( Day 16 & Day 17 Solutions)

Day 16: Exceptions - String to Integer    :
 import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        try{
        String S = in.next();
            System.out.println(""+Integer.parseInt(S));
    }catch(Exception e)
            {
            System.out.println("Bad String");
        }
    }
}

Day 17: More Exceptions   :
class Calculator :
    def power(self, a, b):
        if a>= 0 and b>=0:
            return a**b
        else:
            return "n and p should be non-negative"
 

No comments:

Post a Comment