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
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...
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....
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....
Question: Can I get the code in Java for this assignment to compare? Please and thank you....
Question: Can I get the code in Java for this assignment to compare? Please and thank you. Can I get the code in Java for this assignment to compare? Please and thank you. Description Write a Java program to read data from a text file (file name given on command line), process the text file by performing the following: Print the total number of words in the file. Print the total number of unique words (case sensitive) in the file. Print...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
Using Java, The program you will be writing displays a weekly payroll report. A loop in...
Using Java, The program you will be writing displays a weekly payroll report. A loop in the program should ask the user for the employee’s number, employee’s last name, number of hours worked, hourly pay rate, state tax, and federal tax rate. After the data is entered and the user hits the enter key, the program will calculate gross and net pay then displays employee’s payroll information as follows and asks for the next employees’ information. if the user works...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT