Lab 3 Solution

advertisement
Lab3
Question. 1
Write a program that takes two values from the user and then swaps them
before printing the values
The output should be like
Enter the first number: 2
Enter the second number: 3
You entered the numbers as 2
and 3
After swapping, the values of
the two numbers are 3 and 2
#include <conio.h>
#include <iostream>
using namespace std;
// This program takes two values from the user and then swaps them
// before printing the values. The user will be prompted to enter
// both numbers.
// Place your name here
int main()
{
float firstNumber;
float secondNumber;
float temp1, temp2;
// Prompt user to enter the first number.
cout << "Enter the first number" << endl;
cout << "Then hit enter" << endl;
cin >> firstNumber;
// Prompt user to enter the second number.
cout << "Enter the second number" << endl;
cout << "Then hit enter" << endl;
cin >> secondNumber;
// Echo print the input.
cout << endl << "You input the numbers as " << firstNumber
<< " and " << secondNumber << endl;
// Now we will swap the values.
temp1 = firstNumber;
temp2 = secondNumber;
firstNumber = temp2;
secondNumber = temp1;
// Output the values.
cout << "After swapping, the values of the two numbers are "
<< firstNumber << " and " << secondNumber << endl;
getch();
return 0;
}
Question. 2
Write a program to print the following:
*
**
***
****
*****
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
cout << "*\n**\n***\n****\n*****\n";
getch();
return 0;
}
Question. 3
Write a program that reads in two integers and determines and prints if the first is a
multiple of the second. (Hint: Use the modulus operator.)
The output should be like
Enter two integers:
42
4 is a multiple of 2
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
if ( num1 % num2 == 0 )
cout << num1 << " is a multiple of " << num2 << endl;
else
cout << num1 << " is not a multiple of " << num2 << endl;
getch();
return 0;
}
Question. 4
Write a program that prints the integer equivalents of some uppercase letters,
lowercase letters, digits and special symbols. At a minimum, determine the integer
equivalents of the following: A B C a b c 0 1 2 $ * + / and the blank character.
Hint ( use static_cast )
The output should be like
Enter a character: A
A 's integer equivalent is 65
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
char symbol;
cout << "Enter a character: ";
cin >> symbol;
cout << symbol << "'s integer equivalent is "
<< static_cast< int > ( symbol ) << endl;
getch();
return 0;
}
Question. 5
Write a function that returns the smallest of three double-precision, floating-point
numbers.
The output should be like
Enter three numbers: 2 3 4
The smallest value is 2
#include <iostream>
#include <conio.h>
using namespace std;
//Function Call by Value
double smallest3( double, double, double );
int main()
{
double x, y, z;
cout << "Enter three numbers: ";
cin >> x >> y >> z;
cout << "The smallest value is " << smallest3( x, y, z ) << endl;
getch();
return 0;
}
double smallest3( double smallest, double b, double c )
{
if ( b < smallest && c > smallest )
return b;
else if ( c < smallest )
return c;
else
return smallest;
}
Download