publicclassMain {publicstaticvoidmain(String[] args) {for (int i =0; i <4; i++) {System.out.println(i); } // end of for }}
0
1
2
3
?מה יודפס כאן
publicclassMain {publicstaticvoidmain(String[] args) {int i =10;for ( ; i >5; i--) System.out.print(i +", "); }}
10, 9, 8, 7, 6,
?מה יודפס כאן
publicclassMain {publicstaticvoidmain(String[] args) {for (int i =10; i <20; i++) {System.out.print(i +", "); i++; } // end of for }}
10, 12, 14, 16, 18,
?מה יודפס כאן
publicclassMain {publicstaticvoidmain(String[] args) {for (int i =0; i <10; i++) {if (i==3)continue;elseif (i==6)break;System.out.print(i +", "); } // end of for }}
0, 1, 2, 4, 5,
explanation
the keyword continue will continue to the next number in the loop
the keyword break will exit the loop altogether.
Factorial and Sum of values ⭐⭐
חשב עצרת וסכום עבור n
n!=1⋅2⋅3⋅...⋅nandi=0∑ni=1+2+..+n
example n=4
4!=1⋅2⋅3⋅4=24andi=0∑4i=1+2+3+4=10
publicclassMain {publicstaticvoidmain(String[] args) {int n =5; // or enter from userint factorial =1;for (int i =1; i <= n; i++) factorial*=i;System.out.println(n +"! = "+ factorial);int sum =0;for (int i =1; i <= n; i++) sum+=i;System.out.println("Summation of "+ n +" = "+ sum); }}
5! = 120
Summation of 5 = 15
version 2
חשב את הערך של n עד שמקבלים -1
publicclassMain {publicstaticvoidmain(String[] args) {int n;while ((n =MyConsole.readInt("enter num: "))!=-1){int factorial =1;for (int i =1; i <= n; i++) factorial*=i;System.out.println(n +"! = "+ factorial);int sum =0;for (int i =1; i <= n; i++) sum+=i;System.out.println("Summation of "+ n +" = "+ sum); } }}
enter num: 5
5! = 120
Summation of 5 = 15
enter num: 10
10! = 3628800
Summation of 10 = 55
enter num: -1
publicclassMain {publicstaticvoidmain(String[] args) {int a =MyConsole.readInt("enter a: ");int b =MyConsole.readInt("enter b: ");int n =MyConsole.readInt("enter n: ");int sum = a;for (int i =0; i < n; i++) { sum+=Math.pow(2,i)*b;System.out.println(sum); } }}
enter a: 3
enter b: 5
enter n: 6
הלוואה מבנק ⭐⭐
פלוני לקח הלוואה מהבנק ומחזיר 10 אחוז מהלוואה כל חודש הדפיסו
כמה כסף הוא מחזיר כל חודש
כמה הוא עדיין חייב אחרי 3 חודשים מאז שהוא התחיל לפרוע בחזרה
publicclassMain{publicstaticvoidmain(String[] args) {int amount =MyConsole.readInt("enter loan amount: ");int original_amount = amount;for (int i =1; i <4; i++){System.out.println("month "+ i +" payback: "+ amount*.10); amount-= (amount *.10); }System.out.println("He still owes "+ amount +" from the "+ original_amount +" loan"); }}
enter loan amount: 5000
month 1 payback: 500.0
month 2 payback: 450.0
month 3 payback: 405.0
He still owes 3645 from the 5000 loan
Catalan number ⭐⭐⭐
חשב את מספר קלטן n
Cn=(n+1)!n!(2n)!for n≥0.
for example
C4=(4+1)!4!(2⋅4)!=(5)!4!(8)!=14
?מה זה מספר קלטן
זהו מספר האפשריות לניתן להגיע מפינה השמאלית למטה לפינה הימנית למעלה בלי לחצות את הקו וללכת רק ימינה ולמעלה
publicclassMain{publicstaticvoidmain(String[] args) {int n =MyConsole.readInt("enter number: ");int numerator =1;int denominator1 =1;int denominator2 =1;//numeratorfor (int i =1; i <= n *2; i++) numerator *= i;//denominatorfor (int i =1; i <= n; i++) denominator1 *= i; denominator2 = denominator1*(n+1);int result = numerator/(denominator1*denominator2);System.out.println("C_"+ n +" = "+ result); }}
version 2
עכשיו תתדפיסו את כל המספרי קלטן עד n
publicclassMain{publicstaticvoidmain(String[] args) {int n =MyConsole.readInt("enter number: ");for (int each_c =0; each_c <= n; each_c++) {int numerator =1;int denominator1 =1;int denominator2 =1;//numeratorfor (int i =1; i <= each_c *2; i++) numerator *= i;//denominatorfor (int i =1; i <= each_c; i++) denominator1 *= i; denominator2 = denominator1*(each_c+1);int result = numerator/(denominator1*denominator2);System.out.println("C_"+ each_c +" = "+ result); } }}
enter number:6C_0 =1C_1 =1C_2 =2C_3 =5C_4 =14C_5 =42C_6 =132
Nested Loops
Print stars ⭐⭐
כתבו תוכנית אשר ידפיס משולש כוכבים עד גובה n
example:
enter number: 4
*
**
***
**** <--- The height of the triangle is 4 stars
***
**
*
code:
publicclassMain{publicstaticvoidmain(String[] args) {int n =MyConsole.readInt("enter number: ");//*//**//***//****for (int i =1; i <= n; i++) {for (int j =1; j <=i ; j++)System.out.print("*");System.out.println(); }//***//**//*for (int i = n-1; i >0; i--) {for (int j = i; j >0 ; j--)System.out.print("*");System.out.println(); } }}
כתוב תוכנית אשר יציג את לוח הכפל (עד10)
1x1 = 1
2x1 = 2
3x1 = 3
4x1 = 4
5x1 = 5
6x1 = 6
7x1 = 7
8x1 = 8
1x2 = 2
2x2 = 4
3x2 = 3
4x2 = 8
5x2 = 10
6x2 = 12
7x2 = 14
8x2 = 16
1x10 = 10
2x10 = 20
3x10 = 30
4x10 = 40
5x10 = 50
6x10 = 60
7x10 = 70
8x10 = 80
publicclassMain{publicstaticvoidmain(String[] args) {for (int i =1; i <10; i++) {for (int j =1; j <10; j++)System.out.print(j +"x"+ i +"="+(i*j) +"\t");System.out.println(); } }}
Later on we will use more nested loops for accessing multi-dimension arrays
e^x ⭐⭐⭐⭐
compute e^x using the following formula:
example:
e3=20.0855369232
if accurray or n=12 we get
e3=1+3+2!32+3!33+4!34+..+12!312=20.0855369232
publicclassMain{publicstaticvoidmain(String[] args) {double e =1;double x =MyConsole.readDouble("enter exponent: ");double accuracy =MyConsole.readDouble("enter accuracy: ");for (int n =1; n <= accuracy; n++) {int factorial =1;//find factorialfor (int i =1; i <= n; i++) { factorial*=i; } // end of inner for e +=Math.pow(x, n)/factorial; } // end of outer forSystem.out.println("e^"+ x +" = "+ e); }}
enter exponent: 3
enter accuracy: 12
e^3.0 = 20.08521256087662
in the outer loop
for (int n =1; n <= accuracy; n++) {int factorial =1;
each time we reset factorial to calculate the factorial
while the inner loop runs until n each time
for (int n =1; n <= accuracy; n++) {...for (int i =1; i <= n; i++) {
version 2
display each accuracy until n
publicclassMain{publicstaticvoidmain(String[] args) {double e =1;double x =MyConsole.readDouble("enter exponent: ");double accuracy =MyConsole.readDouble("enter accuracy: ");double real_value_of_e =2.718281828459045;for (int i =1; i <= accuracy; i++) { e =1;for (int n =1; n <= i; n++) {int factorial =1;//factorialfor (int j =1; j <= n; j++) { factorial*=j; } // end of inner for e +=Math.pow(x, n)/factorial; } // end of middle forSystem.out.println("accuracy: "+ i +", e^"+ (int)x +" = "+ e +", loss of: "+ (real_value_of_e-e)); } // end of outer for }}
enter exponent: 1
enter accuracy: 10
accuracy: 1, e^1 = 2.0, loss of: 0.7182818284590451
accuracy: 2, e^1 = 2.5, loss of: 0.2182818284590451
accuracy: 3, e^1 = 2.6666666666666665, loss of: 0.05161516179237857
accuracy: 4, e^1 = 2.708333333333333, loss of: 0.009948495125712054
accuracy: 5, e^1 = 2.7166666666666663, loss of: 0.0016151617923787498
accuracy: 6, e^1 = 2.7180555555555554, loss of: 2.262729034896438E-4
accuracy: 7, e^1 = 2.7182539682539684, loss of: 2.7860205076724043E-5
accuracy: 8, e^1 = 2.71827876984127, loss of: 3.0586177750535626E-6
accuracy: 9, e^1 = 2.7182815255731922, loss of: 3.0288585284310443E-7
accuracy: 10, e^1 = 2.7182818011463845, loss of: 2.7312660577649694E-8
think how large 12! is its 479,001,600
Remember than e = 2.718281828459045 which isn't bad at all, each time we are accurate by one decimal
Graph ⭐⭐
תכתבו תוכנית אשר מקבלת 2 מספרים שלמים m וn
ומדפיסה בטרמינל את הישר שלהם על גרף בין 10 למינוס 10 בציר הx והציר הy
y=m⋅x+n
publicclassMain{publicstaticvoidmain(String[] args) {// TODO Auto-generated method stubint m =MyConsole.readInt("m = ");int n =MyConsole.readInt("n = ");for (int y =10; y >=-10; y--) {for (int x =-10; x <=10; x++) {if (y == m*x + n) {System.out.print(" * "); }elseif (y ==0&& x ==0){System.out.print(" + "); }elseif (x ==0) {System.out.print(" | "); }elseif (y ==0) {System.out.print(" - "); }else {System.out.print(" "); } }System.out.println(); } }}