Sunday, December 25, 2016

hacker rank 30 days

Day 0 
Solution:
public class Solution {
public static void main(String[] args) {
        // Create a Scanner object to read input from stdin.
Scanner scan = new Scanner(System.in);

// Read a full line of input from stdin and save it to our variable, inputString.
String inputString = scan.nextLine();

// Close the scanner object, because we've finished reading
        // all of the input from stdin needed for this challenge.
scan.close();
   
// Print a string literal saying "Hello, World." to stdout.
System.out.println("Hello, World.");
      System.out.println(inputString);
   // TODO: Write a line of code here that prints the contents of inputString to stdout.
}
}



Day 1
Solution:
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) {
        int i = 4;
        double d = 4.0;
        String s = "HackerRank ";

        Scanner scan = new Scanner(System.in);

        /* Declare second integer, double, and String variables. */
int t;
double j;

        /* Read and save an integer, double, and String to your variables.*/
t=scan.nextInt();
j=scan.nextDouble();
scan.nextLine();
String e=scan.nextLine();
        /* Print the sum of both integer variables on a new line. */

System.out.println(t+i);
System.out.println(j+d);

        /* Print the sum of the double variables on a new line. */
System.out.print(s+""+e );
        /* Concatenate and print the String variables on a new line;
        the 's' variable above should be printed first. */

scan.close();
    }
}


Day 2
Solution:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Arithmetic {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        double mealCost = scan.nextDouble(); // original meal price
        int tipPercent = scan.nextInt(); // tip percentage
        int taxPercent = scan.nextInt(); // tax percentage
        scan.close();
     
        // Write your calculation code here.
      double tip=mealCost*tipPercent/100;
        double tax=mealCost*taxPercent/100;
        double total=mealCost+tip+tax;
        // cast the result of the rounding operation to an int and save it as totalCost
     
        int totalCost = (int) Math.round(total/*numberToRoundHere*/);
     
        // Print your result
        System.out.println("The total meal cost is " + totalCost+ " dollars.");
    }

}

Day 3
Solution:
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 scan = new Scanner(System.in);
      int n = scan.nextInt();
      scan.close();
      String ans="";
       String an="";
         
      // if 'n' is NOT evenly divisible by 2 (i.e.: n is odd)
      if(n%2==1){
         ans = "Weird";
      }
      else{
          if(2<=n&&n<=5||20<n){
              an="Not Weird";
          }
          else
              ans = "Weird";
         // Complete the code
      }
      System.out.print(ans);
       System.out.print(an);

   }

}
Day 4
Solution:

import java.io.*;
import java.util.*;



public class Person {
    private int age;
 
public Person(int initialAge) {
       
    if (initialAge <0) {
                     System.out.println("Age is not valid, setting age to 0.");
                     initialAge = 0;
               } else {
                      age = initialAge;
               }

        // Add some more code to run some checks on initialAge
}

public void amIOld() {
       
        if( age<13){
            System.out.println("You are young."/*Insert correct print statement here*/);
        }
        if(13<=age && age<18){
           System.out.println("You are a teenager."/*Insert correct print statement here*/);

        }if(18<=age){
  // Write code determining if this person's age is old and print the correct statement:
        System.out.println("You are old."/*Insert correct print statement here*/);
        }
    }

public void yearPasses() {
        age++;
  // Increment this person's age.
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < T; i++) {
int age = sc.nextInt();
Person p = new Person(age);
p.amIOld();
for (int j = 0; j < 3; j++) {
p.yearPasses();
}
p.amIOld();
System.out.println();
        }
sc.close();
    }
}
Day 5
Solution: