READ FIRST - This is for JAVA programmming language, and is a very basic class,
ID: 647286 • Letter: R
Question
READ FIRST - This is for JAVA programmming language, and is a very basic class, so all I need is the answer in the easiest way it is possible to do it in, given the question....Question is 3 parts (not 3 different questions so could not split up into different threads)
1.) Find the average of all positive even numbers less than 100.
Answer: You can either write the code within the main method of only inside a method.
double getAverage()
{
double sum=0.0;
int cout=0;
for (int i=2;i<100;i++,count++)
sum+=i;
return sum/count
2.) Write a method that meets the requirements of the following comment header block:
// The purpose of this method is to get the sum from an array of integers.
// Input: numbers the array of integers
// Return: sum the sum of the array
3.) Next, declare a one dimensional array to store first 100 even numbers. Use the method in the previous question to accomplish this.
Explanation / Answer
import java.util.*;
class abc
{
double sum=0.0;
int count=2;
// int sum=0;
public static void main(String args[])
{
abc a = new abc();
int d= a.getAverage();
double a1= a.evenno();
System.out.println("sum of 100 even number"+a1);
}
int getAverage() //
{
int array[]= new int[100];
int sum1=0;
int ss;
count=2;
for (int i=0;i<100;i++) // input the 100 number and sum
{
Scanner sc= new Scanner(System.in);
ss=sc.nextInt();// input integer
sum1=sum1+ss;// addtion of number
}
System.out.println("sum of 100 number"+sum1);
return sum1; //sum of 100 number
}
double evenno() // for even
{
int array[]= new int[100];
int i;
double sum=0.0;
//int count=2;
for ( i=0;i<100;i++)
{
sum=sum+count; // sum of even number
count=count+2;
System.out.println(count);
System.out.println(" "+sum);
}
sum=sum/i;
return sum;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.