Search This Blog

UI/UX Design | Web Development | Mobile Application Development

Whether you need UI/ UX design, web development services, or mobile app development services, Qaabil Digitals will be more than happy to help you. We work with top software developers, designers, and project managers. We tailor our services to suit your exact needs. We closely look at your company, listen to your story, and bring about the best ideas and solutions that can help you drive solid results. We don’t just produce work that works. We produce work that matters. Get a quote now Or Talk to us

Thursday, 1 September 2011

Java program to find average and add two numbers taken from user

Finding addition and average of two numbers is also very simple in java. As you will see it is little bit different from another program which finds sum and product of two numbers. There is its code:

import java.util.*;
class sumavg
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a,b,s,avg;
System.out.print ("\n\t Enter first number = ");
a=in.nextInt();
System.out.print ("\n\t Enter second number = ");
b=in.nextInt();
s=a+b;
avg=s/2;
System.out.print ("\n\t sum = "+s);
System.out.print ("\n\t avg = "+avg);
}
}

Note:
          Look at this "avg=s/2" statement. We have sum of the numbers in s and that's why we are dividing s by 2 to calculate average of these numbers. You can also do that in a different way like:
avg=(a+b)/2;
Share