Java exercises and solutions: Java arithmetic operators


Java arithmetic operators

Exercise 1: Write Java program to allow the user to input two integer values and then the program prints the results of adding, subtracting, multiplying, and dividing among the two values.

See the example below:

Enter value a:30

Enter value b:10

The result of adding is 40.

The result of subtracting is 20;

The result of multiplying is 300.

The result of dividing is 3.

Solution:


import java.util.Scanner;
public class JavaExercises
{
public static void main(String[] args)
{
caculateValues();
}

static void caculateValues(){

int a,b;
int resulta,results,resultm;
float resultd;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a:");
a=sc.nextInt();
System.out.print("Enter b:");
b=sc.nextInt();
resulta=a+b;
results=a-b;
resultm=a*b;
resultd=(float)a/b;
System.out.println("The result of adding is "+resulta);
System.out.println("The result of subtracting is "+results);
System.out.println("The result of multiplying is "+resultm);
System.out.println("The result of dividing is "+resultd);


  }

}

Exercise 2: Write Java program to generate a random number between 1 to 6.

To generate a random number, you can use the Random class of java.util package. You may use the abs() method of Math class to make sure you can get only a positive number.

Solution:


import java.util.*;
public class JavaExercises
{
public static void main(String[] args)
{
caculateValues();
}

static void caculateValues(){

int a;
Random rn=new Random();
a=1+Math.abs(rn.nextInt()%6);
System.out.println("The result: "+a);


}

}


Comments

Alee comment

 Alee

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8
1 2 3 4 5 6 7
1 2 3 4 5 6
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1


Try this very fascinating pattern using Java language,,,

See Solution here:

practiceup.blogspot.com


2018-01-10
salos comment

 salos

thank you



http://java-exercises.com/


2017-11-17
Jack comment

 Jack

Create a constructor class Sum that has public variables num1 and num2. Include a default constructor and a method Add that accepts the two integer parameters, calculates and outputs the sum. In your main class Calculate include a main method that prompts the user to enter 2 interger values and create a reference called total for the contructor Sum.

Please assist


2017-03-04
Manas Singh Chauhan comment

 Manas Singh Chauhan

Write a program that accepts the number and display whether the number is odd or even. Accept the numbers till the user enters 0.


2017-02-08
yugraj comment

 yugraj

why we are using sometimes util type of package and some time scanner type of package..please help me to understand the package .
thank yuh


2016-12-14
Krishna comment

 Krishna

http://java.meritcampus.com/core-java-topics/operators-in-java


2016-02-15
this works too i guess comment

 this works too i guess

import java.util.Random;
public class RandomNumber{
public static void main (String []args){
Random random = new Random();
int randomInt = random.nextInt(30);
System.out.println(" number:" + randomInt);
}
}


2015-09-25
saba comment

 saba

what will RunNum.nextInt() do?


2015-06-04
Yaswanth comment

 Yaswanth

I need calculator problems..


2015-05-11
I tried this way. Any thoughts comment

 I tried this way. Any thoughts

import java.util.Random;
public class RandomNumber{
public static void main (String []ars){
Random RanNum = new Random();
int answer = RanNum.nextInt(6) + 1;
System.out.println(Math.abs (answer));
}
}


2015-03-10



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.