I can\'t figure out how to write a program for these questions for Java. Please
ID: 3820830 • Letter: I
Question
I can't figure out how to write a program for these questions for Java. Please help. 1) Write a program that reads a number of integer values and returns the average. Use a sentinel control loop that terminates the iterations when -1 is entered. 2) Write a program that prints Celsius/Fahrenheit conversion table such as the following: Celsius Fahrenheit 0 32 10 50 20 68 100 212 3) Write a program with loops that compute: A) The sum of all even numbers between 2 and 100 (inclusive). B) The sum of all squares between 1 and 100 (inclusive). All powers of 2 from 20 up to 220. C) The sum of all odd numbers between a and b (inclusive), where a and b are inputs. D) The sum of all odd digits of an input. (For example, if the input is 32677, the sum would be 3 + 7 + 7 = 17). 4) The Fibonacci numbers are defined by the sequence: f1=1 f2=1 fn=fn-1 +fn-2 Implement a program that prompts the user for an integer, n, and prints all the Fibonacci numbers, up to the nth Fibonacci number. 5) Write a program that asks the user for an integer and then prints out all its factors. For example, when the user enters 150, the program should print: 2 3 5 5
Explanation / Answer
You have mixed multiple questions. Answering first.
1) Write a program that reads a number of integer values and returns the average. Use a sentinel control loop that terminates the iterations when -1 is entered
import java.util.Scanner;
public class Avergae {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int num;
double total = 0;
int count = 0;
while(true)
{
num = sc.nextInt();
if (num == -1)
{
break;
}
total += num;
count++;
}
System.out.println("Average is : " + total/count);
sc.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.