Answer to Question #251354 in C++ for mHm

Question #251354

Write a program that reads a 4 digit (or fewer) positive integer and determines

how many digits in the integer are equal to the last character of your Roll number,

and prints the result on the screen. If a user enters a negative integer or an

integer greater than 4 digits, print a message on the screen: “Invalid integer!

Execute program again and enter a valid integer.” Also, write the pseudocode for

this program.


1
Expert's answer
2021-10-15T01:28:06-0400


#include <iostream>
using namespace std;
 
int count_digit(int num)
{
    int cnt = 0;
    while (num != 0){
        num = num / 10;
        cnt++;
    }
    return cnt;
}
 
int main()
{
    int num;
    while(true){
        cout<<"\nEnter a 4 digit (or fewer) positive integer: ";
        cin>>num;
        int num_digits=count_digit(num);
        if(num_digits<=4 && num>0){
            cout<<num;
            break;
        }
        else{
            cout<<"\nInvalid integer!";
        }
    }
    
    cout << "\nNumber of digits : " << count_digit(num);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS