Question

1) use java to print the sum of the list (any #s) using keyboard, also using...

1) use java to print the sum of the list (any #s) using keyboard, also using a while loop and for-loop and do-loop. ( 3 separate programs ).
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;

public class Main

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

System.out.println("Enter array size");

int n = sc.nextInt();

int arr[] = new int[n];

int i;

System.out.println("Enter elements of Array");

for(i=0;i<n;i++)

arr[i]= sc.nextInt();

int for_loop_sum = 0;

for(i=0;i<n;i++)

for_loop_sum+=arr[i];

System.out.println("Sum using for loop:" + for_loop_sum);

int while_loop_sum = 0;

i=0;

while(i<n)

{

while_loop_sum+=arr[i];

i++;

}

System.out.println("Sum using while loop:" + while_loop_sum);

i = 0;

int do_while_sum = 0;

do{

do_while_sum+=arr[i];

i++;

}while(i<n);

System.out.println("Sum using do_while loop:" + do_while_sum);

}

}

Add a comment
Know the answer?
Add Answer to:
1) use java to print the sum of the list (any #s) using keyboard, also using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT