Write an assignment statement to calculate the sum of the number from 1 through n
using Gauss'formula
sum=n(n+1)/2
Code
int n=20;
int sum=0;
sum=n*(n+1)/2;
print(sum);
Output
210
Text book name : Programming and Problem solving with Java second edition
Author : Nell Dale and Chip Weems
ISBN : 978-0-7637-3402-2
ไม่มีความคิดเห็น:
แสดงความคิดเห็น