Problem 2: Pin Number A pin number is a four-digit number whose digits sum is a
ID: 3850537 • Letter: P
Question
Problem 2: Pin Number
A pin number is a four-digit number whose digits sum is a multiple of 10. A one check digit is usually added to the right of a three-digit number to form the four-digit pin number. For example, for the three digits number 572 we add the digit 6 to the right to form the pin number 5726 whose digits sum 5+7+2+6 is 20 which is a multiple of 10.
Write a program that prompts the user to enter a three digits number then form and print the four-digit pin number.
Note:- (Using C++)
Explanation / Answer
#include <iostream>
using namespace std;
int main() {
int OriPin,NewPin,num,sum=0;
cout<<" Please enter 3 digit pin number : ";
cin>>OriPin;
num = OriPin;
while (num != 0)
{
sum = sum + num % 10;
num = num / 10;
}
if (sum%10 == 0)
{
//If Original pin is divisible by 0
NewPin = OriPin*10;
}
else
{
//First multiply original number by 10 and then add (10-(sum of orginal number digits %10))
NewPin = OriPin*10 +(10-(sum%10));
}
cout << " Entered pin is "<< OriPin << " New Pin is " << NewPin;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.