English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

.if a five digit no. is input through the keyboard .write a program in c/c++ to print a new no. by adding one to each of its digit.e.g. 12391 then output must be 23402.

2006-07-10 02:20:27 · 3 answers · asked by yaameen786 1 in Computers & Internet Programming & Design

3 answers

You MIGHT think the answer is as simple as adding 11111 to the number, but that would be incorrect, since 12391 + 11111 = 23502 (not 23402)...

Thus, we need to account for the "9" which (according to the specification) must yield a "0" result and not a "10" (i.e. "carry")...

So...
the code fragment for this would look something like this...

int i, digits[5], input=12391;
/* Extract the digits and add 1 to each & get single digit answer*/

for (i=4; i>=0; i--) {
digits[i] = (input%10 + 1) %10;
input/=10;
}

/* print the digits in order */
for (i=0; i<5; i++)
putchar (digits[i] + '0');

}

2006-07-10 02:45:05 · answer #1 · answered by N2FC 6 · 0 0

Since there are only 10 digits, easiest way is to create an array of that maps number 0 to 9, to its new value, then iterate through input and get the output from the defined mapping array.

2006-07-10 03:03:28 · answer #2 · answered by Yangyanzhe 2 · 0 0

contain #contain #contain #contain utilising namespace std; important () { char n[one hundred]; int temp; cout<<"please enter a kind"<>n; int sum=0; for (int i=0;i

2016-12-08 17:51:37 · answer #3 · answered by mudsir 3 · 0 0

fedest.com, questions and answers