## Java

Report
Question

### Please briefly explain why you feel this question should be reported .

Pass an integer argument and print the number of 2’s and the average of the digits of the argument passed.

solved 0
Java 8 months 2 Answer 636 views 0

## Answers ( 2 )

1. ### Please briefly explain why you feel this answer should be reported .

import java.util.Scanner;
public class Exercise1 {

public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print(“Input an integer: “);
int digits = in.nextInt();
System.out.println(“The average is “+avgDigits(digits));
System.out.println(“Number of 2 presents in the integer ” + (Digitstwo(digits)));
}

public static double avgDigits(int n){
double result;
double sum = 0;
double nlen = String.valueOf(n).length();
while(n > 0) {
sum += n % 10;
n/= 10;
}
return (double)(sum/nlen);
}

public static int Digitstwo(int n) {
String s = String.valueOf(n);
int result = s.length() – s.replace(“2”, “”).length();
return result;
}
}