Write a for loop that computes the sum of all integers from


Q1. What is the output of the following code snippet

1.1

int i = 0;

while (i != 9)

{

System.out.println("" + i);

i = i + 2;

}

 

1.2.

int i;

int sum=0;

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

{

if(i>1)

sum=sum+i;

}

System.out.println("Sum is " + sum); System.out.println("last value of i is" + i);

Q2. 1 Convert the following for loop into a while loop and draw the flow chart.

int sum = 0;

for (int i = 10; i > 10; i--)

sum = sum + i


Q2.2 Write a for loop that computes the sum of all integers from 1 to 10.


Q3: Write complete Java programs, provide a screen snapshot showing your program in execution to receive full credit.


3.1 Generate the following output using any loop statement .

*
**
***
****
*****

3.2 In municipal elections office, voters are checked for eligibility and after registration they can vote. Write a program that asks the user about their name and age, and check if the voter is eligible to cast vote. (For eligibility age should be greater than 18)

Ask the user "do you want to take another voter details (Yes/No)" if the user presses Yes , take name and age again and check eligibility. Do it again until user presses No.

The following is a sample output when user enters Abdullah/23/Yes , Sarah/15/No.


Sample output:

Enter name: Abdullah

Enter Age :23

Abdullah can cast vote

Do you want to take another voter details (Yes/No) :Yes

Enter name :Sarah

Enter Age :15

Sarah cannot cast vote.

Do you want to take another voter details (Yes/No) : No

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Write a for loop that computes the sum of all integers from
Reference No:- TGS01164659

Now Priced at $25 (50% Discount)

Recommended (97%)

Rated (4.9/5)