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