#need help in hackerrank question

37 messages · Page 1 of 1 (latest)

torn bloom
#
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        String s=" ";int j=0,len=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("================================");
        for(int i=0;i<=2;i++)
        {
            s=sc.nextLine();
            j=sc.nextInt();   
            len=s.length();
            System.out.print(s);
            for(int k=(16-(len+1));k>=1;k--)
            {
                System.out.print(' ');
            }
            if(j>=100 && j<=999)
            {
                System.out.println(j);
            }
            else if(j<=100)
            {
                System.out.println("0"+j);
            }
            else if(j<=9)
            {
                System.out.println("00"+j);
            }
        }
        System.out.println("================================");
        sc.close();
    }
}```
gritty palmBOT
#

This post has been reserved for your question.

Hey @torn bloom! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

torn bloom
#

bro

#

actuallythis is the question

#

but i found a online solution in just 1 line

#
System.out.printf("%-15s%03d\n", s1, x);
#

what does this line means may u explain?

hot summit
#

and padded with spaces to fill the field if necessary

torn bloom
#

and how this line auto adds 0 befor int if it is not of 3 digit?

hot summit
#

%03d part of the format string specifies that the second argument, x, should be formatted as a decimal integer with at least three digits, padded with leading zeros!

torn bloom
#

oo still i have not studied printf

hot summit
#

goodluck!

torn bloom
#

tq

#

@hot summit

#

may u explain my a ques?

hot summit
torn bloom
#

what is it trying to say i can even understand that

hot summit
torn bloom
#

from my understanding i think we should print a+2 to the power 0 to the number provided(which is n)*b

#
for (int j = 0; j < n; j++) {
                int x = (int) Math.pow(2, j);
                s = a + x * b;
            }```
#

like this?

woeful anvil
#

dont do hacker rank yet if you can't solve it without a lot of help

cosmic drift
#

Did you study AP? It's an AP question

#

Maybe

#

Not AP but progression fs

torn bloom
torn bloom
woeful anvil
#

not disrespecting or all this stuff is not good for practice when starting off

cosmic drift