Showing posts with label oop1ch3e. Show all posts
Showing posts with label oop1ch3e. Show all posts

Friday, January 6, 2017

Create the equivalent of a four-function calculator. The program should ask the user to enter a number, an operator, and another number. (Use floating point.) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result. When it finishes the calculation, the program should ask whether the user wants to do another calculation. The response can be ‘y’ or ‘n’. Some sample interaction with the program might look like this: Enter first number, operator, second number: 10 / 3 Answer = 3.333333 Do another (y/n)? y Enter first number, operator, second number: 12 + 100 Answer = 112 Do another (y/n)? n

Leave a Comment
Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-3 C++ Loops and Decisions  -- Questions+Exercises

Question:

Create the equivalent of a four-function calculator. The program should ask the user to enter a number, an operator, and another number. (Use floating point.) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result. When it finishes the calculation, the program should ask whether the user wants to do another calculation. The response can be ‘y’ or ‘n’. Some sample interaction with the program might look like this:

Enter first number, operator, second number: 10 / 3
Answer = 3.333333
Do another (y/n)? y

Enter first number, operator, second number: 12 + 100
Answer = 112
Do another (y/n)? n

Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include <iostream>
using namespace std;
int main()
{
       double n1, n2, ans;
       char oper, ch;
       do {
              cout << "\nEnter first number, operator, second number : ";
              cin >> n1 >> oper >> n2;
              switch (oper)
              {
              case '+':  ans = n1 + n2;  break;
              case '-':  ans = n1 - n2;  break;
              case '*':  ans = n1 * n2;  break;
              case '/':  ans = n1 / n2;  break;
              default:   ans = 0;
              }
              cout << "Answer = " << ans;
              cout << "\nDo another(Enter ‘y’ or ‘n’) ? ";
              cin >> ch;
       } while (ch != 'n');
       return 0;

}

Output:
Create the equivalent of a four-function calculator. The program should ask the user to enter a number, an operator, and another number. (Use floating point.) It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Use a switch statement to select the operation. Finally, display the result. When it finishes the calculation, the program should ask whether the user wants to do another calculation. The response can be ‘y’ or ‘n’. Some sample interaction with the program might look like this:  Enter first number, operator, second number: 10 / 3 Answer = 3.333333 Do another (y/n)? y  Enter first number, operator, second number: 12 + 100 Answer = 112 Do another (y/n)? n



Related Articles:



Read More

Operators such as >>, which read input from the keyboard, must be able to convert a series of digits into a number. Write a program that does the same thing. It should allow the user to type up to six digits, and then display the resulting number as a type long integer. The digits should be read individually, as characters, using getche(). Constructing the number involves multiplying the existing value by 10 and then adding the new digit. (Hint: Subtract 48 or ‘0’ to go from ASCII to a numerical digit.) Here’s some sample interaction: Enter a number: 123456 Number is: 123456

Leave a Comment
Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-3 C++ Loops and Decisions  -- Questions+Exercises

Question:

Operators such as >>, which read input from the keyboard, must be able to convert a series of digits into a number. Write a program that does the same thing. It should allow the user to type up to six digits, and then display the resulting number as a type long integer. The digits should be read individually, as characters, using getche(). Constructing the number involves multiplying the existing value by 10 and then adding the new digit. (Hint: Subtract 48 or ‘0’ to go from ASCII to a numerical digit.) Here’s some sample interaction:

Enter a number: 123456
Number is: 123456

Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
// makes a number out of digits
#include <iostream>
using namespace std;
#include <conio.h>                   //for getche()
int main()
{
       char ch;
       unsigned long total = 0;          //this holds the number
       cout << "\nEnter a number : ";
       while ((ch = _getche()) != '\r')    //quit on Enter
              total = total * 10 + ch - '0';     //add digit to total*10
       cout << "\nNumber is : " << total << endl;
       return 0;

}

Output:
Operators such as >>, which read input from the keyboard, must be able to convert a series of digits into a number. Write a program that does the same thing. It should allow the user to type up to six digits, and then display the resulting number as a type long integer. The digits should be read individually, as characters, using getche(). Constructing the number involves multiplying the existing value by 10 and then adding the new digit. (Hint: Subtract 48 or ‘0’ to go from ASCII to a numerical digit.) Here’s some sample interaction:  Enter a number: 123456 Number is: 123456



Related Articles:



Read More

Assume that you want to generate a table of multiples of any given number. Write a pro- gram that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines. Interaction with the program should look like this (only the first three lines are shown): Enter a number: 7 7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 126 133 140 147 154 161 168 175 182 189 196 203 210

Leave a Comment
Object-Oriented Programming in C++ Fourth Edition By Robert Lafore Chapter-3 C++ Loops and Decisions  -- Questions+Exercises

Question:

Assume that you want to generate a table of multiples of any given number. Write a pro- gram that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines. Interaction with the program should look like this (only the first three lines are shown):

Enter a number: 7
7   14   21   28   35   42   49   56   63   70
77   84   91   98  105  112  119  126  133  140
147  154  161  168  175  182  189  196  203  210

Explanation:
Below mention code is compiled in Visual Studio 2015 and Code Blocks 13.12,output snap is attached.. If any problem you feel and you want some explanation feel free to contact us.

Code:

/**************************************************|
/*************C++ Programs And Projects************|
***************************************************/
#include <iostream>
#include <iomanip>                     //for setw()
using namespace std;
int main()
{
       unsigned long n;                    //number
       cout << "\nEnter a number : ";
       cin >> n;                           //get number
       for (int j = 1; j <= 200; j++)           //loop from 1 to 200
       {
              cout << setw(5) << j*n << "  ";  //print multiple of n
              if (j % 10 == 0)                  //every 10 numbers,
                     cout << endl;                 //start new line
       }
       return 0;

 }

Output:
Assume that you want to generate a table of multiples of any given number. Write a pro- gram that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines. Interaction with the program should look like this (only the first three lines are shown):  Enter a number: 7 7   14   21   28   35   42   49   56   63   70 77   84   91   98  105  112  119  126  133  140 147  154  161  168  175  182  189  196  203  210



Related Articles:



Read More