Calculate sum and Average in java

Ads

 
 
 

Share on Google+Share on Google+

Naulej kumar
Calculate sum and Average in java
1 Answer(s)      4 years and 11 months ago
Posted in : Java Beginners

How to calculate sum and average in java program?

Ads
View Answers

June 8, 2012 at 6:52 PM


Example:-

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class AvarageTest {
    public static void main(String[] args) throws IOException {

        System.out.println("Input any No.");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String data = (String) br.readLine();
        int num = Integer.parseInt(data);
        int sum = 0;
        float avg = 0.0f;

        for (int i = 1; i <= num; i++) {
            sum = sum + i;
        }
        avg = sum / num;
        System.out.println("Sum- " + sum);
        System.out.println("Avg- " + avg);

    }
}

Output

Input any Value.

Description:- In this example we are calculating sum and average of n numbers. The given code accepts the numbers from the user using BufferedReader class. Then using the for loop, we have calculated the sum of all numbers. And after dividing the sum by the total number of numbers, we have determined the average of numbers.


Ads









Related Tutorials/Questions & Answers:
Calculate sum and Average in java
Calculate sum and Average in java  How to calculate sum and average.... Description:- In this example we are calculating sum and average of n numbers... the sum by the total number of numbers, we have determined the average of numbers
How to calculate average of array in Java?
How to calculate average of array in Java?  Hi, I have an integer array and I want to calculate the average of the values in it. How to calculate average of array in Java? Thanks   Hi, You can iterate through
Advertisements
calculate average
calculate average   Design and write a java program to determine all three digit numbers such that the sum of the cubes of the digits is equal... and write a program and calculate your final average in this course. The details
calculate average
calculate average  Question 2 cont.d Test case c: home works/labs 10 9 9 9 10 10 9 9 10 10 9 9 10 test scores: 65 89 note, the program should work with different numbers of home works/labs
sum and average of grades
sum and average of grades  how to print a program which is sum and average of Korean (90), English(85), Mathematics(78), Biology(65), Chemistry(83
How to calculate the average in Hibernate?
How to calculate the average in Hibernate?  Hi, I have to calculate the average in Hibernate. How to calculate the average in Hibernate? Thanks... of calculating the average on the fee field in database: Projections.avg("fee
calculate sum of individual digit
calculate sum of individual digit   How calculate product of first and last digit of indiviual numbers Like a=43278 4*8=32
Calculate an average Age of my Class?
Calculate an average Age of my Class?  average age of my class
calculate sum of individual digit
calculate sum of individual digit  Hi How to calculate product of first snd last digit of indiviual digit Like Num=23456 S=2*6
Calculate sum of even and odd numbers in Java
Calculate sum of even and odd numbers In this section, you will learn how to read the file that contains even and odd numbers and calculate their sum... numbers from the file and then calculate their sum. Here is the code
Calculate the Sum of three Numbers
Calculate the Sum of Three Numbers       This is simple java programming tutorial . In this section you will learn how to calculate the sum of three numbers by using three
How to calculate sum of HH:MM:SS Format?
How to calculate sum of HH:MM:SS Format?  how to calculate sum of HH:MM:SS? for example: 10:25:36+26:50:56 calculate sum of these values
Example of Average, Sum, Min and Max functions in MySQL
Example of Average, Sum, Min and Max functions in MySQL  Hi, How to use the aggregate functions like Average, Sum, Min and Max in MySQL? Thanks... | +------------------+ 1 row in set (0.00 sec) mysql> select sum(email_count) from email
C calculate sum of first and last element of array
C calculate sum of first and last element of array In this section, you will learn how to calculate sum of first and last element from the array of five... and the last number and determine their sum. Here is the code
Average
Average   I have created a file that reads in a text file for a combo box which are exams assignments and quizes. I then have a a textfield..., assignments, etc.) so I can have the average of each item. I then need to graph
SQL Average Count
SQL Average Count       Average Count in SQL is used to count the aggregate sum of any field... demonstrative example ,which helps you to calculate the average count of any
Sum database colum in java
Sum database colum in java  please i have two columns-col1 and col2... the database to sum the Items and display output so that it will be like... question what you have asked. select item, sum(quqantity) from `order` group
sum
sum  a program to find the sum of the alternative diagit of it ex- no=123456 sum=1+3+5=9
Java Sum of Digits
Java Sum of Digits In this Java Tutorial section, you will learn how to find the sum of multidigit number. For this purpose, we have allowed the user to enter... for Sum of Digits in Java: import java.util.*; class SumOfDigits { public
sum
; sum=sum+s; if(i==n){ System.out.print(s...+"+"); } System.out.print("\nSum of the series : "+sum
How to calculate Date Difference in Java?
How to calculate Date Difference in Java?  Hi, I have two dates variable. How to calculate Date Difference in Java? Thanks   Hi, You can use the Calendar class for calculating the date difference in Java. Get data
how to find the sum of integers in a string of sentence in java
how to find the sum of integers in a string of sentence in java  how to find the sum of integers in a string of sentence in java for e.g:--> abc d 2 3 21 the output should be 2+3+21=26
Sum of Column in a Database Table
;    This section describes how we can calculate the sum... university where we need to calculate the sum of  numbers achieved... of that column of which we want to calculate the sum. If the statement gets processed
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
Calculating Sum of two Multidim
that will calculate the sum of the matrix. To make this program run we need to declare two multidimensional array of type int. Firstly calculate the length... Sum of Multidimensional Arrays   
Sum of two numbers using command line arguments in Java
? The calculate the sum of two numbers using command line arguments in Java... sum = a+b; Check complete example code at Add Two Numbers in Java. Thanks...Sum of two numbers using command line arguments in Java  Hi, How
Need a Code to calculate hair density in java
Need a Code to calculate hair density in java  Hello, Would you please give me a code to calculate the hair density on scalp from image in java
Sum of two Matrix
Sum of  two Matrix       In this section, we are going to calculate the sum of two matrix... to this. In this program we are going to calculate the sum of two matrix. To make this program
Student average
to maintain this status is to maintain an average not below 1.8. write a java program... should display the average grade of the student and would display the message...: 1.2 Enter number of units: 1 you got an average of 1.59 congratulation
1-2+5-10+17-26 sum the series - Java Beginners
1-2+5-10+17-26 sum the series  1-2+5-10+17-26 sum the series  Hey Ankit, Try the following code. public class SumOfSeries3 { /** * @param 'series' holds the series need to be printed */ StringBuffer
Calculate Company's Sale using Java
Calculate Company's Sales Using Java In this section, you will learn how to calculate Company's sale. A company sales 3 items at different rates and details...); System.out.println("Average Weekly Sale per day= " + sum / 7
Tutorials   
Java Spring Hibernate Struts Training ClassNotFoundException HttpRequestInterceptor java.lang.noclassdeffounderror: org/apache/http/httprequest noclassdeffounderror: org/apache/http/client/methods/httpurirequest java.lang.NoClassDefFoundError: org/apache/http/client/HttpClient How do I resolve this Java Class not found exception? httpclient java.lang.NoClassDefFoundError Apache Commons ioutils maven dependency Read/Convert an inputStream to a String What is the meaning of Java Platform? Why Java is a platform independent language? What is the benefits of learning Core Java? Which technology should I learn after Java? What is array in java with example? How to Convert ArrayList to Array? How to substring in Java? How to format number in Java? What is instance variable in Java? How to download MySQL JDBC driver? What is Calendar class in Java? Which is the best Java tutorials for beginners? How to rename a file in Java? How to delete file in Java code? How to get day from date in Java using Calendar? How to get day of week in Java? How to calculate Date Difference in Java? How to compare date in Java? How to declare array in Java? How to calculate average of array in Java? What is Array in Java? write a java program to find the summation of all the integers entered on command line Sum of two numbers using command line arguments in Java How to create and use Array in Java? How to pass command line arguments in Java? How to create Applet Hello World? Appending String efficiently in Java How to append String in Java? How to list even numbers between 1 and 100? How to add BigDecimal in Java? What is Abstraction In Java? Which is best Beginners Java Tutorial? What is java.util package? Create list from array in Java Filter collection in Java 8 What is the best way to filter a Java Collection? Easy way to transform Collection to Array? How to convert Collection to Array in Java? What are Basic Java Language Elements? Advanced Java Tutorials in 2017 Java brief history Best Reasons to learn Java

Ads

 
Advertisement null

Ads