In: Computer Science
Using c++, Write a program to perform the multiplication of 10 consecutive number starting from 5?
Write a program to perform the summation of 10 even number starting from 2?
Write a program to perform the summation of 10 odd number starting from 2?
Write a program to perform the summation of 10 number starting from 2 and increment is
given by user?
Write a program to combine all operations from 1 to 4 in a single program using ‘Switch’
statement. Ask user whether they want to continue or not after one run of the program.
A program to perform the multiplication of 10 consecutive numbers starting from 5?
#include <iostream>
using namespace std;
int main()
{
long n = 1;
for(int i=5; i<15; i++)
{
n = n*i;
}
cout<<n;
return 0;
}
OUTPUT:
A program to perform the summation of 10 even numbers starting from 2?
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for(int i=2; i<=20; i++)
{
if(i%2==0)
sum = sum + i;
}
cout<<sum;
return 0;
}
OUTPUT:
A program to perform the summation of 10 odd numbers starting from 2?
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for(int i=2; i<=21; i++)
{
if(i%2!=0)
sum = sum + i;
}
cout<<sum;
return 0;
}
OUTPUT:
A program to perform the summation of 10 numbers starting from 2 and increment is given by the user?
#include <iostream>
using namespace std;
int main()
{
int sum = 2, inc, nextTerm;
cout<<"Enter the increment value: ";
cin>>inc;
nextTerm = sum+inc;
for(int i=1; i<10; i++)
{
sum = sum + nextTerm;
nextTerm = nextTerm + inc;
}
cout<<sum;
return 0;
}
OUTPUT:
Write a program to combine all operations from 1 to 4 in a single program using the ‘Switch’ statement. Ask the user whether they want to continue or not after one run of the program.
#include <iostream>
using namespace std;
void mul10Con()
{
long n = 1;
for(int i=5; i<15; i++)
{
n = n*i;
}
cout<<n;
}
void sum10Even()
{
int sum = 0;
for(int i=2; i<=20; i++)
{
if(i%2==0)
sum = sum + i;
}
cout<<sum;
}
void sum10Odd()
{
int sum = 0;
for(int i=2; i<=21; i++)
{
if(i%2!=0)
sum = sum + i;
}
cout<<sum;
}
void sum10User()
{
int sum = 2, inc, nextTerm;
cout<<"Enter the increment value: ";
cin>>inc;
nextTerm = sum+inc;
for(int i=1; i<10; i++)
{
sum = sum + nextTerm;
nextTerm = nextTerm + inc;
}
cout<<sum;
}
int main()
{
int n;
char c;
while(true)
{
cout<<endl<<"1: Write a program to perform the
multiplication of 10 consecutive number starting from 5";
cout<<endl<<"2: Write a program to perform the
summation of 10 even number starting from 2";
cout<<endl<<"3: Write a program to perform the
summation of 10 odd number starting from 2";
cout<<endl<<"4: A program to perform the summation of
10 numbers starting from 2 and increment is given by the
user";
cout<<endl<<endl<<"Enter your choice: ";
cin>>n;
switch(n)
{
case 1:
mul10Con();
break;
case 2:
sum10Even();
break;
case 3:
sum10Odd();
break;
case 4:
sum10User();
break;
}
cout<<"\nDo you want to continue (y/n): ";
cin>>c;
if(c=='y' || c=='Y')
continue;
else
break;
}
return 0;
}
OUTPUT: