Search This Blog

If you have any problem or question please write in the comments. We will try to solve and send you the solution. Not just that we will also post your problems solution on our blog.

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;

Java program to find sum and product of two numbers

It is very easy to find sum and product of two numbers in java. First of all we must take two numbers from user and then we will add and multiply these numbers. Lets see how, it is

import java.util.*;
class sumproduct
{
public static void main (String args[])
{
Scanner in = new Scanner (System.in);
int a,b,s,p;
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;
p=a*b;
System.out.print ("\n\t sum = "+s);
System.out.print ("\n\t product = "+p);
}
}

Note:
          After we have the numbers, take two variables for sum and product. As we have s and p in this program.