{"id":2092,"date":"2017-05-22T07:16:27","date_gmt":"2017-05-22T07:16:27","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2092"},"modified":"2024-11-16T14:56:09","modified_gmt":"2024-11-16T14:56:09","slug":"java-program-to-calculate-the-mean-and-standard-deviation","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/java-program-to-calculate-the-mean-and-standard-deviation\/","title":{"rendered":"Program to Calculate the Mean &#038; Standard deviation"},"content":{"rendered":"<h1>Program Description<\/h1>\r\n<p>Write a java program to calculate the mean and standard deviation of the given population of numbers.<\/p>\r\n<h2>IPO Chart<\/h2>\r\n<h3>Input<\/h3>\r\n<p>We will take input from the user for the population of numbers.<\/p>\r\n<h3>Process\u00a0<\/h3>\r\n<p>mean = (x1 + x2 +&#8230; xn)\/n<\/p>\r\n<p>sd = sqrt( ((x1- mean)^2 + (x2- mean)^2 +&#8230;+ (xn- mean)^2 )\/n)<\/p>\r\n<h3>Output<\/h3>\r\n<p>The program will output the mean and standard deviation.<\/p>\r\n<h3>Environment &amp; Tools Used<\/h3>\r\n<p>The tools and environment used in the example are as follows:<\/p>\r\n<ul>\r\n<li>JDK<\/li>\r\n<li>Eclipse IDE<\/li>\r\n<li>Windows 10 Operating System<\/li>\r\n<\/ul>\r\n<h2>Java Program<\/h2>\r\n<p>Launch the Eclipse IDE. Create a new class for the program. Add the program code.<\/p>\r\n<p>The program uses Scanner class to take input from the user.\u00a0<\/p>\r\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Scanner; \r\n\r\npublic class StandardDeviation \r\n{  \r\n  public static final int ARRAYSIZE=4;\r\n\r\n  public static void main (String [] args)  \r\n  {  \r\n    System.out.println(\"Enter numbers to find Standard Deviation\");  \r\n    Scanner input = new Scanner(System.in);  \r\n    double[] arr= new double[ARRAYSIZE];  \r\n    double sum=0.0, mean=0.0,sumOfSquares=0.0,meanOfSquares=0.0;  \r\n    for (int i=0; i&lt;ARRAYSIZE; i++) \/\/Take input in the array  \r\n    {  \r\n      System.out.println(\"Enter a number : \");  \r\n      arr[i]=input.nextDouble();  \r\n      sum+=arr[i]; \/\/sum of all elements  \r\n    }  \r\n    mean=sum\/ARRAYSIZE;  \r\n\r\n    System.out.println(\"Mean of elements := \"+mean); <br \/>\/\/Display mean of all elements  \r\n    double[] sd= new double[ARRAYSIZE];  \r\n    for (int i=0; i&lt;ARRAYSIZE; i++) \/\/calculate standard deviation  \r\n    {  \r\n      sd[i]=Math.pow((arr[i]-mean),2);  \r\n      sumOfSquares+=sd[i];  \r\n    }  \r\n    meanOfSquares=sumOfSquares\/(ARRAYSIZE);  \r\n    double deviation=Math.sqrt(meanOfSquares);  \r\n    System.out.println(\"Standard Deviation := \"+ deviation);  \r\n  }  \r\n}  \r\n<\/pre>\r\n<h3>Program Output<\/h3>\r\n<p>Save the program in the Eclipse IDE and run the application. Right-click in the code editor and choose <strong>Run As &gt;&gt; Java Application<\/strong> to run the program.<\/p>\r\n<p>Enter numbers to find Standard Deviation<br \/>Enter a number : 7<br \/>Enter a number : 4<br \/>Enter a number : 5<br \/>Enter a number : 2<br \/>Mean of elements := 4.5<br \/>Standard Deviation := 1.8027756377319946<\/p>\r\n<h3>Screenshot<\/h3>\r\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-2093 size-full\" title=\"program to calculate the mean\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Standard-Deviation-Java-Program.png\" alt=\"program to calculate the mean\" width=\"1721\" height=\"879\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Standard-Deviation-Java-Program.png 1721w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Standard-Deviation-Java-Program-300x153.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Standard-Deviation-Java-Program-1024x523.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Standard-Deviation-Java-Program-768x392.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Standard-Deviation-Java-Program-1536x785.png 1536w\" sizes=\"auto, (max-width: 1721px) 100vw, 1721px\" \/><\/p>\r\n<p>&#8212;<\/p>\r\n<p>Java Tutorial on this website:<\/p>\r\n<p><strong><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/strong><\/p>\r\n<p>More information on Java can be found on the official website:<\/p>\r\n<p><strong><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/strong><\/p>\r\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2092\" data-postid=\"2092\" class=\"themify_builder_content themify_builder_content-2092 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Program Description Write a java program to calculate the mean and standard deviation of the given population of numbers. IPO Chart Input We will take input from the user for the population of numbers. Process\u00a0 mean = (x1 + x2 +&#8230; xn)\/n sd = sqrt( ((x1- mean)^2 + (x2- mean)^2 +&#8230;+ (xn- mean)^2 )\/n) Output [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[],"class_list":["post-2092","post","type-post","status-publish","format-standard","hentry","category-java","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2092","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2092"}],"version-history":[{"count":13,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2092\/revisions"}],"predecessor-version":[{"id":21191,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2092\/revisions\/21191"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}