- 11th Nov 2022
- 03:54 am
- Admin
This is a C++ program that allows the user to enter up to 9 integer values and then provides options to calculate the average, sum, or both of the entered numbers based on user choice
#include
using namespace std;
int main()
{
int x[9];
int k=0;
char ch;
do
{
if(k<=9)
{
cout<<"Enter the values :- ";
cin>>x[k];
k++;
cout<<"Enter y or Y if you want to continue :-";
cin>>ch;
}
else
{
cout<<"Max entry reached\n";
break;
}
}while((ch=='y')||(ch=='Y'));
int ent;
cout<<"Enter 1 to calculate avg. of numbers entered \n 2 to calculate sun of numbers entered \n 3 to calculate both sum and avg of entered numbers";
cout<<"\nYour entry:- ";
cin>>ent;
switch(ent)
{
case 1 :
{
float avg,sum=0.0;
for(int i =0;i {
sum=sum+x[i];
}
avg=sum/k;
cout<<"\n The avg of given no. is "< break;
}
case 2 :
{
int sum=0;
for(int i =0;i {
sum=sum+x[i];
}
cout<<"\n The sum of given no. is "< break;
}
case 3 :
{
float avg,sum=0.0;
for(int i =0;i {
sum=sum+x[i];
}
avg=sum/k;
cout<<"\n The avg of given no. is "< cout <<" and sum is "< break;
}
default :
{ cout<<"\nWrong entry";
break;
}
}
return 0;
}
Overall, this program is designed to allow the user to enter multiple values and then choose to calculate the average, sum, or both of those values.
If you have any particular inquiries or need additional explanations regarding the program, please don't hesitate to ask!