Skip to main content

Java Loops | HackerRank solutions -codewithyasar

  Objective In this challenge, we're going to use loops to help us do some simple math. Task Given an integer,  , print its first   multiples. Each multiple   (where  ) should be printed on a new line in the form:  N x i = result . Input Format A single integer,  . Constraints Output Format Print   lines of output; each line   (where  ) contains the   of   in the form: N x i = result . Sample Input 2 Sample Output 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20 Explanation:           Here, we just need to use for loops to achieve the result Solution : import java.io.* ; import java.math.* ; import java.security.* ; import java.text.* ; import java.util.* ; import java.util.concurrent.* ; import java.util.regex.* ; public class Solution { public static void main ( String [] args ) throws IOException { BufferedReader bufferedReader = new BufferedReader ( new InputStreamReader ( Syste

Hackerrank | printf() formatting problem | JAVA

Java's System.out.printf function can be used to print formatted output. The purpose of this exercise is to test your understanding of formatting output using printf.


To get you started, a portion of the solution is provided for you in the editor; you must format and print the input to complete the solution.

Input Format

Every line of input will contain a String followed by an integer.

Each String will have a maximum of  alphabetic characters, and each integer will be in the inclusive range from  to .

Output Format

In each line of output there should be two columns:

The first column contains the String and is left justified using exactly  characters.

The second column contains the integer, expressed in exactly  digits; if the original input has less than three digits, you must pad your output's leading digits with zeroes.


Sample Input

java 100

cpp 65

python 50

Sample Output


================================

java           100 

cpp            065 

python         050 

================================

Explanation


Each String is left-justified with trailing whitespace through the first  characters. The leading digit of the integer is the  character, and each integer that was less than  digits now has leading zeroes.


Problem solution:


import java.util.Scanner;

public class Solution {

    public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            System.out.println("================================");
            for(int i=0;i<3;i++){
                String s1=sc.next();
                int x=sc.nextInt();
                //Complete this line
                System.out.printf("%-15s%03d%n",s1,x);
            }
            System.out.println("================================");

    }
}


Explanation :

  • Here %-15s = - is a flag here that will be responsible for left justification of characters within 15 spaces. 
  • %03d = Here 0 is a flag that will add 0 to integer values if the value is not 3 digits.

More details :

   In java.io package, there is a class called Printstream which has two formatting methods format and printf

 It is also an overloaded method of the PrintStream class.

syntax:-

System.out.printf(string); (the string parameter is simple as the printIn() method) System.out.printf(format, arguments); System.out.printf(locale, format, arguments);

Format


To specify the formatting rules we use the format parameter. This string is composed of literals and format specifiers. Rules start with the % character. Arguments are required only if there are format specifiers in the format string. Format specifiers include flags, width, precision, and conversion characters in the below sequence:-

%[flags][width][.precision]conversion-character


Specifiers in the brackets are optional.

Conversion Characters

  • d : formats decimal integer [byte, short, int, long]
  • f : formats floating-point number [float, double]
  • c : formats character Capital C will uppercase the letter
  • s : formats String Capital S will uppercase all the letters in the string
  • n : adds a new line character
  • t : formats date/time values.


Flags


The [flags] define standard ways to modify the output.

  • - : left-justify ( default is to right-justify )
  • + : output a plus ( + ) or minus ( - ) sign for a numerical value
  • 0 : forces numerical values to be zero-padded ( default is blank padding )

Width

The [width] specifies the field width for outputting the argument. It represents the minimum number of characters to be written to the output.


Precision

The [.precision] specifies the number of digits of precision or the length of a substring to extract from a String. Numbers are rounded to the specified precision.


Solution without using printf:

import java.util.Scanner;
public class Demo {

   public static void main(String[] args) {
      Scanner sc=new Scanner(System.in);
      System.out.println("================================");
      for(int i=0;i<3;i++){
         String s1=sc.next();
         int x = sc.nextInt();
         String newX = "";
         if(x>=0 && x<=9) {
            newX = "00";
         }
         else if(x>=10 && x<=99) {
            newX = "0";
         }
         else {
            newX = "";
         }
         int ct = s1.length();
         int space = 15 - ct;
         String bspc = "";
         for(int j=0; j<=space-1; j++) {
            bspc = bspc +" ";
         }
         System.out.println(s1 + bspc + newX+x);
      }
      System.out.println("================================");
   }
}

Comments

Highlights

Super ASCII String Checker | CodeVita | PYTHON CODE

Problem: In the Byteland country a string "S" is said to super ascii string if and only if count of each character in the string is equal to its ascii value. In the Byteland country ascii code of 'a' is 1, 'b' is 2 ...'z' is 26. Your task is to find out whether the given string is a super ascii string or not. Input Format: First line contains number of test cases T, followed by T lines, each containing a string "S". Output Format: For each test case print "Yes" if the String "S" is super ascii, else print "No" Constraints: 1<=T<=100 1<=|S|<=400, S will contains only lower case alphabets ('a'-'z'). Explanation: For eg1 - String "bba" is super ascii string Explanation:- .String "bba" The count of character 'b' is 2. Ascii value of 'b' is also 2. The count of character 'a' is 1. Ascii value of 'a' is also 1. Hence string "bba"

Lexi String | codevita |Python Solution

Lexi String Little Jill jumbled up the order of the letters in our dictionary. Now, Jack uses this list to find the smallest lexicographical string that can be made out of this new order. Can you help him? You are given a string P that denotes the new order of letters in the English dictionary. You need to print the smallest lexicographic string made from the given string S. Constraints: 1 <= T <= 1000 Length (P) = 261 <= length (S) <= 100 All characters in the string S, P are in lowercase Input Format The first line contains number of test cases T The second line has the string P The third line has the string S Output Print a single string in a new line for every test case giving the result Test Case Example 1 Input 2 polikujmnhytgbvfredcxswqaz abcd qwryupcsfoghjkldezxvbintma ativedoc Output bdca codevita Explanation: The transformed smallest lexicographical strings are in order they would be if order of letters are changed to string P

Haunted Hotel

Haunted Hotel Follow the instructions to live 6th June 1906, there was a big fire accident at a 5 star hotel in which more than 160 people died, from that time no one even dared to go near that burnt place, because it was believed that the place became haunted. This fear among people went increasing due to the suspicious deaths of the people who tried entering the hotel. More than 100 years have passed, but that has not reduced thee fear among the people even a bit. Dhoni is a person who likes adventures, so wanted to see what actually is the mystery of the hotel. So on a Friday evening he went inside the hotel with a camera which was live streamed live on his Facebook page.   The live stream lasted not even for an hour, all the electronic equipment he had began malfunctioning. He might be brave, but being alone in a place which is believed to be haunted and that too in late evening, his nerves got him. It was so quite so that, he could hear his own heart beat increasing as a drum