publicclassMain {publicstaticvoidmain(String[] args) {int a =1, b=71, c=3, d=10, max; max = (a >b) ? a : b; max =Math.max(max, c); //same thing max = (max >d) ? max : d;System.out.println(max); }}
71
כמה ספרות וכמה אפסים
קלוט מספר n
הדפס מספר הספרות
הדפס מספר האפסים
publicclassMain {publicstaticvoidmain(String[] args) {int num =MyConsole.readInt("enter a number: ");System.out.println("You entered a "+ (num >=0?"positive":"negative") +" number");int countZeros=0;int countDigits=0;int absNum = num>=0? num :-num;if (num==0){ countZeros=1; countDigits=1; }while (absNum >0) {if (absNum%10==0) countZeros++; absNum /=10; countDigits++; }System.out.println("number of digits: "+ countDigits);System.out.println("number of zeros: "+ countZeros); }}
enter a number: -30012
You entered a negative number
number of digits: 5
number of zeros: 2
זה if מקוצר
int absNum = num>=0? num :-num;
version 2
let num be a random number from -1000 until 9999 (including)
int max =10_000, min =-1_000;int num = (int) ((Math.random() * (max - min)) + min); //random number from -1,000 to 9,999 (including)System.out.println("Random num:"+ num);
Random num:6318
You entered a positive number
number of digits: 4
number of zeros: 0
Note: it includes the min but not the max [min, max)
Rotate Square
יש לסובב את הקוביה
publicclassMain {publicstaticvoidmain(String[] args) {int a =1, b =2, c=3, d=4, temp; temp = d; d = c; c = b; b = a; a = temp;System.out.println("a: "+ a +"\nb: "+ b +"\nc: "+ c +"\nd: "+ d); }}
a: 4
b: 1
c: 2
d: 3
טבלת מעקב
Version 2 : n right or left shifts
קלוט מספר n:
אם הוא חיובי אז סובב n פעמים ימינה
אם הוא שלילי אז סובב n פעמים שמאלה
publicclassMain {publicstaticvoidmain(String[] args) {int a =1, b =2, c=3, d=4, temp;boolean isRightShift=true;int n_shifts =MyConsole.readInt("enter:\n* posivite num for right shift"+"\n* negative for left shift\ninput :");if (n_shifts<0) { isRightShift =false; n_shifts = n_shifts * -1; //since while works on positive number, otherwise we would need 2 different whiles
System.out.println("Left shifting "+ n_shifts +" times"); } elseif (n_shifts==0)System.out.println("No shifts");elseSystem.out.println("Right shifting "+ n_shifts +" times");while (n_shifts>0){if (isRightShift) { temp = d; d = c; c = b; b = a; a = temp; n_shifts = n_shifts-1;System.out.println("a: "+ a +"\tb: "+ b +"\tc: "+ c +"\td: "+ d); }else { temp = a; a = b; b = c; c = d; d = temp; n_shifts = n_shifts-1;System.out.println("a: "+ a +"\tb: "+ b +"\tc: "+ c +"\td: "+ d); } } }}
enter:
* posivite num for right shift
* negative for left shift
input : -2
Left shifting 2 times
a: 2 b: 3 c: 4 d: 1
a: 3 b: 4 c: 1 d: 2
note
since while (n_shifts>0) checks for positive numbers and we do n_shifts = n_shifts-1; inside the while loop without n_shifts = n_shifts * -1 if we have a left shift we would need anotherwhile loop
Find Aprox. log2
מצא עבור קלט x את:
קודם כל בא נבין מה קורה כאן
אם הוא לא חזקה שלמה הדפס את חזקה שלמה ועוד .x
publicclassMain {publicstaticvoidmain(String[] args) {float x = (float)MyConsole.readDouble("enter positive num: ");while (x<0.) x = (float)MyConsole.readDouble("Try again\nPlease enter a positive num: ");// float x = 1<<10;// float x = 1_024f;float original = x;int count =0;boolean isPowOf2 =false;while(x >2) { x = x /2; count +=1;if (x==2.){System.out.println("log_2("+original+")="+(count+1)); isPowOf2 =true; } }if (isPowOf2==false)System.out.println("log_2("+original+")="+count +".x"); }}
enter num: 1024
log_2(1024.0)=10
2nd run
enter num: 1025
log_2(1025.0)=10.x
To find the real answer we should use a Talyor series.