C++. Write a program that uses while loops to perform the following steps: a. Pr
ID: 3857589 • Letter: C
Question
C++. Write a program that uses while loops to perform the following steps:
a. Prompt the user to input two positive integers. Variables: firstNum and secondNum (firstNum must be less than secondNum) (use while loop); create a user-defined function called validateUserInput() to validate the user's input. Use Call-by-Value. validateUserInput() is a value returning function.
b. Output all odd numbers between firstNum and secondNum. (use while loop); create a user-defined function called oddNumbers(). Use Call-by-Value. oddNumbers() is a void function.
c. Output the sum of all even numbers between firstNum and secondNum. (use while loop); create a user-defined function called sumEvenNumbers(). Use Call-by-Value. Declare a variable called sumEven in the main() for the sumEvenNumbers(). sumEvenNumbers() is a value returning function. Use sumEven to hold a returned value.
d. Output the numbers and their squares between 1 and 10. (use while loop): create a user-defined function called displaySquareNumbers(). Call-by-Value. displaySquareNumbers() is a void function.
e. Output the sum of the square of the odd numbers between firstNum and secondNum. (use while loop); create a user-defined function called sumSqureOddNumbers(). Use Call-by-Value. Declare a variable called sumSquareOdd in the main(), for the sumSqureOddNumbers(). sumSqureOddNumbers() is a value returning function. Use sumSquareOdd to hold a returned value.
f. Output all uppercase letters. (use while loop); create a user-defined function called displayUppercaseLetters(). Use Call-by-Value. displayUppercaseLetters() is a void function.
OUTPUTS:
**************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers: 8 a
Incorrect Input.
Please try again.
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers:
**************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers: a 8
Incorrect Input.
Please try again.
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers:
**************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers: 8 2
First number must be less than the second number!
Please try again.
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers:
**************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers: -2 8
No negative numbers!
Please try again.
Enter two positive integer numbers.
First number must be less than the second number:
Enter numbers:
**************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number you enter
Enter numbers: 2 8
Odd integers between 2 and 8 are:
3 5 7
Sum of even integers between 2 and 8 = 20
Number Square of Number
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Sum of the squares of odd integers between 2 and 8 = 83
Upper case letters are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Do you want to repeat this program?
y/n
> y
**************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number you enter
Enter numbers: 1 9
Odd integers between 1 and 9 are:
1 3 5 7 9
Sum of even integers between 1 and 9 = 20
Number Square of Number
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Sum of the squares of odd integers between 1 and 9 = 165
Upper case letters are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Do you want to repeat this program?
y/n
> y
**************************************************************************************
Enter two positive integer numbers.
First number must be less than the second number you enter
Enter numbers: 11 15
Odd integers between 11 and 15 are:
11 13 15
Sum of even integers between 11 and 15 = 26
Number Square of Number
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
10 100
Sum of the squares of odd integers between 11 and 15 = 515
Upper case letters are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Do you want to repeat this program?
y/n
> n
**************************************************************************************
Explanation / Answer
CPP Program :
#include <iostream>
using namespace std;
bool validateUserInput(string s1, string s2){
int a = 0,b=0;
int sign = +1;
int i=0;
if(s1.at(0)=='-'){
sign = -1;
i = 1;
}
for(;i<s1.length();i++){
if(s1.at(i)>='0'&&s1.at(i)<='9'){
a = 10*a+(s1.at(i)-48);
}else{
cout<<"Incorrect Input."<<endl<<"Please try again"<<endl<<endl;
return false;
}
}
a = a*sign;
i = 0;
sign = 1;
if(s2.at(0)=='-'){
sign=-1;
i=1;
}
for(;i<s2.length();i++){
if(s2.at(i)>='0'&&s2.at(i)<='9'){
b = 10*b+(s2.at(i)-48);
}else{
cout<<"Incorrect Input."<<endl<<"Please try again"<<endl<<endl;
return false;
}
}
b = b*sign;
if(a>b){
cout<<"First number must be less than the second number"<<endl<<"please try again"<<endl<<endl;
return false;
}
if(a<0||b<0){
cout<<"No Negative numbers"<<endl<<"Please try again"<<endl<<endl;
return false;
}
return true;
}
int main()
{
int firstNum,secondNum;
string s1,s2;
while(true){
cout<<"Entere two positive integers"<<endl<<"First number must be less than the second "<<endl<<"Enter numbers : ";
cin>>s1>>s2;
cout<<endl;
bool valid = validateUserInput(s1,s2);
if(!valid){
continue;
}
int a = 0,b=0;
for(int i=0;i<s1.length();i++){
if(s1.at(i)>='0'&&s1.at(i)<='9'){
a = 10*a+(s1.at(i)-48);
}else{
cout<<"Incorrect Input."<<endl<<"Please try again"<<endl<<endl;
return false;
}
}
for(int i=0;i<s2.length();i++){
if(s2.at(i)>='0'&&s2.at(i)<='9'){
b = 10*b+(s2.at(i)-48);
}else{
cout<<"Incorrect Input."<<endl<<"Please try again"<<endl<<endl;
return false;
}
}
int start = a;
if(start%2==0){
start++;
}
cout<<"Odd integers between "<<a<<" and "<<b<<" are "<<endl;
for(int i=start;i<=b;i+=2){
cout<<i<<endl;
}
start = a;
if(start%2==1){
start++;
}
int sum = 0;
for(int i=start;i<=b;i+=2){
sum = sum+i;
}
cout<<"sum of even numbers is "<<sum<<endl;
cout<<"Number square of Number"<<endl;
int os = 0;
for(int i=a;i<=b;i++){
cout<<i<<" "<<i*i<<endl;
if(i%2==1){
os = os+i*i;
}
}
cout<<"sum of square of odd integers between "<<a<<" and "<<b<<" is "<<os<<endl;
cout<<"Upper case letters are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z"<<endl<<endl;
cout<<"Do you want to repeat this program?"<<endl<<"y/n"<<endl;
char ch = 'n';
cin>>ch;
if(ch=='n'){
break;
}
}
return 0;
}
OUTPUT :
Entere two positive integers
First number must be less than the second
Enter numbers : 2 8
Odd integers between 2 and 8 are
3
5
7
sum of even numbers is 20
Number square of Number
2 4
3 9
4 16
5 25
6 36
7 49
8 64
sum of square of odd integers between 2 and 8 is 83
Upper case letters are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Do you want to repeat this program?
y/n
y
Entere two positive integers
First number must be less than the second
Enter numbers : 1 9
Odd integers between 1 and 9 are
1
3
5
7
9
sum of even numbers is 20
Number square of Number
1 1
2 4
3 9
4 16
5 25
6 36
7 49
8 64
9 81
sum of square of odd integers between 1 and 9 is 165
Upper case letters are: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Do you want to repeat this program?
y/n
n
Process returned 0 (0x0) execution time : 24.284 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.