answer for how it would be evaluated in Java without writing a code for it.
a) 17 + 2 - 5 / 5
b) 2 + 3 * 5 - 2
c) 3 * 2 / 3 + 8
d) 6 % 2 * 10
Problem 2
: Write an application in java that inputs one number consisting of 5 digits (e.g.
12345) from the user, separates the number into its individual digits and prints the digits
separated from one another by three spaces each (e.g. 1 2 3 4 5).
Assume that the user enters the correct number of digits. What happens when
you enter a number with more than five digits? What happens when you enter a number
with fewer than five digits?
Problem 3:
Write an application in java that reads an integer and determines and prints
whether it’s odd or even.
Problem 4
: Write an application that calculates the squares and cubes of the numbers
from 0 to 10 and prints it in tabular format
Problem 5
: Write a in java program that inputs five integers and determines and prints the
number of negative integers entered, number of positive integers entered and the
number of zeros entered.
problem 1 .
In java every operator has its priority that is called operator precedence .
so in java operator precedence is :-
1 .) * / % (multiplication , division and modulus operator have same priority so the which will encounter first from left in an expression will be evaluated first ).
2.) + - (Addition and multiplication have same priority so the which will encounter first from left in an expression will be evaluated first ).
there are many more operator in java and every operator have its priority .
below is the attached table with operators with its priority
Level | Operator | Description | Associativity |
---|---|---|---|
16 | [] . () |
access array element access object member parentheses |
left to right |
15 | ++ -- |
unary post-increment unary post-decrement |
not associative |
14 | ++ -- + - ! ~ |
unary pre-increment unary pre-decrement unary plus unary minus unary logical NOT unary bitwise NOT |
right to left |
13 | () new |
cast object creation |
right to left |
12 | * / % | multiplicative | left to right |
11 | + - + |
additive string concatenation |
left to right |
10 | << >> >>> |
shift | left to right |
9 | < <= > >= instanceof |
relational | not associative |
8 | == != |
equality | left to right |
7 | & | bitwise AND | left to right |
6 | ^ | bitwise XOR | left to right |
5 | | | bitwise OR | left to right |
4 | && | logical AND | left to right |
3 | || | logical OR | left to right |
2 | ?: | ternary | right to left |
1 | = += -= *= /= %= &= ^= |= <<= >>= >>>= |
assignment | right to left |
Coming to equations
a) 17 + 2 - 5 / 5
first division operator will be evaluated then addition and them subtraction
so after evaluation answer will come i.e. = 18
b) 2 + 3 * 5 - 2
first multiplication then addition and then subtraction will be evaluated
so after evaluation answer will come i.e. = 15
c) 3 * 2 / 3 + 8
first multiplication operator will be evaluated then division and then addition
so after evaluation answer will be = 10
d) 6 % 2 * 10
first modulus then multiplication operator will be evaluated
so after evaluation answer will be = 0
PROBLEM 2.
program: -
import java.util.Scanner;
public class splitdigit {
public static void main(String[] args) {
Scanner sc=new
Scanner(System.in);
int num=sc.nextInt();
String s="";
while (num > 0) {
s=" "+(num%10)+s;
num = num
/ 10;
}
System.out.println(s.trim());
}
}
output : -
PROBLEM 3.
Program : -
import java.util.Scanner;
public class evenorodd {
public static void main(String[] args) {
Scanner sc=new
Scanner(System.in);
int num=sc.nextInt();
if(num%2==0) {
System.out.println("even");
}else {
System.out.println("odd");
}
}
}
output :-
PROBLEM 4.
program : -
public class SQUAREANDCUBE {
public static void main(String[] args) {
System.out.println("SQUARE\tCUBE");
for(int i=0;i<=10;i++) {
System.out.println(i*i+"\t"+i*i*i);
}
}
}
output : -
PROBLEM 5.
Program : -
import java.util.Scanner;
public class SQUAREANDCUBE {
public static void main(String[] args) {
Scanner sc=new
Scanner(System.in);
int n=0,p=0,z=0;
int num;
for(int i=0;i<5;i++) {
num=sc.nextInt();
if(num<0)n++;
if(num>0)p++;
if(num==0)z++;
}
System.out.println("Negative =
"+n+"\nPositive = "+p+"\nZero = "+z);
}
}
output : -
Get Answers For Free
Most questions answered within 1 hours.