1.
Write a Java
program to print 'Hello' on screen and then print your name on a separate
line.
Expected Output :
Hello
Alexandra Abramov
SOLUTION
public class Exercise1 {
public static void main(String[] args) {
System.out.println("Hello\nAlexandra Abramov!");
}
}
2.
Write a Java
program to print the sum of two numbers.
Test Data:
74 + 36
Expected Output :
110
SOLUTION
public class Exercise2 {
public static void main(String[] args) {
System.out.println(24+26);
}
}
3. Write
a Java program to divide two numbers and print on the screen.
Test Data :
50/3
SOLUTION
public class Exercise3 {
public static void main(String[] args) {
System.out.println(50/3);
}
}
4. Write
a Java program to print the result of the following operations.
Test Data:
a. -5 + 8 * 6
b. (55+9) % 9
c. 20 + -3*5 / 8
d. 5 + 15 / 3 * 2 - 8 % 3
Expected Output :
43
1
19
13
public class Exercise4 {
SOLUTION
public static void main(String[] args) {
System.out.println(-5 + 8 * 6);
System.out.println((55+9) % 9);
System.out.println(20 + -3*5 / 8);
System.out.println(5 + 15 / 3 * 2 - 8 % 3);
}
}
5. Write
a Java program that takes two numbers as input and display the product of two
numbers.
Test Data:
Input first number: 25
Input second number: 5
Expected Output :
25 x 5 = 125
SOLUTION
import java.util.Scanner;
public class Exercise5 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input first number: ");
int num1 = in.nextInt();
System.out.print("Input second number: ");
int num2 = in.nextInt();
System.out.println(num1 + " x " + num2 + " = " +
num1 * num2);
}
}
6. Write
a Java program to print the sum (addition), multiply, subtract, divide and
remainder of two numbers.
Test Data:
Input first number: 125
Input second number: 24
Expected Output :
125 + 24 = 149
125 - 24 = 101
125 x 24 = 3000
125 / 24 = 5
125 mod 24 = 5
SOLUTION
import java.util.Scanner;
public class Exercise6 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input first number: ");
int num1 = in.nextInt();
System.out.print("Input second number: ");
int num2 = in.nextInt();
System.out.println(num1 + " + " + num2 + " = " +
(num1 + num2));
System.out.println(num1 + " - " + num2 + " = " +
(num1 - num2));
System.out.println(num1 + " x " + num2 + " = " +
(num1 * num2));
System.out.println(num1 + " / " + num2 + " = " +
(num1 / num2));
System.out.println(num1 + " mod " + num2 + " = " +
(num1 % num2));
}
}
7. Write
a Java program that takes a number as input and prints its multiplication table
upto 10.
Test Data:
Input a number: 8
Expected Output :
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
...
8 x 10 = 80
SOLUTION
import java.util.Scanner;
public class Exercise7 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Input a number: ");
int num1 = in.nextInt();
for (int i=0; i< 10; i++){
System.out.println(num1 + " x " + (i+1) + " = " +
(num1 * (i+1)));
}
}
}
Copy
8. Write
a Java program to display the following pattern.
Sample Pattern :
J a v
v a
J a a v
v a a
J
J aaaaa V V
aaaaa
JJ
a a V
a a
SOLUTION
public class Exercise8 {
public static void main(String[] args) {
System.out.println(" J
a v v a
");
System.out.println(" J a
a v
v a a");
System.out.println("J J
aaaaa V V aaaaa");
System.out.println(" JJ a
a V a
a");
}
}
9. Write
a Java program to compute the specified expressions and print the output.
Test Data:
((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))
Expected Output
2.138888888888889
SOLUTION
public class Exercise9 {
public static void main(String[] arg) {
System.out.println((25.5 * 3.5 - 3.5 * 3.5)
/ (40.5 - 4.5));
}
}
Copy
10. Write a Java program to compute a specified
formula. Specified Formula :
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
Expected Output
2.9760461760461765
SOLUTION
public class Exercise10 {
public static void main(String[] args) {
double result = 4.0 * (1 - (1.0/3) + (1.0/5)
- (1.0/7) + (1.0/9) - (1.0/11));
System.out.println(result); //
}
}
11. Write
a Java program than read an integer and calculate the sum of its digits and
write the number of each digit of the sum in English
SOLUTION
import java.io.*;
public class Main {
public
static void main(String[] args) {
BufferedReader
br = new BufferedReader(new InputStreamReader(System.in));
try
{
int
sum = 0;
String
str = br.readLine();
char[]
numStr = str.toCharArray();
for
(int i = 0; i < numStr.length; i ++) {
sum
+= numStr[i] - '0';
}
System.out.println("Original
Number: "+str);
print_number(sum);
}
catch (IOException e) {
e.printStackTrace();
}
}
public
static void print_number(int n) {
int
x; int y; int z;
String[]
number = {"zero","one","two","three","four","five","six","seven","eight","nine"};
System.out.println("Sum
of the digits of the said number: "+n);
if
(n < 10) {
System.out.println(number[n]);
}
else
if (n < 100) {
x
= n / 10;
y
= n - x *10;
System.out.println("In
English: "+number[x] + " " + number[y]);
}
else
{
x
= n / 100;
y
= (n - x * 100) / 10;
z
= n - x * 100 - y * 10;
System.out.println("In
English: "+number[x] + " " + number[y] + " " +
number[z]);
}
}
12. Write a Java program to check whether a security
manager has already been established for the current application or not.
import java.lang.*;
public class
Exercise89 {
public static
void main(String[] args)
{
System.out.println("System
security interface:");
System.out.println(System.getSecurityManager());
}
}
13. Write a Java program to check whether a security
manager has already been established for the current application or not.
SOLUTION
import java.lang.*;
public class Exercise89
{
public static void
main(String[] args)
{
System.out.println("System
security interface:");
System.out.println(System.getSecurityManager());
}
}
14. Write a Java
program to get the first occurrence (Position starts from 0.) of an element of
a given array.
SOLUTION
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int nums[] =
{2, 4, 6, 7, 8};
int target =
7;
int
lower = 0;
int
upper = nums.length - 1;
int
index = -1;
while
(lower <= upper) {
int
mid = (lower + upper) >> 1;
if
(nums[mid] == target) {
index = mid;
}
if
(nums[mid] >= target) {
upper = mid - 1;
}
else {
lower = mid + 1;
}
}
System.out.print("Position of "+target +" is
"+index);
}
}
15. Write a Java program to find the index of the
first unique character in a given string, assume that there is at least one
unique character in the string. Go to the editor
Sample Output:
Original String: wresource
First unique character of the above: 0
SOLUTION
import java.util.*;
public
class Solution {
public
static void main(String[] args) {
String
s = "wresource";
System.out.println("Original
String: "+s);
System.out.println("First unique character of the above:
"+first_Uniq_Char(s));
}
public
static int first_Uniq_Char(String s) {
int[]
freq = new int[256];
for
(char c : s.toCharArray()) {
freq[c - 'a']++;
}
for (int
i = 0; i < s.length(); i++) {
if
(freq[s.charAt(i) - 'a'] == 1) return i;
}
return
-1;
}
}
Post a Comment