Here's the question,
Use while or do-while loop (you will ask user for the value till which he wants to print the series).
- To display the numbers 9, 98, 997, …
So, for this program, I made the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
using namespace std;
int main()
{
long int a, b = 1, c = 10;
cout << "Enter number until when you want the series: ";
cin >> a;
do
{
if (a == 0)
break;
cout << c - b << ", ";
c = c * 10;
b++;
} while (b <= a);
cout << endl;
system("pause");
}
|
However, I am facing one problem in it.. Program runs fine, but only if user entered value is 9 or less than 9.. For example,
On entering 9 as input, it gives
9, 98, 997, 9996, 99995, 999994, 9999993, 99999992, 999999991,
However, if I enter any number bigger than 9, it starts showing abnormal outputs.. Like, ?
On entering 10 as input, it gives
9, 98, 997, 9996, 99995, 999994, 9999993, 99999992, 999999991, 1410065398,
Or on entering 15, it gives,
9, 98, 997, 9996, 99995, 999994, 9999993, 99999992, 999999991, 1410065398, 1215752181, -727379980, 1316134899, 276447218, -1530494991,
Can anyone explain this abnormal behavior of the program and what should I do to fix this up? Thank you!