You need to create a program that uses loops to perform the following steps.
1. Allow the user to input two integers. Variables: Num1 and Num2 (Num1 must be less than Num2, values must be a positive number) (use loop); create a user-defined function to validate the user's input.
2. Output all even numbers between Num1 and Num2. (use loop); create a user-defined function. The function is a void function.
3. Output the sum of all odd numbers between Num1 and Num2. (use loop); create a user-defined function called sum(). Declare a variable called sumodd in the main() for the sum(). sum() is a value returning function. Use sumodd to hold a returned value.
#include<iostream>
using namespace std;
void even(int a,int b){
cout<<"Even numbers"<<endl;
while (a<=b){
if(a%2==0){ cout<<a<<endl; }
++a;
}
}
int odd(int a,int b){
cout<<"Odd numbers"<<endl;
int s=0;
while (a<=b){
if(a%2==1){ cout<<a<<endl;
s=s+a;
}
++a;
}
return s;
}
void dation(){
int num1,num2;
bool d=false;
while(d==false){
cout<<"Enter First number"<<endl;
cin>>num1;
cout<<"Enter second number"<<endl;
cin>>num2;
if((num1<num2) && (num1>=0)){
d=true;
cout<<"first value="<<num1<<" second value="<<num2<<endl;
}else{
cout<<"Entered wrong values:\n both values must be positive and first value must be less than second value\n try again"<<endl;
}
}
}
int main(){
int a,b;
cout<<" enter num1 and num2"<<endl;
cin>>a>>b;
dation();
int sumodd=odd(a,b);
even(a,b);
cout<<"sum odd="<<sumodd<<endl;
return 0;}
Comments
Leave a comment