int n, s, input;

 

System.put,print("양수 ? ");

input=sc.nextInt();

 

n=1;

s=0;

while(n<=input){

s+=n;

n+=2;

} System.out.println("결과 : " +s);

== 

while(n<=input){

n+=2;

s+=n;

} System.out.println("결과 : " +(s+1));

n 에 먼저 2를 해주게 되면 1의 값은 더하지 않고 3부터 입력한 수까지의 홀수를 더하므로 마지막에 (s+1)을 해줘야함.

 

 

밑에 주석은 처음 풀어보라고 했을 때 

적어 놨던 건데 while 뒤를이상하게 했음. 뭔 생각으로 저렇게했는지 감이안잡히는군..

+ Recent posts