Back to feed
Renewal·서른의 생활코딩

Day 6) Chapter 4 Exercises 20180606_am, 1st attempt

NS
normalstory
cover image


[4-2] Find the total sum of integers from 1 to 20 that are not multiples of 2 or 3.

// [Attempt 01] Before learning loops 

// 1 <= x < 21

// x%2 != 0

// x%3 != 0

int sum =0;

int x = 1 ;

if(1 <= x && x < 21){

x ++;

if(x%2 != 0 || x%3 != 0){

sum = sum + x;

}

}

System.out.println("Answer 2: " + sum);

// Ah... of course... since there's no loop, it only goes around once.. 

// The increment i++ is meaningless 

// because you need to go back up and repeat..  


// [Attempt 02] After learning loops

int sum1 = 0; // variable declaration and initialization for the sum

//for(){} // performing an operation on repeating numbers. 

// But since it's not an operation on unknown numbers..

for(int i=1;i<21;i++){ // filter 1 (1 <= i < 21) <-- integers from 1 to 20

//sum = sum + i;                 // first, the total sum 

// filter 2 (i%2==0 || i%3==0) <-- multiple of 2 or 3 condition

if(i%2!=0 || i%3!=0){

sum1 = sum1 + i;         // the ultimate total

}

}

System.out.println(sum1);


[4-3] Calculate the result of 1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+...+10).

// [Attempt 01] Before learning loops 

// x 1

// x + (x + x+1) 1+(1+2)

// x + (x + x+1) + (x + (x + x+1)) 1+(1+2)+(1+2+3)


// [Attempt 02] After learning loops  

// No common formula emerges... let me try directly...

//1 //sum

//(1+2) //sum=sum+i++

//(1+2+3) //sum=sum+i++

//(1+2+3+4) // ...


int sum = 1;

for(int i =2 ; i<11 ; i++){

sum=sum+i;

}

System.out.println(sum);

// Value is 55? That's odd... let me verify by hand...

System.out.println(  1+

(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+8+9)+

(1+2+3+4+5+6+7+8+9+10));

// Whoa... sure enough... the value is different, 220??  !!!  

// Again... 

1                                                   // sum

(1+2)

1+(1+2)                                          // sum + (sum + i)   

1+(1+2) +(1+2+3)                            // sum + (sum + i)   


1                                                   // 1

(1+2)

1+(1+2)                                          // 1+ (1+ 2)   

1+(1+2) +(1+2+3)                            // 1+ (1+ 2) + 1+ (1+ 2)   +3   

// The formula is wrong.....  


(1)+    //0 //sum+1

(1+2)+    //3 //sum+2

(1+2+3)+    //6  //sum+3

(1+2+3+4)+    //10  //sum+4

(1+2+3+4+5)+    //15  //sum+5

