3. Program that should allow the user to type up to six digits, and then display the resulting number as a type long integer.

Chapter 3: 

Loops and Decisions

Programming Exercise

Problem # 3:

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

Solution:

#include <iostream>
using namespace std;
#include <conio.h>
int main()
{
    char a,b,c,d,e,f;
    int num;
  
    cout<<"Enter a number: ";
    a=getche();
    b=getche();
    c=getche();
    d=getche();
    e=getche();
    f=getche();
  
    num=(int)a-48;
    num=num*10+((int)b-48);
    num=num*10+((int)c-48);
    num=num*10+((int)d-48);
    num=num*10+((int)e-48);
    num=num*10+((int)f-48);
  
    cout<<endl<<"Number is: "<<num;
  
    return 0;
}

Let me know in the comment section if you have any question.

Similar Posts:
  1. Program that allows the user to enter the number and then generates the table, formatting it into 10 columns and 20 lines. 
  2. Temperature-conversion program that gives the user the option of converting Fahrenheit to Celsius or Celsius to Fahrenheit. Then carry out the conversion. 
  3. Program that should allow the user to type up to six digits, and then display the resulting number as a type long integer. 
  4. Program should ask the user to enter a number, an operator, and another number. It should then carry out the specified arithmetical operation: adding, subtracting, multiplying, or dividing the two numbers. Finally, display the result. 
  5. Use for loops to construct a program that displays a pyramid of Xs on the screen.
  6. Modify the FACTOR program in this chapter so that it repeatedly asks for a number and calculates its factorial, until the user enters 0, at which point it terminates.
  7. Program that calculates how much money you’ll end up with if you invest an amount of money at a fixed interest rate, compounded yearly. Have the user furnish the initial amount, the number of years, and the yearly interest rate in percent.
  8. Program that repeatedly asks the user to enter two money amounts expressed in old-style British currency: pounds, shillings, and pence. The program should then add the two amounts and display the answer, again in pounds, shillings, and pence. Use a do loop that asks the user whether the program should be terminated.
  9. Program where you tell the final amount and it figures out how many years it will take, at a fixed rate of interest compounded yearly, to reach this amount.
  10. Create a three-function calculator for old-style English currency. The calculator should allow the user to add or subtract two money amounts, or to multiply a money amount by a floating-point number.
  11. Create a four-function calculator for fractions. The user should type the first fraction, an operator, and a second fraction. The program should then display the result and ask whether the user wants to continue.

Comments