آرایه ها در c++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

آرایه ها در c++

چطور در c++ با استفاده از آرایه ها و متغیر ها یک از کاربر یک عدد ۱۶ رقمی بگیریم( int a[16] ) و تک تک رقم های آن را جداگانه چاپ کنیم ؟ How to get a 16-digit number (int a[16]) from the user using arrays and variables in C++ and print each digit separately?

22nd May 2024, 3:08 PM
Black Knight
Black Knight - avatar
2 Answers
+ 4
Black Knight 1. Read the console input as 16 individual characters. 2. Convert each character digit to its equivalent int value by subtracting char constant '0' (ASCII int value 48) from the input character. Store in the a[16] array. 3. Loop through the a[16] array and print each digit. It is not necessary to store the digits in an array. You could do the same thing minimally with only a single char variable in one loop that performs the input, conversion, and output.
22nd May 2024, 4:43 PM
Brian
Brian - avatar
+ 2
string text; //read user input as string cin >> text; //optional: check for possible errors if (text.size() != 16 || text.find_first_not_of("0123456789") != string::npos) return "error"; //convert every chacter to integer and print it for (size_t x = 0; x != 16; x++) { cout << (int)(text[x] - '0'); }
23rd May 2024, 12:24 AM
john ds
john ds - avatar