Problem Instructions Write a program that gives the user a list of menu: 1) Addi
ID: 3634948 • Letter: P
Question
Problem InstructionsWrite a program that gives the user a list of menu: 1) Addition; 2) Multiplication; 3) Add squared of a number. Then prompt the user for a positive integer K. Depending on the selection made on the menu, you have to 1) add the first K positive integers; or 2) multiply the first K positive integers; or 3) add the first K squared positive integers. You should use recursion to write your function that handles the menu choices.
Example 1:
Do you want to add first k integers or multiply?
1) Addition.
2) Multiplication.
3) Add squared of a number.
1
Input the number of positive integer you want to perform: 10
The sum of the first 10 positive integers is: 55
Example 2:
Do you want to add first k integers or multiply?
1) Addition.
2) Multiplication.
3) Add squared of a number.
2
Input the number of positive integer you want to perform: 5
The product of the first 5 positive integers is: 120
Example 3:
Do you want to add first k integers or multiply?
1) Addition.
2) Multiplication.
3) Add squared of a number.
3
Input the number of positive integer you want to perform: 3
The sum of the first 3 squared positive integers is: 14
Explanation / Answer
Please Rate:Thanks
#include<iostream.h>
void main()
{
int ch;
cout<<"Do you want to add first k integers or multiply? "<<endl;
cout<<"1.Addition"<<endl;
cout<<"2.Multiplication"<<endl;
cout<<"3.Add squared of a number "<<endl;
cout<<"Enter your choice ";
cin>>ch;
switch(ch)
{
int n;
case 1:cout<<"Input the number of positive integer you want to perform: ";
cin>>n;
int sum=0;
for(int i=1;i<=n;i++)
sum=sum+i;
cout<<" The sum of first "<<n<<" positive integers is : "<<sum;
break;
case 2:cout<<"Input the number of positive integer you want to perform: ";
cin>>n;
int mul=1;
for(int i=1;i<=n;i++)
mul=mul*i;
cout<<" The product of first "<<n<<" positive integers is : "<<mul;
break;
case 3:cout<<"Input the number of positive integer you want to perform: ";
cin>>n;
int sum=0;
for(int i=1;i<=n;i++)
sum=sum+i*i;
cout<<" The sum of first "<<n<<" positive integers is : "<<sum;
break;
default:cout<<"Invalid choice";
}
}
--------------------------------------------------------------------------------
Output :
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.