Question

Programming language is C++.

9. Write a program that reads digits and composes

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

#include<iostream>
#include<string>
#include<vector>
#include<sstream>
using namespace std;

int main()
{
   string digitsasSTR;
   cout<<"\nEnter digits (upto 4 digits): ";
   cin>>digitsasSTR;
   vector<int> numbers;
   int RequiredNumber;

   stringstream ss(digitsasSTR);

   int i;

   while (ss >> i)
{
numbers.push_back(i);

       //if not numbers remove it
if (ss.peek()-'0'<0 || ss.peek()-'0'>9)
ss.ignore();
}

   if(numbers.size()==1)
   {
       //compose the number
       RequiredNumber=numbers[0];
       cout<<"\n"<<RequiredNumber<<" is "<<numbers[0]<<" ones."<<"\n";
   }
   if(numbers.size()==2)
   {
       //compose the number
       RequiredNumber=(numbers[0]*10)+(numbers[1]*1);
       cout<<"\n"<<RequiredNumber<<" is "<<numbers[0]<<" tens and "<<numbers[1]<<" ones."<<"\n";
   }
   if(numbers.size()==3)
   {
       //compose the number
       RequiredNumber=(numbers[0]*100)+(numbers[1]*10)+(numbers[2]*1);
       cout<<"\n"<<RequiredNumber<<" is "<<numbers[0]<<" hundred and "<<numbers[1]<<" tens and "<<numbers[2]<<" ones."<<"\n";
   }
   if(numbers.size()==4)
   {
       //compose the number
       RequiredNumber=(numbers[0]*1000)+(numbers[1]*100)+(numbers[2]*10)+numbers[3];
       cout<<"\n"<<RequiredNumber<<" is "<<numbers[0]<<" thousands and "<<numbers[1]<<" hundreds and "<<numbers[2]<<" tens and "<<numbers[3]<<" ones." <<"\n";
   }
   cout<<"\n";
   system("pause");
   return 0;
}

Output:

H:Madhuri Cprograms\Debug Cprograms.exe Enter digits 〈upto 4 digits); 4.3.2.1 4321 is 4 thousands and 3 hundreds and 2 tens a

Add a comment
Know the answer?
Add Answer to:
Programming language is C++. 9. Write a program that reads digits and composes them into integers....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a program that reads a list of integers ending with a negative, and that then...

    Write a program that reads a list of integers ending with a negative, and that then outputs that list in reverse. After the negative that ends the list, a character indicates what character should separate the output integers. If the input is: 3 5 2 - 1 * the output is: 2*5*3 LAB ACTIVITY 7.16.1: LAB: Reverse a list, with separating characters 2 / 10 main.cpp Load default template... 6 7 8 int numbers [100],i=0, num, size; char character; cin>>num;...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • Write a program to subtract large unsigned integers. Your program should prompt and read in two...

    Write a program to subtract large unsigned integers. Your program should prompt and read in two large unsigned integers. The two large integers should be stored arrays, one array for each integer. The integers should be stored with one digit per location in the two arrays. The first integer should be no smaller than the second. Your program should subtract the second integer from the first. The result should be stored in an array, again one digit per location in...

  • C++ Programming question Problem: 5. Write a program that reads in a list of integers into...

    C++ Programming question Problem: 5. Write a program that reads in a list of integers into an array with base type int. Provide the facility to either read this array from the keyboard or from a file, at the user's option. If the user chooses file input, the program should request a file name. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is to be...

  • Instructions In c++ write a program that reads in a string of zeros and ones without...

    Instructions In c++ write a program that reads in a string of zeros and ones without spaces and for each 8 digits (one byte) outputs the corresponding value in decimal format. The output numbers are separated by spaces. Stop when a character different than 0 or 1 is encountered. The final output may be generated from less than 8 digits but no output should be generated for the final character. Should work for example: input: 00000011000000010010X output: 3 1 2

  • In C++: Write a program that reads a list of integers, and outputs the two smallest...

    In C++: Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The list is preceded by an integer indicating how many numbers are in the list. If the input is: 5 10 5 3 21 2, the output is: 2 3 To achieve the above, first read the integers into a vector.

  • Assignment #9 will be the construction of a program that reads in a sequence of integers...

    Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers. Then compute the minimum number, compute the largest number among the numbers that are divisible by 2, count even numbers, and compute the...

  • In C programming please (b) You will write a program that will do the following: prompt...

    In C programming please (b) You will write a program that will do the following: prompt the user enter characters from the keyboard, you will read the characters until reading the letter 'O' You will compule statistics concerning the type of characters entered. In this lab we will use a while loop. We will read characters from stdin until we read the character 'Q' Example input mJ0*5/1+x1@3qcxQ The 'Q' should be included when computing the statistics properties of the input....

  • Consider the following program that reads a number of nonnegative integers into an array and prints...

    Consider the following program that reads a number of nonnegative integers into an array and prints the contents of the array.   Complete the missing parts, add new function prototypes and function definitions, and test the program several times. Add the following to the program: Write a void function that prints the list of nonnegative integers in reverse. Write a void function that prints all the numbers stored in the list that are greater than 10. It will also print the...

  • C programming! Write a program that reads integers until 0 and prints the sum of values...

    C programming! Write a program that reads integers until 0 and prints the sum of values on odd positions minus the sum of values on even positions. Let ?1, ?2, … , ??, 0 be the input sequence. Then the program prints the value of ?1 − ?2 + ?3 − ?4 + ⋯ ??. The input is a sequence of integers that always contains at least 0 and the output will be a single number. For example, for input...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT