Uploaded by ANMOL GARG

2020540955 RohitKumar

advertisement
ROHIT KUMAR
2020540955(SEC-G)
BootCamp Java
Assignment-1
Hacker Rank Problem Solutions:
1. Java If-Else
Code:
public class Solution {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int N = scanner.nextInt();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
scanner.close();
if(N%2==0)
{
if ( N>=2 && N<5)
{
System.out.println("Not Weird");
}
else if (N>=6 && N<=20)
{
System.out.println("Weird");
}
else{
System.out.println("Not Weird");
}
}
else {
System.out.println("Weird");
}
}
Output:
2. Java Loops-1
code:
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
int N = Integer.parseInt(bufferedReader.readLine().trim());
for(int i=1; i<=10;i++){
System.out.printf("%d x %d = %d \n", N, i, N* i);
}
System.out.println();
bufferedReader.close();
}
}
Output:
3. Java Loops-II
Code:
class Solution{
public static void main(String []argh){
Scanner in = new Scanner(System.in);
int t=in.nextInt();
for(int
int
int
int
i=0;i<t;i++){
a = in.nextInt();
b = in.nextInt();
n = in.nextInt();
for(int j = 0; j < n; j++){
a = a + (int)Math.pow(2,j)*b;
System.out.print(a + " ");
}
System.out.println("");
}
in.close();
}
}
Output:
4. Java Stdin and Stdout II
Code:
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
double d = scan.nextDouble();
scan.nextLine();
String s = scan.nextLine();
scan.close();
// Write your code here.
System.out.println("String: " + s);
System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Output:
5. Java Strings Introduction
Code:
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
String B=sc.next();
/* Enter your code here. Print output to STDOUT. */
int len =A.length()+B.length();
System.out.println(len);
int l2=A.compareTo(B);
if (l2 >0){
System.out.println("Yes");
}
else{
System.out.println("No");
}
String a=A.substring(0,1).toUpperCase();
String b=B.substring(0,1).toUpperCase();
System.out.println(a+A.substring(1)+" "+b+B.substring(1));
}
}
Output:
6. Java String Reverse
Code:
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String A=sc.next();
/* Enter your code here. Print output to STDOUT. */
boolean palindrome = true;
for(int i = 0; i<A.length()/2;i++)
{
if(A.charAt(i)!=A.charAt(A.length()-1-i))
{
palindrome = false;
}
}
if(palindrome){System.out.println("Yes");}
else{System.out.println("No");}
}
}
Output:
7. Java Substring Comparison
Code:
public static String getSmallestAndLargest(String s, int k) {
String current_sub=s.substring(0,k);
String smallest =current_sub ;
String largest = current_sub;
for(int i=1;i<=s.length()-k;i++){
current_sub=s.substring(i,i+k);
if(current_sub.compareTo((largest))>0){
largest=current_sub;
}
if(current_sub.compareTo((smallest))<0){
smallest=current_sub;
}
}
return smallest + "\n" + largest;
}
Output:
Download