Write a program that uses while loops to perform the following steps:
#include <iostream>
using namespace std;
int main()
{
int firstNum = 0;
int secondNum = 0;
cout << "Enter first number: ";
cin >> firstNum;
cout << "Enter second number: ";
cin >> secondNum;
if (firstNum >= secondNum) {
cout << "First number must be less then second number" << endl;
return 0;
}
cout << "Odd numbers: ";
int i = firstNum;
while (i <= secondNum) {
if (i % 2 == 1)
cout << i << " ";
i++;
}
cout << endl << "Even numbers: ";
i = firstNum;
while (i <= secondNum) {
if (i % 2 == 0)
cout << i << " ";
i++;
}
cout << endl << "Numbers and squares: ";
i = 1;
while (i <= 10) {
cout << i << ":" << i * i << " ";
i++;
}
int sum = 0;
i = firstNum;
while (i <= secondNum) {
if (i % 2 == 1)
sum += i * i;
i++;
}
cout << endl << "Sum of squares: " << sum;
cout << endl << "Uppercase letters: ";
char c = 'A';
while (c <= 'Z') {
cout << c << " ";
c++;
}
cout << endl;
return 0;
}
Comments
Leave a comment