﻿ Java exercises and solutions: Java arithmetic operators

# 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 Alee1 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.com2018-01-10 salosthank you http://java-exercises.com/2017-11-17 JackCreate 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 assist2017-03-04 Manas Singh ChauhanWrite 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 yugrajwhy we are using sometimes util type of package and some time scanner type of package..please help me to understand the package . thank yuh2016-12-14 Krishnahttp://java.meritcampus.com/core-java-topics/operators-in-java2016-02-15 this works too i guessimport 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 sabawhat will RunNum.nextInt() do? 2015-06-04 YaswanthI need calculator problems..2015-05-11 I tried this way. Any thoughtsimport 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.