...

 = (sum_0) + (sum+2) + (sum+3) + (sum+4).... +(sum+10

 = (sum +j(1)++) + (sum+j++) + (sum+j++) + (sum+j++).... +(sum+j(10)

// A loop that calculates the first sum 

// and another loop that repeatedly adds the previous sum — how about making two loops?

int sum = 1;

for(int i =2 ; i<11 ; i++){

sum=sum+i;

for(int j =2 ; j<11 ; j++){

sum=sum+j;

}

}

System.out.println(sum);

// Value is 64? That's odd... let me verify by hand again...

// After checking with breakpoints... // another question arises

// The point is: previous sums + new sum... but the formula I'm using

// is just the new sum...  because there's only one container to hold the sum

// impossible to reuse previously stored values...

// let me create another container for the sum 


// [Attempt 03]      

int sumA = 0;

int sumB = 0;


for(int i =1 ; i<11 ; i++){

sumA=sumA+i;

for(int j =1 ; j<11 ; j++){

sumB=sumA+j;

}

sumA + sumB;        (+error)

}

System.out.println(sumB);




[4-4] When you keep adding in the pattern 1+(-2)+3+(-4)+..., 

find up to what number   you must add to make the total sum 100 or greater.

// [Attempt 01] 

int p=0;// variable to hold the sum of odd (positive) values

int m=0;// variable to hold the sum of even (negative) values

int sum4 =0;

// don't know when to stop adding, so use while

int i = 0; // loop variable

while(true){

if(sum4<100){  // when to end?

if(i%2!=0){

p=p+i;

i++; // increment

}else{

m=m+i;

i++; // increment

}

sum4 = p-m; // total

}else{

break; 

}

}

System.out.println(p);

System.out.println(m);

System.out.println(sum4);

// The values look very strange.... 


// [Attempt 02] 

int sum4 =0;// total 

// don't know when to stop adding, so use while

int i = 1; // loop variable

while(true){

sum4=(i%2==0)? sum4-i : sum4+i;

i++;

if(sum4>=100){

break;

}

}

System.out.println(i);



[4-5] Convert the following for loop to a while loop.

// public class Exercise4_5 {

// public static void main(String[] args) {

for(int i5=0; i5<=10; i5++) {

for(int j=0; j<=i5; j++){

System.out.print("*");

}

System.out.println();

}


// Attempt 1)

int i5=0;

while(i5<=10){

for(int j=0;j<=i5 ;j++){

System.out.print("*");

}

System.out.println();

i5++;

}

// } // end of main

// } // end of class



-----------------------------------------------------------------------------------------------------------------


package z_exam;


public class exam04 {

public static void main(String[] args) {

// [4-1] Express the following statements as conditional expressions.

// 1. Condition true when int variable x is greater than 10 and less than 20

// 10 <= x && x <20 

// 2. Condition true when char variable ch is neither space nor tab

// " " != ch && tab != ch

// 3. Condition true when char variable ch is 'x' or 'X'

// ch == 'x' || ch == 'X'

// 4. Condition true when char variable ch is a digit ('0'~'9')

// '0' <= ch && ch < '10'

// 5. Condition true when char variable ch is an English letter (uppercase or lowercase)

// "A" <= ch && ch <= "Z" || "a" <= ch && ch <= "z"

// 6. Condition true when int variable year is divisible by 400, or divisible by 4 but

// not divisible by 100

// (year%400 == 0 || year%4 == 0) && year%100 != 0

// 7. Condition true when boolean variable powerOn is false

// powerOn == false

// 8. Condition true when string reference variable str is "yes"

// str == "yes"

// [4-2] Find the total sum of integers from 1 to 20 that are not multiples of 2 or 3.

// [Attempt 01] Before learning loops 

// 1 <= x < 21

// x%2 != 0

// x%3 != 0

// int x = 1 ;

// if(1 <= x && x < 21){

// x ++;

// }else  if(x%2 != 0 || x%3 != 0 ){

// x += 1;

// x ++;

// }

// System.out.println("Answer 2: " + x);

// [Attempt 02] After learning loops

int sum1 = 0; // variable declaration and initialization for the sum

//for(){} // performing an operation on repeating numbers. 

// But since it's not an operation on unknown numbers..

for(int i=1;i<21;i++){ // filter 1 (1 <= i < 21) <-- integers from 1 to 20

//sum = sum + i; // first, the total sum 

// filter 2 (i%2==0 || i%3==0) <-- multiple of 2 or 3 condition

if(i%2!=0 || i%3!=0){

sum1 = sum1 + i; // the ultimate total

}

}

System.out.println(sum1);

//^ [4-3] Calculate the result of 1+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+...+10).

// [Attempt 01] Before learning loops 

// x 1

// x + (x + x+1) 1+(1+2)

// x + (x + x+1) + (x + (x + x+1)) 1+(1+2)+(1+2+3)

// [Attempt 02] After learning loops

int sum = 1;

// No common formula emerges...

//1 //sum

//(1+2) //sum=sum+i++

//(1+2+3) //sum=sum+i++

//(1+2+3+4) // ...

for(int i =2 ; i<11 ; i++){

sum=sum+sum+i;

}

System.out.println(sum);

// [4-4] When you keep adding in the pattern 1+(-2)+3+(-4)+..., 

// find up to what number you must add to make the total sum 100 or greater.

int sum4 =0;// total 

// don't know when to stop adding 

int i = 1; // loop variable

while(true){

sum4=(i%2==0)? sum4-i : sum4+i;

i++;

if(sum4>=100){

break;

}

}

System.out.println(i);

//^ [4-5] Convert the following for loop to a while loop.

// [Exercises]/ch4/Exercise4_5.java

// public class Exercise4_5 {

// public static void main(String[] args) {

for(int i5=0; i5<=10; i5++) {

for(int j=0; j<=i5; j++){

System.out.print("*");

}

System.out.println();

}

// Attempt 1)

int i5=0;

while(i5<=10){

for(int j=0;j<=i5 ;j++)

System.out.print("*");

System.out.println();

i5++;

}

// } // end of main

// } // end of class

// [4-6] Write a program that outputs all possible cases where the sum of two dice rolls

// equals 6.


// [4-8] Find all solutions of the equation 2x+4y=10. 

// x and y are integers and each range is 0<=x<=10, 0<=y<=10.

//  2y=5-x   //x=5-2y

// 0<=x && x<=10   //0~11 - 11 kinds, starts at 0

// 0<=y && y<=10   //0~11 - 11 kinds, starts at 0

// int x8 = (int)(Math.random()*11+0);

// int y8 = (int)(Math.random()*11+0);

// if(0<=x8 && x8<=10 || 0<=y8 && y8<=10){

// 2*x8 + 4*y8 = 10

// }

java_jungsuk3_연습문제_20160227.pdf
Download


// [4-9] When there is a string str consisting of numbers, complete the code that outputs

// the result of adding each digit. If the string is "12345", the result of '1+2+3+4+5',

// which is 15, should be output. Fill in the appropriate code for (1).

// [Hint] Use the charAt(int i) method of the String class


}

}







This English version was translated by Claude.

pdfjava_jungsuk3_연습문제_20160227.pdf
친절한 찰쓰씨
Written by
친절한 찰쓰씨

Pleasant Charles — UI/UX researcher at AIT. Keeping notes on design, planning, and slow days here since 2010.

More on the author's page

Keep reading

Renewal

Steadily, for the long haul, without burning out

Mar 31, 2026·9 min
Renewal

Tech-life balance

Feb 7, 2026·3 min
Renewal

Humanality, by Park Jeong-ryeol

Feb 7, 2026·11 min