Question

In: Computer Science

C++. Write a program that uses for loops to perform the following steps: a. Prompt the...

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).

Solutions

Expert Solution

#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 :


Related Solutions

Write a program with class name ForLoops that uses for loops to perform the following steps:1....
Write a program with class name ForLoops that uses for loops to perform the following steps:1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smallerthan secondNum).2. Output all the even numbers between firstNum and secondNum inclusive.3. Output the sum of all the even numbers between firstNum and secondNum inclusive.4. Output all the odd numbers between firstNum and secondNum inclusive.5. Output the sum of all the odd numbers between firstNum and secondNum inclusive. Java programming
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
IN C++ Write a program that uses nested loops to collect data and calculate the average...
IN C++ Write a program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the ­­program should display the...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall...
Write a C++ program that uses nested loops to collect data and calculate the average rainfall over a period of years. The program should first ask the user for the number of years. The outer loop will iterate once for each year. The inner loop will iterate 12 times, once for each month. Each iteration of the inner loop will ask the user for the inches of rainfall for that month. After all iterations, the program should display the number...
Write a C program with the following prompt *do not use gotostatement* "Write an interactive...
Write a C program with the following prompt *do not use goto statement* "Write an interactive program that implements a simple calculator. The program should allow the user to choose a binary arithmetic operation and enter two terms to which to apply the operation. The program should then compute the result and display it to the user. Your calculator will have two modes: double-precision mode and integer mode. Double-precision mode will do calculations and output using variables of type double....
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
Write a program in java which is in main and uses no classes, methods, loops or...
Write a program in java which is in main and uses no classes, methods, loops or if statements Ask the user to enter their first name Ask the user to enter their last name Print their initials followed by periods (ie. Clark Kent = C. K.) Print the number of letters in their last name
In C program, Use "do...while" and "for" loops to write a program that finds all prime...
In C program, Use "do...while" and "for" loops to write a program that finds all prime numbers less than a specified value.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT