In: Computer Science
C++. Write a program that uses for 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). Validate the user's input; prompt the user
again if firstNum is not less than secondNum (use for
loop).
b. Output all odd numbers between firstNum and
secondNum. (use for loop).
c. Output the sum of all even numbers between firstNum and
secondNum. (use for loop).
d. Output the numbers and their squares between 1 and 10. (use for
loop).
e. Output the sum of the square of the odd numbers between
firstNum and secondNum. (use for loop)
f. Output all uppercase letters. (use for loop).
#include <iostream>
#include <string>
using namespace std;
int main() {
int firstNum,secondNum;
cout<<"Enter first number : ";
cin>>firstNum;
cout<<"Enter second number : ";
cin>>secondNum;
if(firstNum>=secondNum){
cout<<"First number should be less than second number\n";
for(int i=0;firstNum>=secondNum;i++){
cout<<"Enter first number : ";
cin>>firstNum;
cout<<"Enter second number : ";
cin>>secondNum;
}
}
cout<<"All odd numbers between firstNum and secondNum : ";
for(int i=firstNum;i<=secondNum;i++){
if(i%2!=0){
cout<<i<<" ";
}
}
cout<<"\nThe sum of all even numbers between firstNum and secondNum : ";
int evensum = 0;
for(int i=firstNum;i<=secondNum;i++){
if(i%2==0){
evensum+=i;
}
}
cout<<evensum;
cout<<"\nNumbers and their sqaures between 1 and 10 : \n";
for(int i=1;i<=10;i++){
cout<<i<<" "<<i*i<<endl;
}
cout<<"Sum of the squares of the odd numbers between firstNum and secondNum : ";
int oddsum = 0;
for(int i=firstNum;i<=secondNum;i++){
if(i%2!=0){
oddsum += (i*i);
}
}
cout<<oddsum;
string str;
cout<<"\nEnter a word: ";
cin>>str;
cout<<"All uppercase letters in word are : ";
for(int i=0;str[i]!='\0';i++){
if(str[i]>=65 && str[i]<=90){
cout<<str[i]<<" ";
}
}
return 0;
}
Output :