Java

Report
Question

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

Report
Cancel

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

solved 0
Java 2 months 2 Answer 272 views 0

About Rajat

Answers ( 2 )

  1. rg123

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

    Report
    Cancel

    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;
    }
    }

    Best answer
  2. Please briefly explain why you feel this answer should be reported .

    Report
    Cancel

    Hello. And Bye.

Leave an answer

Browse

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>