Building Software I 2559
Sunday, September 13, 2015
Lab4 - Calculate sum of integers from 1 to N (1+2+3+...+N) using loop (not recursive function)
void setup(){
int numbers=10;
println("N = "+numbers);
sum_1_to_N(numbers);
}
void sum_1_to_N(int n){
int sum=0;
while(n>0){
if(n>1){
println(n+"+");
}else{
println("1 = ");
}
sum=sum+n;
n--;
}
println(sum);
println("Sum of 1 to N = "+sum);
}
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment