Friday, February 16, 2018

Variables declarations & Arithmetic Operators

Challenge: Write a program that prints the product of the first ten positive integers, 1 × 2 × … × 10. (Use * to indicate multiplication in Java.) 


Solution: 
public class lab1_02 {
public static void main(String[] args) {

int factorial = 1*2*3*4*5*6*7*8*9*10;



 System.out.println("10 Factorial is equal: "+factorial);
    }
}




Variable Declarations:
Data Type Size Range
byte 1  byte Integers in the range -128 to +127
short 2 bytes Integers in the range -32,768 to + 32,767
int 4 bytes Integers in the range of -2147483647 to +2147483647
long 8 bytes Integers in the range of -9,223,372,036,854,755,808 to +9,223,372,036,854,775,807
float 4 bytes Floating-points in the range of ±3.4x10^-38 to ± 3.4x10^38
double 8 bytes Floating-point numbers in the range of ±1.7x10^-308 to ±1.7x10^308

Variables Declaration: Naming Rules
  • First character must be a letter, an underscore or a dollar sign.
  • Variable names cannot contain any symbols, such as:~ \ | / @ # % ^ & - +
  • Variable names may contain letters, numbers, underscores or a dollar sign.
  • They could be any length.
  • Keywords cannot be used as variables names.
Declaring Integer Data types variables:Also is correct:
int myVariable;
int new_Variable;



int a;
int b;
int c;
int a,b,c;

Incorrect:
int 2d;
int pink#;

Declaring Float Data types variables:
 float b;
 float c;
 float myFloat;

 c = 23.5; Error!
 c = 23.5F Correct!

Declaring the Char Data types variables;
char a;
char b;
char c;
a = 'A';

Declaring the Boolean Data types variables;
boolean myBool;
myBool= true;
myBool =fool;

Primitive data type ranking
double    ^
float        |
long        |
int           |
short       |
byte        |



Arithmetic Operators:
Operator Meaning Type
+ Addition Binary
- Subtraction Binary
* Multiplication Binary
/ Division Binary
% Modulus Binary






No comments:

Post a Comment