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.
#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;
}
Comments
Leave a comment