Answer to Question #156272 in C++ for allan

Question #156272

You need to create a program that uses loops to perform an output of all even numbers between Num1 and Num2. (use loop); create a user-defined function. The function is a void function.


1
Expert's answer
2021-01-17T20:55:37-0500
#include<iostream> // library for standard i/o
using namespace std; // allows to name objects
bool validate(int v1, int v2) { // function to validate user input
  bool v = false;
    if (v1 < v2 && v1 > 0 && v2 > 0) {  v = true;   } // if the user input is valid retun true
    else {  v=false;  }      // else return false  
    return v; // return v
  }
void printeven(int v1, int v2){  // function to print even numbers
        cout<<"Even numbers between Num1 and Num2 are :"<<endl;
        for(int i=v1;i<v2;i++){
                if(i%2==0){   cout<<i<<" ";   }  // display even number
        }
        cout<<endl;
}
int sum(int v1, int v2){ // function to count odd sum
        int s=0;
        for(int i=v1;i<v2;i++){
                if(i%2==1){  s=s+i; } // add the odd number to s
        }
        return s; // return s
}
int main(){ // main function
 int Num1,Num2;
 cout<<"Enter first value : ";
 cin>>Num1; // user input 
 cout<<"Enter second value : ";
 cin>>Num2; // user input
 if(validate(Num1,Num2)){ // passing parameters to validate function
        printeven(Num1,Num2); // passing parameters to printeven function
    int sumodd=sum(Num1,Num2); // passing parameters to sum function
    cout<<"Sum odd is : "<<sumodd<<endl; // diaplay sumodd
 }
 else{
        cout<<"Invalid input";
 }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment