Showing posts with label ASCII. Show all posts
Showing posts with label ASCII. Show all posts

C Program to Print ASCII equivalent of a character until terminated by key is ‘x’

How to write a C Program to Print ASCII equivalent of a character until terminated by key is ‘x’ in C Programming Language ?

Solution:

/* C Program to Print ASCII equivalent of a character until terminated by key is ‘x’*/

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Now Enter the Data. To Stop Press 'x'.\n\n");
while((ch = getch()) != 'x')
printf("The ASCII Value of %c is %d\n", ch, ch);
}

You may also learn these C Program/Code :

C Program To Swap Two Numbers Without Using Third Variable

Printing ASCII Table with Numbers and corresponding characters

How to write a C Program to Printing ASCII Table with Numbers and corresponding characters in C Programming Language ?

The following program prints the ASCII Table with the numbers from 0 to 255 and their corresponding equivalent characters in ASCII.

Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.

  1. #include<stdio.h>
  2. main()
  3. {
  4. int num;
  5.  
  6. printf("Printing ASCII values Table...\n\n");
  7.  
  8. num = 1;
  9.  
  10. while(num<=255)
  11.  
  12. {
  13.  
  14. printf("\nValue:%d = ASCII Character:%c", num, num); /*This change has been made as per the comment. Thank you anonymous Blog Viewer ... */
  15.  
  16. num++;
  17. }
  18.  
  19. printf("\n\nEND\n");
  20.  
  21. }

the program should have a char input declaration .. ie.. 
int num;
char c;
.
.
.
then, after the while loop 
printf("\n Value:%d= ASCII character:%c",num,c);
.. because the character variable in the above printf statement cannnot be "num" as it has been declared as the integer variable at the start of the program.

It says that there's not need for the char variable. printf("%c", int_variable)..will always produce an output which is the ASCII equivalent of the then value of that int_variable. 

So, introducing a new char variable would be absolutely unnecessary.

Always keep in mind..the programs that we make always have to be compact. The most compact and the most efficient program bags the prize

You could simplify and use a for loop instead.

  1. #include
  2.  
  3. int main(void)
  4. {
  5.  
  6. printf("Printing ASCII values Table...\n\n");
  7.  
  8. for (int i = 1; i <= 255; ++i)
  9. {
  10. printf("\nValue:%d = ASCII Character:%c", i, i); /*This change has been made as per the comment. Thank you anonymous Blog Viewer ... */
  11. }
  12. printf("\n\nEND\n");
  13.  
  14. return 0;
  15. }

We should define (char)c=num(int); such as in this code.


  1. #include
  2. #include
  3. main()
  4. {
  5. int num;
  6. char c;
  7.  
  8. printf("Printing ASCII values Table...\n\n");
  9.  
  10. num = 1;
  11.  
  12. while(num<=255)
  13.  
  14. {
  15. c=num;
  16. printf("\nValue:%d = ASCII Character:%c", num, c);
  17. num++;
  18. }
  19.  
  20. printf("\n\nEND\n");
  21. getche();
  22.  
  23. }


C Program to Printing ASCII Table with Numbers and corresponding characters



  1. #include
  2. void main()
  3. {
  4. int i;
  5. char j;
  6. for (i=0,j=0;i<=255;j++,i++)
  7. {
  8. printf("character %c ,Value %d\n", j,i);
  9. }
  10. }

C Program to Fill the screen with a smiling face

How to Write a C Program to fill the entire screen with a smiling face in C Programming Language ?

The smiling face has an ASCII value 1. 

An infinite loop like the following can also do the job.
for (i=1;i<=1000;) 

  1. #include<stdio.h>
  2. main()
  3. {
  4. int i, j;
  5.  
  6. for (i=1, j=1; j<=5000; j++)
  7.  
  8. {
  9. printf("%c", i);
  10. }
  11. }

This following C Program also displays smiling face very clearly try it :

  1. #include
  2. main()
  3. {
  4. int i;
  5. for(i=0;i<=2559;i++)
  6. {
  7. printf("%c",2);
  8. }
  9. }

You can use any int instead of 2 thats just for what you want to print on screen. n char is not used because you have only integers have this smiling faces ASCII char has 66 77 like ASCII value.