Question
Do it on C++ please
.Question 2: Take a user input integer, and print out the digit by digit. (can enter any positive number, e.g. 123, 34567,37.. (hint: first you need to find the digit of integer, use while loop to find the digit of integer, then look the exercise 6 of lecture 5) Example: . Enter an integer: 123456789 e first digit: 1 o second digit: 2 o ninth digit: 9 Do it on C++ please
0 0
Add a comment Improve this question Transcribed image text
Answer #1

code:

#include<iostream>

using namespace std;

int main()

{

int n;

cout<<"Enter an integer: ";

cin>>n;

int sum = 0;

while (n > 0)

{

sum = (sum * 10) + n % 10;

n = n / 10;

}

n = sum;

int i=1;

while (n>0) {

int digit = n % 10;

n = n / 10;

cout << "The digit no. "<<i++<< " is: " << digit << endl;

}

}

or //

#include<iostream>
using namespace std;

void digit(int n)
{
static int i=0;
if(n !=0)
{
digit(n/10);
cout<<"The digit no. "<< ++i <<" is: "<<n%10<<endl;
}

}

int main()
{
int num;
cout<<"Enter an integer: ";
cin>>num;
digit(num);
}

output:

Enter an integer: 123456789 The digit no. 1 is: 1 The digit no. 2 is: 2 The digit no. 3 is: 3 The digit no. 4 is: 4 The digit no. 5 is: 5 The digit no. 6 is: 6 The digit no. 7 is: 7 The digit no. 8 is: 8 The digit no. 9 is: 9 Process returned e (exe) execution time 2.983 s Press any key to continue.

Add a comment
Know the answer?
Add Answer to:
Do it on C++ please Do it on C++ please .Question 2: Take a user input...
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
  • USE of C++ please Write a program that achieves the following requirements: 1. A while/do-While loop...

    USE of C++ please Write a program that achieves the following requirements: 1. A while/do-While loop that will ask the user to input an integer on each iteration. The loop keeps a running total of the input and exits only when the total exceeds the first input times 10. 2. A for loop that does the exact same thing. The program should print out the *new* total each time the total is input.

  • In C:Write a program that can determine whether a user input is a binary number or...

    In C:Write a program that can determine whether a user input is a binary number or not. (1, 2, 3, 8, 13) Write a program that accomplishes all of the following: Greets the user and informs them that the program will determine whether an input from the user is a valid binary number or not (e.g., consisting of only 0’s and/or 1’s). Accept an integer input from the user. Assume that the user provides a valid numeric input that will...

  • Write a complete C++ program that do following. 1) Read a positive integer from the user...

    Write a complete C++ program that do following. 1) Read a positive integer from the user with proper prompt. Assume that this user is nice and he/she would not start the integer with digit 0. 2) Create a size 10 array as counters for digits 0 - 9. 3) Use a while loop for processing a) Single out the current last digit using modular 10 technique b) Increment the corresponding counter through the index derived in a). 4) At the...

  • C++ only Implement a program that prompts a user to enter a number N where N...

    C++ only Implement a program that prompts a user to enter a number N where N is a positive number. The program must validate a user input and ask a user to re-enter until an input is valid. Implement a function that pass N as an argument and return a result of calculates N! (N-factorial): The function must use loop for the implementation. Hint: Factorial: N! = N * (N-1) * (N-2) * …. * 1 ( This is only...

  • please solve in c++ MULT PLES number Writo a complete nat counts and prints the even...

    please solve in c++ MULT PLES number Writo a complete nat counts and prints the even pies of 5 as well as the number of multiples of 8 be.ween two integer values entered by the user and the total number. You must use a while or do ... while loop. If the user's first input is greater than or equal to the second one, your program should ask the user for another two inputs until the first one is less...

  • Using C# Create a “Main” method that will take user input one line at a time...

    Using C# Create a “Main” method that will take user input one line at a time until they enter the phrase “done”. Store each line entered into an ArrayList, so that one element of the ArrayList is one line of user input. You do NOT need to write your own ArrayList class, please use the standard library implementation of an ArrayList. After the user finishes typing in input, ask the user for a location and file name. Save the contents...

  • Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program...

    Lab 5-2 Nested Loops 2. Summation Of Numbers (using Nested While Loops) Part A: The program will calculate and display the total of all numbers up to a specific number (entered by the user). Only positive numbers are allowed. Part B: User-controlled loop Part A Input Validation loop Part A: Summation loop Examples: When 5 is entered the program will calculate the total as 1+2+...+5 and display 15. When 2 is enterered the program will calculate the total as 1+2...

  • C++ please Problem 3: Write a C++ program that prompts the user to enter an integer...

    C++ please Problem 3: Write a C++ program that prompts the user to enter an integer of any length (but not more than 8- digits), the program then extracts and prints each digit in its respective column. For example, in the integer 436, 4 should be printed in the 4th column, 3 in the 3rd and 6 in the 6th column. Hint: you can use setw to solve this problem. Sample Input/Output Enter an integer of any length. 9836472 ...Printing...

  • This program will take advantage of methods written in the Help.java class First, you will write...

    This program will take advantage of methods written in the Help.java class First, you will write a new method in Help.java. This method will be named: addArray) and will accept an array of integers as input. This method will calculate the sum of all values in the array and return the resulting sum. Remember that the array being passed to the method may be of ANY length. (Use the existing methods in the Help.java class to help you remember how...

  • Complete the Python program below that performs the following operations. First prompt the user to input...

    Complete the Python program below that performs the following operations. First prompt the user to input two integers, n and m. If n<m, then print the odd positive integers that are less than m (in order, on a single line, separated by spaces). If man, then print the even positive integers that are less than n (in order, on a single line, separated by spaces). If neem, then print nothing. For instance, if the user enters 5 followed by 10,...

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