Program to find the average of n numbers using class :)
1)
#include<iostream.h>
#include<conio.h>
class no
{
private:
int n,i;
int a[10],v;
public:
void read()
{
cout<<"\n Enter the elemnts: ";
cin>>n;
cout<<"\n Enter "<<n<<" elements below\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
void avg()
{
float sum=0;
for(i=0;i<n;i++)
{
sum+=a[i];
}
v=sum/n;
cout<<"\n Average= "<<v;
}
};
void main()
{
clrscr();
no n1;
n1.read();
n1.avg();
getch();
}
2)
#include<iostream.h>
#include<conio.h>
class no
{
private:
int n,i;
int a[10];
public:
void read()
{
cout<<"\n Enter the elemnts: ";
cin>>n;
cout<<"\n Enter "<<n<<" elements below\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
}
float avg()
{
float sum=0;
for(i=0;i<n;i++)
{
sum+=a[i];
}
return sum/n;
}
};
void main()
{
clrscr();
no n1;
n1.read();
cout<<"\n Average= "<<n1.avg();
getch();
}
No comments:
Post a Comment