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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner name = new Scanner([Link]);
[Link]("What's your name");
String ime = [Link]();
[Link]("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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
[Link]("Please enter the first number");
int broj1 = [Link]();
[Link]("Please enter the second number");
int broj2 = [Link]();
[Link]("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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
[Link]("Please enter the first number");
int broj1 = [Link]();
[Link]("Please enter the second number");
int broj2 = [Link]();
[Link]("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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
for (int i=0; i<4; i++)
[Link]("Please enter the first number");
int broj1 = [Link]();
[Link]("Please enter the second number");
int broj2 = [Link]();
if (i==0)
[Link]("The result of the mathematic opearation "+ (-broj1)+"+"+broj2+"*6 is "+(-
broj1+broj2*6));
if (i==1)
[Link]("The result of the mathematic opearation "+ (broj1)+"+"+broj2+"%9 is
"+((broj1+broj2)%9));
if (i== 3)
[Link]("The result of the mathematic opearation "+ (broj1)+"+"+broj2+"/3*2 -8%3 is
"+(broj1+broj2/3*2-8%3));
if (i==2)
[Link]("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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
[Link]("Please enter the first number");
int broj1 = [Link]();
[Link]("Please enter the second number");
int broj2 = [Link]();
[Link]("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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
for (int i=0; i<5; i++){
[Link]("Please enter the first number");
int broj1 = [Link]();
[Link]("Please enter the second number");
int broj2 = [Link]();
if(i==0)
[Link]("The sum of your numbers is "+ (broj1+broj2));
if(i==1)
[Link]("The difference of your numbers is "+(broj1-broj2));
if(i==2)
[Link]("The multiplicatopn of your numbers is "+(broj1*broj2));
if(i==3)
[Link]("The quatance of your numbers is "+ broj1/broj2);
if(i==4)
[Link]("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
[Link](" J a v v a ");
[Link](" J a a v v a a ");
[Link](" J J aaaaa v v aaaaa");
[Link](" 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
[Link](((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
[Link](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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
[Link]("Please enter the radius of the circle");
float radius = [Link]();
[Link]("The perimeter of the circle is "+(2*radius*[Link]));
[Link]("The area of the circle is "+(radius*radius*[Link]));
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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
[Link]("Please enter the first number");
float broj1 = [Link]();
[Link]("Please enter the second number");
float broj2 = [Link]();
[Link]("Please enter the third number");
float broj3 = [Link]();
[Link]("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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
[Link]("Please enter the width");
float width = [Link]();
[Link]("Please enter the height");
float height = [Link]();
[Link]("The perimeter is "+(width+height)*2);
[Link]("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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner number = new Scanner([Link]);
[Link]("Please enter the first number");
int first = [Link]();
[Link]("Please enter the second number");
int second = [Link]();
int temp=first;
first=second;
second=temp;
[Link]("Your firs number is "+first);
[Link]("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
[Link]("+\"\"\"\"\"+");
[Link]("[| o o |]");
[Link](" | ^ | ");
[Link](" | '-' | ");
[Link](" +-----+ ");
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 [Link];
public class Main {
public static void main(String[] args) {
// Write your code here
Scanner binaryn = new Scanner([Link]);
long binary1, binary2;
int sum[] = new int[20];
int prenos=0,i=0;
[Link]("Please enter the first binary number");
binary1 = [Link]();
[Link]("Please enter the second binary number");
binary2 = [Link]();
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;
[Link]("The sum of your binary numbers is ");
while (i >= 0) {
[Link](sum[i--]);
[Link]("\n");