Question

In: Computer Science

write these programs in c++ using getch() function Write a program that reads 20 integers from...

write these programs in c++ using getch() function

Write a program that reads 20 integers from a user using a while loop and determines and prints whether each of those integers is an even number.

Write a program that uses a while statement to read integers from a user and prints the sum, average, and largest of these numbers. The input should terminate if the user enters -1.

Solutions

Expert Solution

NOTE that getch() is no longer present in C++ compiler as part of conio.h. Hence, thee is no way we can use getch() to provide the code for these problems. Hence, I am using standard cin to get user input.

1)

#include <iostream>

using namespace std;

int main() {

int num;

cout << "Enter 20 integers: " << endl;

int i = 1;

while (i<=20) {

cout << "Enter " << i << " integer: ";

cin >> num;

if (num % 2 == 0) {

cout << num << " is even" << endl;

} else {

cout << num << " is odd" << endl;

}

i ++;

}

}

2)

#include <iostream>

#include <climits>

using namespace std;

int main() {

int num;

int largest = INT_MIN;

int sum = 0;

int count = 0;

while (true) {

cout << "Enter an integer: ";

cin >> num;

if (num == -1){

break;

}

count ++;

sum += num;

largest = (largest < num) ? num : largest;

}

cout << "Sum = " << sum << endl;

cout << "Average = " << (double)(sum) / (double)(count) << endl;

cout << "Largest number = " << largest;

}


Related Solutions

1) Using either emacs or vi write a C program that reads 100 integers from stdin...
1) Using either emacs or vi write a C program that reads 100 integers from stdin into a 2-dimensional 10x10 array. The program will read one additional integer which is used to multiply each element of the array. The final array should be printed to stdout as follows: Run 1: Enter 100 integers: 1 2 3 4 5 6 7 8 9 10 11 …. 100 Enter factor: 3 Result: 3 6 9 12 … 30 33 36 39 42...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
Code is in C Write a program that reads integers from stdin. Once it reaches the...
Code is in C Write a program that reads integers from stdin. Once it reaches the * end of the input, it prints the smallest absolute value among those * of the numbers it read. * * For example, if * 4, 6 -3, 3, -2, 13, -4 * are read from stdin, the program should print 2. * * If the end of file is reached before any integer is seen, the * number printed should be INT_MAX (defined...
C++ programing Write a main function that  reads a list of integers from a user, adds to...
C++ programing Write a main function that  reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation.
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
Write a main function that reads a list of integers from a user, adds to an...
Write a main function that reads a list of integers from a user, adds to an array using dynamic memory allocation, and then displays the array. The program also displays the the largest element in the integer array. Requirement: Using pointer notation. Please do this with C++
Write a C program that reads three integers and then prints them in the order read...
Write a C program that reads three integers and then prints them in the order read and reversed. Use four functions: main, one to read the data, one to print them in the order read, and one to print them reversed.
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. The whole program is under 40 lines of code. Just read from cin. Now, you need to detect non integers. In this case,...
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop. Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
Write a C++ program that reads integers from standard input until end of file. Print out...
Write a C++ program that reads integers from standard input until end of file. Print out the largest integer that you read in, on a line by itself. Any erroneous input (something that is not an integer) should be detected and ignored. In the case where no integers are provided at all, print NO INTEGERS and stop Remember, try not to do the entire job all at once! First try input of a single number and make sure it works....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT