Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java End Of File – HackerRank Solution

 The challenge here is to read n lines of input until you reach EOF, then number and print all   n lines of content.

Hint: Java’s Scanner.hasNext() method is helpful for this problem.

Input Format

Read some unknown  n  lines of input from stdin(System.in) until you reach EOF; each line of input contains a non-empty String.

Output Format

For each line, print the line number, followed by a single space, and then the line content received as input.

Sample Input

Hello world
I am a file
Read me until end-of-file.

Sample Output

1 Hello world
2 I am a file
3 Read me until end-of-file.

Solution:-

/ Java End Of File - HackerRank 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) 
    {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        // Java End Of File - HackerRank Solution START
        
        Scanner io = new Scanner(System.in);
        int i=0;
        while(io.hasNextLine())
        {
            String s = io.nextLine();
            if (s.contains("end-of-file"))
            {
                i++;
                System.out.println(i+" "+s);
                break;
            }
            else
            {
                i++;
                System.out.println(i+" " +s);       
            }
        }
        
        // Java End Of File - HackerRank Solution END
    }
}

Comments