fork download
  1. #include <iostream>
  2. #include <iomanip> // For formatting output
  3.  
  4. using namespace std;
  5.  
  6. /*
  7.  * Program Description:
  8.  * This program displays the characters corresponding to ASCII codes 0 through 127.
  9.  * The output is formatted such that 16 characters are displayed on each line.
  10.  * It handles the boundary conditions and uses proper error checking.
  11.  * The ASCII codes range from 0 to 127 and are printed using a loop.
  12.  */
  13.  
  14. int main() {
  15. // Loop through ASCII codes from 0 to 127
  16. int count = 0;
  17.  
  18. cout << "Displaying characters for ASCII codes 0 through 127:\n";
  19.  
  20. for (int i = 0; i <= 127; i++) {
  21. // Check if the ASCII code is printable. Codes below 32 are control characters.
  22. if (i < 32) {
  23. cout << " "; // Print a space for non-printable control characters
  24. } else {
  25. cout << char(i); // Print the corresponding character for the ASCII code
  26. }
  27.  
  28. // After printing 16 characters, go to the next line
  29. count++;
  30. if (count == 16) {
  31. cout << endl; // Move to the next line after 16 characters
  32. count = 0; // Reset count for the next row
  33. } else {
  34. cout << " "; // Add space between characters for better visibility
  35. }
  36. }
  37.  
  38. cout << "\nEnd of program." << endl;
  39. return 0;
  40. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Displaying characters for ASCII codes 0 through 127:
                               
                               
  ! " # $ % & ' ( ) * + , - . /
0 1 2 3 4 5 6 7 8 9 : ; < = > ?
@ A B C D E F G H I J K L M N O
P Q R S T U V W X Y Z [ \ ] ^ _
` a b c d e f g h i j k l m n o
p q r s t u v w x y z { | } ~ 

End of program.