1.
Write a Java program to print 'Hello' on screen and then print your name on a
separate line. Go to the editor
Expected Output :
Hello
Alexandra Abramov
Click me to see the solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner name = new Scanner(System.in);
System.out.println("What's your name");
String ime = name.nextLine();
System.out.println("Hello "+ime+" how are you today?");
2. Write a Java program to print the sum of two numbers. Go to the editor
Test Data:
74 + 36
Expected Output :
110
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
System.out.println("Please enter the first number");
int broj1 = number.nextInt();
System.out.println("Please enter the second number");
int broj2 = number.nextInt();
System.out.println("The sum of your numbers is "+ (broj1+broj2));
3. Write a Java program to divide two numbers and print on the screen. Go to the
editor
Test Data :
50/3
Expected Output :
16
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
System.out.println("Please enter the first number");
int broj1 = number.nextInt();
System.out.println("Please enter the second number");
int broj2 = number.nextInt();
System.out.println("The quotient of your numbers is "+ (broj1/broj2));
4. Write a Java program to print the result of the following operations. Go to the
editor
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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
for (int i=0; i<4; i++)
System.out.println("Please enter the first number");
int broj1 = number.nextInt();
System.out.println("Please enter the second number");
int broj2 = number.nextInt();
if (i==0)
System.out.println("The result of the mathematic opearation "+ (-broj1)+"+"+broj2+"*6 is "+(-
broj1+broj2*6));
if (i==1)
System.out.println("The result of the mathematic opearation "+ (broj1)+"+"+broj2+"%9 is
"+((broj1+broj2)%9));
if (i== 3)
System.out.println("The result of the mathematic opearation "+ (broj1)+"+"+broj2+"/3*2 -8%3 is
"+(broj1+broj2/3*2-8%3));
if (i==2)
System.out.println("The result of the mathematic opearation "+ (broj1)+"+-"+broj2+"*5/8 is
"+(broj1+(-broj2*5/8)));
else;
5. Write a Java program that takes two numbers as input and display the product
of two numbers. Go to the editor
Test Data:
Input first number: 25
Input second number: 5
Expected Output :
25 x 5 = 125
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
System.out.println("Please enter the first number");
int broj1 = number.nextInt();
System.out.println("Please enter the second number");
int broj2 = number.nextInt();
System.out.println("The product of your numbers is "+ broj1*broj2);
6. Write a Java program to print the sum (addition), multiply, subtract, divide and
remainder of two numbers. Go to the editor
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
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
for (int i=0; i<5; i++){
System.out.println("Please enter the first number");
int broj1 = number.nextInt();
System.out.println("Please enter the second number");
int broj2 = number.nextInt();
if(i==0)
System.out.println("The sum of your numbers is "+ (broj1+broj2));
if(i==1)
System.out.println("The difference of your numbers is "+(broj1-broj2));
if(i==2)
System.out.println("The multiplicatopn of your numbers is "+(broj1*broj2));
if(i==3)
System.out.println("The quatance of your numbers is "+ broj1/broj2);
if(i==4)
System.out.println("The mod of your numbers is "+ broj1%broj2);
8. Write a Java program to display the following pattern. Go to the editor
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
public class Main {
public static void main(String[] args) {
// Write your code here
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. Go to the editor
Test Data:
((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5))
Expected Output
2.138888888888889
public class Main {
public static void main(String[] args) {
// Write your code here
System.out.println(((25.5 * 3.5 - 3.5 * 3.5) / (40.5 - 4.5)) );
10. Write a Java program to compute a specified formula. Go to the editor
Specified Formula :
4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11))
Expected Output
2.9760461760461765
public class Main {
public static void main(String[] args) {
// Write your code here
System.out.println(4.0 * (1 - (1.0/3) + (1.0/5) - (1.0/7) + (1.0/9) - (1.0/11)));
11. Write a Java program to print the area and perimeter of a circle. Go to the
editor
Test Data:
Radius = 7.5
Expected Output
Perimeter is = 47.12388980384689
Area is = 176.71458676442586
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
System.out.println("Please enter the radius of the circle");
float radius = number.nextFloat();
System.out.println("The perimeter of the circle is "+(2*radius*Math.PI));
System.out.println("The area of the circle is "+(radius*radius*Math.PI));
12. Write a Java program that takes three numbers as input to calculate and print
the average of the numbers. Go to the editor
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
System.out.println("Please enter the first number");
float broj1 = number.nextFloat();
System.out.println("Please enter the second number");
float broj2 = number.nextFloat();
System.out.println("Please enter the third number");
float broj3 = number.nextFloat();
System.out.println("The average of your three numbers is "+(broj1+broj2+broj3)/3);
}
}
13. Write a Java program to print the area and perimeter of a rectangle. Go to the
editor
Test Data:
Width = 5.5 Height = 8.5
Expected Output
Area is 5.6 * 8.5 = 47.60
Perimeter is 2 * (5.6 + 8.5) = 28.20
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
System.out.println("Please enter the width");
float width = number.nextFloat();
System.out.println("Please enter the height");
float height = number.nextFloat();
System.out.println("The perimeter is "+(width+height)*2);
System.out.println("The area is "+(width*height));
14. Write a Java program to print an American flag on the screen. Go to the
editor
Expected Output
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
* * * * * ==================================
* * * * * * ==================================
==============================================
==============================================
==============================================
==============================================
==============================================
==============================================
15. Write a Java program to swap two variables. Go to the editor
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner(System.in);
System.out.println("Please enter the first number");
int first = number.nextInt();
System.out.println("Please enter the second number");
int second = number.nextInt();
int temp=first;
first=second;
second=temp;
System.out.println("Your firs number is "+first);
System.out.println("Your second number is "+second);
16. Write a Java program to print a face. Go to the editor
Expected Output
+"""""+
[| o o |]
| ^ |
| '-' |
+-----+
public class Main {
public static void main(String[] args) {
// Write your code here
System.out.println("+\"\"\"\"\"+");
System.out.println("[| o o |]");
System.out.println(" | ^ | ");
System.out.println(" | '-' | ");
System.out.println(" +-----+ ");
17. Write a Java program to add two binary numbers. Go to the editor
Input Data:
Input first binary number: 10
Input second binary number: 11
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner binaryn = new Scanner(System.in);
long binary1, binary2;
int sum[] = new int[20];
int prenos=0,i=0;
System.out.println("Please enter the first binary number");
binary1 = binaryn.nextLong();
System.out.println("Please enter the second binary number");
binary2 = binaryn.nextLong();
while (binary1!=0 || binary2!=0){
sum[i++] = (int)((binary1 % 10 + binary2 % 10 + prenos) % 2);
prenos = (int)((binary1 % 10 + binary2 % 10 + prenos) / 2);
binary1 = binary1 / 10;
binary2 = binary2 / 10;
if (prenos != 0) {
sum[i++] = prenos;
--i;
System.out.println("The sum of your binary numbers is ");
while (i >= 0) {
System.out.print(sum[i--]);
System.out.print("\n");