Question

In: Computer Science

java code Question 4: Iterating with loops You can process the list using a For loop....

java code

Question 4:

Iterating with loops

You can process the list using a For loop. The variable in the For loop becomes the index value for each of the items in the loop. Everything you do to an element inside the loop gets done for the entire list. Examine the following loops. Then use what you know to write the following loops.

int[] numbers = new int[100];

for(int i = 0; i < numbers.length; i++){

numbers[i] = i;

}

int sum = 0;

for(int i = 0; i < numbers.length; i++){

sum += numbers[i];

}

  1. Write a loop to display all the numbers in the list.

  1. Write a loop to display the number in the list in reverse order.

  1. Write a loop with an if statement to print only the even numbers in the list.

  1. Write a loop to subtract the value 50 to every element in the list.

  1. Write a loop to count the number of negative values in the list.


Solutions

Expert Solution

public class Demo {

public static void main(String args[]) {

int[] numbers=new int[100];

for(int i=0;i<numbers.length;i++)

{

numbers[i]=i;

}

int sum=0;

for(int i=0;i<numbers.length;i++)

{

sum+=numbers[i];

}

  

//Write a loop to display all the numbers in the list.

for(int i=0;i<numbers.length;i++)

{

System.out.print(numbers[i]+" ");

}

  

//Write a loop to display the number in the list in reverse order.

for(int i=numbers.length-1;i>=0;i--)

{

System.out.print(numbers[i]+" ");

}

  

  

//Write a loop with an if statement to print only the even numbers in the list.

for(int i=0;i<numbers.length;i++)

{

if(numbers[i]%2==0)

System.out.print(numbers[i]+" ");

}

  

//Write a loop to subtract the value 50 to every element in the list.

for(int i=0;i<numbers.length;i++)

{

numbers[i]=numbers[i]-50;

}

  

//Write a loop to count the number of negative values in the list.

int count=0;

for(int i=0;i<numbers.length;i++)

{

if(numbers[i]<0)

count++;

}

  

}

}

// If any doubt please comment


Related Solutions

Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
write a java code program using loops to compute the sum of the 30 terms of...
write a java code program using loops to compute the sum of the 30 terms of the series below. Find sum of all integers between 100 and 200 which are divisible by 9 or 13 Write a program to find the sum of the first 8 terms of the series 1 + 11 + 111 + 1111 + . . . Output: Term Ratio Sum
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve...
Create Python Code using a "for" loop and a "while" loop. You are starting a twelve week program training to compete in a triathlon. The triathlon consists of three athletic events, 1.5 k swim, 40k bike, 10k run. In order to be prepared for the competition you want to print a training schedule. Starting with week 1 you will increase the distance of each activity so that you reach the race distance by week twelve. Due to rounding, you may...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number...
CODE MUST BE IN C++ (please use for loop) write a program that loops a number from 1 to 10 thousand and keeps updating a count variable (count variable starts at 0 ) according to these rules: n1 = 14 n2 = 54 n3 = 123 if the number is divisible by n1, increase count by 1 if the number is divisible by n2, increase count by 2 if the number is divisible by n3, increase count by 3 if...
Question 1: What is For Loop? Explain with 2 Examples. You can write code in any...
Question 1: What is For Loop? Explain with 2 Examples. You can write code in any programming language.
Create a Java application NumberLoops to practice loops. The draft has one loop to calculate a...
Create a Java application NumberLoops to practice loops. The draft has one loop to calculate a product and the final will add another loop to print integers in a required format. There is no starter code for the problem. Use a Scanner to input an integer then compute and display the product of all positive integers that are less than the input number and multiple of 3. If no numbers satisfy the conditions, then print out 1 for the product....
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write...
JAVA CODE, USE FOR LOOP PLEASE Using the PurchaseDemo program and output as your guide, write a program that uses the Purchase class to set the following prices, and buy the number of items as indicated. Calculate the subtotals and total bill called total. Using the writeOutput() method display the subtotals as they are generated as in the PurchaseDemo program. Then print the total bill at the end Use the readInput() method for the following input data Oranges: 10 for...
Design of 4 Bit Adder/Subtractor using Loops (Behavior Modeling Style) (verilog Code) -
Design of 4 Bit Adder/Subtractor using Loops (Behavior Modeling Style) (verilog Code) -
All code is done using Java. The steps to the question are as follows (There is...
All code is done using Java. The steps to the question are as follows (There is more information in the code comments posted below as well): Begin by deciding how many fields are required and what their types should be. Add these fields to your class (making sure that they each have a private access modifier) giving them a sensible name when you do so. Once you have added the fields to your class, implement the methods getLevel and getWidth....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT