C.E/Java
1부터 n까지의 합을 계산하여 출력하는 자바프로그램을 작성
숨[Breath]
2013. 10. 19. 09:47
wilhe문 사용
n은 정수형 변수로 선언할 것
다음 n값들에 대해 테스트할 것
0, 1, 5, 10, -4
public class P003 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a=1,b=0,c=0;
// a=초기값,b=목적값,c=합
while(a<=b){
c=c+a;
a++;
}
System.out.println(c);
a=1; //초기값
b=1; //목적값 1
c=0; //초기화
while(a<=b){
c=c+a;
a++;
}
System.out.println(c);
a=1; //초기값
b=5; //목적값 5
c=0; //초기화
while(a<=b){
c=c+a;
a++;
}
System.out.println(c);
a=1; //초기값
b=10; //목적값 10
c=0; //초기화
while(a<=b){
c=c+a;
a++;
}
System.out.println(c);
a=1; //초기값
b=-4; //목적값 -4
c=0; //초기화
while(a<=b){
c=c+a;
a++;
}
System.out.println(c);
}
}