AIM:
To write a Java program to find the sum of numbers and average using interface.
ALGORITHM:
Step 1: Start
Step 2: Define the interface inter1
Step 3: Define the class A which implements the interface inter1
Step 4: Initialize the variable sum
Step 5: for i=0 to n-1 sum+=a[i]
Step 6: Declare the object for class A
Step 7: Read the input values to find sum
Step 8: Calculate sum and average
Step 9: Stop
SOURCE CODE:
testinterface.java
import java.io.*;import java.lang.*;
interface inter1
{
public int avg(int a[], int n);
}
class A implements inter1
{
public int avg(int a[], int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
}
return sum;
}
}
class testinterface
{
public static void main(String args[]) throws IOException
{
A a=new A();
int z;
int arr[]=new int[100];
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the number of integers to find the sum");
String str=in.readLine();
int num=Integer.parseInt(str);
for(int i=0;i<num;i++)
{
System.out.println("Enter the number "+(i+1)+":");
str=in.readLine();
z=Integer.parseInt(str);
arr[i]=z;
}
int result=a.avg(arr,num);
System.out.println("The total is "+result);
System.out.println("The average is "+(float)result/num);
}
}
OUTPUT :
RESULT:
Thus the Java program for finding the sum and average using interface was performed and the output was verified.
.
EmoticonEmoticon