Java Programming :How to add ten numbers using while loop and arrays.
Step 1:
declare and initialize 3 variables. integer i as counter and integer sum.
Step 2:
declare and initialize array integer type, size of the array is ten.
Step 3: while loop to generate the numbers and add the numbers.
Step 4:
Print out the result.
Full code below :
// How to add 10 numbers using while
public class AddTenNumbers{
public static void main(String[] args){
int i = 0 ; // as counter
int sum = 0 ; // sum up the total
int number[] = new int[10]; // use array to generate ten numbers
// use while to generate the numbers and sum up the numbers
while( i < 10){
// Generate the 10 numbers
number[i] = i + 1 ;
// SUm the numbers
sum = sum + number[i];
i++; // forget this
}
// Print the sum
System.out.println("SUmmation of 1 till 10 is : " + sum);
}
}
No comments:
Post a Comment