How do i take input of very large numbers in c/c++. Say the number of digits in the number can be anywhere between 1<=n<=10000.
Prefer taking input as a string in such cases.
i already tried that. i did the following,
char num[1000];
scanf("[^\n]*c",num);
and the printf("%s",num); perfectly prints the number too as a string.
However now when i try to access the individual keys, i end up printing the all the 1000 keys resulting in lot of junk values.
Any idea how do i find the number of digits my number is having so that i can loop to access individual keys and fulfill my requirements.
The number of digits in your number is just the length of your string. Use strlen(str) for finding the length of the string. Doing any operation on any particular digit or accessing them will require its conversion from char to int. This is because each individual digit is stored as a char.
An example, something like this:
cin>>str;
int no_of_digits= strlen(str);
cout<<"The 5th digit of the number is "<<(str[4]-‘0’);
i think you should used to long key word before your data type…
eg:
long int a=10000;
The range of a is not from 1 to 1000, the input number has “1000 digits”…
It is much more convenient to do the conversion once during input itself rather than doing it every time you want to work with the number.