Practical 3 (The For...Loop or do....while Loop )
Write a program that uses while loops to perform the following steps:
a. Prompt the user to input two integers: firstNum and secondNum
(firstNum must be less than secondNum).
b. Output all odd numbers between firstNum and secondNum.
c. Output the sum of all even numbers between firstNum and secondNum.
d. Output the numbers and their squares between 1 and 10.
e. Output the sum of the square of the odd numbers between firstNum and secondNum.
f. Output all uppercase letters.
#include <iostream>
using namespace std;
int main(){
int firstNum, secondNum, number, evensum = 0, even2sum = 0;
cout << "First number: ";
cin >> firstNum;
cout << "Second number (must be more than first number): ";
cin >> secondNum;
// check: firstNum must be less than secondNum)
while (firstNum > secondNum) {
if (firstNum > secondNum)
cout << "\nFirst number must be less than second number. Try again."<<endl;
cout << "First number: ";
cin >> firstNum;
cout << "Second number: ";
cin >> secondNum;
}
number = firstNum;
cout<<"\nAll even numbers between "<<firstNum<<" and "<<secondNum<<endl;
while (number <= secondNum){
if(number % 2 != 0){
cout << number << " ";
even2sum += number * number;
evensum += number ;
}
number++;
}
cout<<"\nSum of all even numbers between "<<firstNum<<" and "<<secondNum<<endl;
cout<<evensum<<endl;
cout<<"\nNumbers and their squares between 1 and 10."<<endl;
number = 1;
while (number <= 10) {
cout << number << "^2" << " = " << number * number << endl;
number++;
}
cout<<"\nSum of squares of even numbers between "<<firstNum<<" and "<<secondNum<<endl;
cout<<even2sum<<endl;
// uppercase letters.
cout << "\n All uppercase letters."<<endl;
char ch = 'A';
while (ch <= 'Z'){
cout << ch << ", ";
ch++;
}
cout << endl;
return 0;
}
Comments
Leave a comment