Write a program that inputs three integers from the
keyboard and prints the
• sum,
• average,
• product,
• smallest, and
• largest
of these numbers. The screen dia log should appear as
follows:
//Program for Summary Statistics
#include <iostream> //Load input-output library functions
using std::cout; //Using standard output functions of C++
using std::cin; //Using standard input functions of C++
using std::endl; //Using standard end line functions of C++
int main(){ //Begin the main function
int num1, num2, num3, sum;
//Declare integer numbers
cout << "Input three different integers: ";
//Print out the text
cin >> num1 >> num2 >> num3;
//Read numbers input from keyboard
sum = num1 + num2 + num3;
/*The be low block computes the smallest number from the
three input numbers*/
int smallest = num1;
if (num2 < smallest)
smallest = num2;
if (num3 < smallest)
smallest = num3;
/*The below block computes the largest number from the
three input numbers */
int largest = num1;
if (num2 > largest)
largest = num2;
if (num3 > largest)
largest = num3;
//The below lines print out the results onto the screen
cout << "Sum is " << sum << endl;
cout << "Average is " << sum/3 << endl;
cout << "Product is " << num1 * num2 * num3 << endl;
cout << "Smallest is " << smallest << endl;
cout << "Largest is " << largest << endl;
return 0;
} |
Program 2. The Geometry of Circles
Write a program that reads in the radius of a circle as an
integer and prints the
circle’s
• diameter,
• circumference, and
• area.
Use the constant 3.14159 for π. Do all calculations in
output statements.
[Note: We have discussed only integer constants and variables. We will discuss
floating-point numbers, i.e., values that can have decimal points later.]
//Program for The Geometry of Circles
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
main(){
#define pi 3.14159 //Define pi is 3.14159
int radius; // Variable for radius
cout << "Input the radius: "; //Print out the text line
cin >>radius; /*Read a number input from keyboard
for the radius variable*/
//Below lines print out the results
cout << "Diameter is: " << radius*2 << endl;
cout << "circumference is: " << 2*pi*radius << endl;
cout << "Area is " << pi*radius*radius <<endl;
return 0;
} |
C++ can re present uppercase letters, lowercase letters and
a considerable variety
of special symbols . C++ uses small integers internally to represent each
different
character. The set of characters a computer uses and the corresponding integer
representations for those characters are called that computer’s character set.
You
can print a character by enclosing that character in single quotes, as with
cout << ’A’; // print an uppercase A
You can print the integer equivalent of a character using static cast as
follows:
cout << static_case< int >( ’A’ ); // print ’A’ as an integer
This is called a cast ope ration (we formally introduce casts in Chapter 4). When
the preceding statement executes, it prints the value 65 (on systems that use
the
ASCII character set).
Write a program that prints the integer equivalent of a character typed at the
keyboard.
Store the input in a variable of type char. Test your program several times
using uppercase letters, lower case letters, digits , and special characters
( like $).
//Program for Cast Operators
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main(){
char ch; //Declare character variable ch
cout << "Input a character: ";
cin >> ch; //Read a character input into ch
cout << static_cast < int > (ch); /*Cast the input value
of ch to the corresponding ASCII number */
cout << endl; //Return to a new line
return 0;
} |