#include <iostream>
using namespace std;
void readValues (int & ,int &); // 2 values A and B
int factorial (int &); // A
int divCount (int); // A
void evSum (int &, int &,long); // A and B and I should have the long sum
int main ()
{
int A,B; int choice;
do {
cout<<"Enter a positive integer A :"<<endl; // how do I use call by reference to ask the user for A and B?
cin>>A;
cout<<"Enter a positive integer B :"<<endl;
cin>>B;
}while (A<=0 || B<=0 || A>B);
do {
cout<<"Choose a computation from the following menu :"
<<"1- Compute sum of the Factorials of "<<A<<"."
<<"2- Count all divisors of "<<A<<"."
<<"3- Compute the sum of all even integers between "<<A<<" and "<<B<<"."
<<"4- Exit the program.";
cin>>choice;
switch (choice) {
case 1 : {
cout<<"The following program computes the sum of factorials of "<<A<<"."<<endl;
long int sumFactorial = 0; //I want to compute 1! + 2! + 3! .....A! should I put this in main or not?
for (int K=1; K<=A;K++);
sumFactorial += factorial (A); // error saying that overloaded
cout<<"The sum of factorials of "<<A<<" is equal to :"<<sumFactorial<<endl;
break; }
case 2 : {
cout<<"The following program counts all the divisors of "<<A<<"."<<endl;
int divcount;
divcount = divCount (A); // It should count all divisors of A just count them and output the number
cout<<"The number of divisors of "<<A<<" is equal to : "<<divcount<<endl;
break; }
case 3 : {
cout<<"The following program computes sum of even integers between "<<A<<" and "<<B<<"."<<endl;
long int evsum = 0; // it should count the even integers between A and B.
evSum (A,B,evsum);
cout<<"The sum of even integers between "<<A<<" and "<<B<<" is equal to :"<<endl;
break; }
case 4 : {
cout<<"Exit the program"<<endl;
break; }
default : {
cout<<"Wrong imput , Please choose a number from the menu."<<endl;
break; }
return 0;
}
}while (choice != 4);
}
long int factorial (int a) { // I need to use call by reference in these function to call the values
if(a == 0)
return 1; // I have some error that I can't understand . it's not working
else
return (a) * factorial(a - 1);
int divCount (int a) { // error on this brace
for (int i=1;i<=a;i++)
if (a % i == 0)
divCount++; // same in here I should call the values . I have error on "divCount" saying local function definitions are illegal.
return divCount;
void evSum (int a,int b) {
for ( int i>=a,i<=b,i++)
if (i % 2 == 0)
int evSum = 0; // I don't know if I should use "evsum" from main or "evSum"
evSum += i;
return evSum;
}
#include <iostream>
using namespace std;
void readValues (int & ,int &); // 2 values A and B
int factorial (int &); // A
int divCount (int); // A
void evSum (int &, int &,long); // A and B and I should have the long sum
int main ()
{
int A,B; int choice;
do {
cout<<"Enter a positive integer A :"<<endl; // how do I use call by reference to ask the user for A and B?
cin>>A;
cout<<"Enter a positive integer B :"<<endl;
cin>>B;
}while (A<=0 || B<=0 || A>B);
do {
cout<<"Choose a computation from the following menu :"
<<"1- Compute sum of the Factorials of "<<A<<"."
<<"2- Count all divisors of "<<A<<"."
<<"3- Compute the sum of all even integers between "<<A<<" and "<<B<<"."
<<"4- Exit the program.";
cin>>choice;
switch (choice) {
case 1 : {
cout<<"The following program computes the sum of factorials of "<<A<<"."<<endl;
long int sumFactorial = 0; //I want to compute 1! + 2! + 3! .....A! should I put this in main or not?
for (int K=1; K<=A;K++);
sumFactorial += factorial (A); // error saying that overloaded
cout<<"The sum of factorials of "<<A<<" is equal to :"<<sumFactorial<<endl;
break; }
case 2 : {
cout<<"The following program counts all the divisors of "<<A<<"."<<endl;
int divcount;
divcount = divCount (A); // It should count all divisors of A just count them and output the number
cout<<"The number of divisors of "<<A<<" is equal to : "<<divcount<<endl;
break; }
case 3 : {
cout<<"The following program computes sum of even integers between "<<A<<" and "<<B<<"."<<endl;
long int evsum = 0; // it should count the even integers between A and B.
evSum (A,B,evsum);
cout<<"The sum of even integers between "<<A<<" and "<<B<<" is equal to :"<<endl;
break; }
case 4 : {
cout<<"Exit the program"<<endl;
break; }
default : {
cout<<"Wrong imput , Please choose a number from the menu."<<endl;
break; }
return 0;
}
}while (choice != 4);
}
long int factorial (int a) { // I need to use call by reference in these function to call the values
if(a == 0)
return 1; // I have some error that I can't understand . it's not working
else
return (a) * factorial(a - 1);
int divCount (int a) { // error on this brace
for (int i=1;i<=a;i++)
if (a % i == 0)
divCount++; // same in here I should call the values . I have error on "divCount" saying local function definitions are illegal.
return divCount;
void evSum (int a,int b) {
for ( int i>=a,i<=b,i++)
if (i % 2 == 0)
int evSum = 0; // I don't know if I should use "evsum" from main or "evSum"
evSum += i;
return evSum;
}
To copy to clipboard, switch view to plain text mode
Bookmarks