LOOPS
Write a program that uses while loops to perform the following steps:
(firstNum must be less than secondNum).
secondNum.
and secondNum.
#include <iostream>
using namespace std;
int main()
{
double firstNum = 2;
double secondNum = 1;
while (firstNum > secondNum)
{
cout << "Enter first number: ";
cin >> firstNum;
cout << "Enter second number (must be more than first number): ";
cin >> secondNum;
if (firstNum > secondNum){
cout << "First number must be less than second number. Try again. \n";
}
}
cout << "\n";
//b. Output all odd numbers between firstNum and secondNum.
cout << "All odd numbers between first number and second number:\n";
int number = firstNum;
int sum = 0;
while (number <= secondNum)
{
if (number % 2!=0){
cout << number<< "\n";
}else{
sum+=number;
}
number++;
}
//c. Output the sum of all even numbers between firstNum and secondNum.
cout << "\nThe sum of all even numbers between first number and second number: "<<sum<<"\n";
//d. Output the numbers and their squares between 1 and 10.
cout << "The numbers and their squares between 1 and 10:\n";
number = 1;
while (number <= 10)
{
cout << number << " -> " << number << "^2" << " = " << number * number << '\n';
number++;
}
cout << "\n";
//e. Output the sum of the square of the odd numbers between firstNum and secondNum.
number = firstNum;
sum = 0;
while (number <= secondNum)
{
if (number % 2!=0){
sum += (number * number);
}
number++;
}
cout << "The sum of the square of the odd numbers between first number and second number: "<<sum<<"\n";
//f. Output all uppercase letters.
cout << "\n\nAll uppercase letters.\n";
int c = 65;
while (c < 91){
cout << char(c++) << ", ";
}
cout << "\n";
cin>>sum;
return 0;
}
Comments
Leave a comment