0% found this document useful (0 votes)
8 views

Java Assignment 2

The document contains a Java assignment with five questions that require writing code to perform various tasks, such as printing strings, reading integers, and applying conditional logic. Each question includes a sample solution in Java code, demonstrating the required functionality. The assignment is aimed at practicing basic programming concepts in Java.

Uploaded by

phoniexstar2304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Java Assignment 2

The document contains a Java assignment with five questions that require writing code to perform various tasks, such as printing strings, reading integers, and applying conditional logic. Each question includes a sample solution in Java code, demonstrating the required functionality. The assignment is aimed at practicing basic programming concepts in Java.

Uploaded by

phoniexstar2304
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Hrishikesh Suryawanshi

PRN: 1132221141

Java Assignment 2

Q1) Print "Hello, World.”, “Hello, Java." On two separate lines.

public class Solution {

public static void main(String[] args) {


System.out.println("Hello, World.");
System.out.println("Hello, Java.");
}
}

Q2) In this challenge, you must read 3 integers from stdin and then print them to stdout. Each
integer must be printed on a new line.

import java.util.*;

public class Solution {

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
int a = scan.nextInt();
int b = scan.nextInt();
int c = scan.nextInt();

System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
Q3) Given an integer, n, perform the following conditional actions:

If n is odd, print Weird

If n is even and in the inclusive range of 2 to 5, print Not Weird

If n is even and in the inclusive range of 6 to 20, print Weird

If n is even and greater than 20, print Not Weird

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 {

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])?");
if(N%2 == 1){
System.out.println("Weird");
}else if(N%2==0 && (2<=N && N<=5)){
System.out.println("Not Weird");
}else if(N%2==0 && (6<=N && N<=20)){
System.out.println("Weird");
}else{
System.out.println("Not Weird");
}
scanner.close();
}
}
Q4) In this challenge, you must read an integer, a double, and a String from stdin, then print the
values according to the instructions in the Output Format section below.

There are three lines of output: -

1. On the first line, print String: followed by the unaltered String read from stdin.

2. On the second line, print Double: followed by the unaltered double read from stdin.

3. On the third line, print Int: followed by the unaltered integer read from stdin.

import java.util.Scanner;

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();

System.out.println("String: " + s);


System.out.println("Double: " + d);
System.out.println("Int: " + i);
}
}
Q5) Every line of input will contain a String followed by an integer.

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

In each line of output there should be two columns:

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

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

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();
System.out.printf("%-15s",s1);
System.out.printf("%03d\n",x);
}
System.out.println("================================"
);

}
}

You might also like