PLAY WITH CODING

Home Basic Program If & Else Loops Conversion Pattern logo

Program Based on Loops

Program to print all letters with their ASCII value

Input - Output

Input
ASCII value of each character are :

Output
w = 199
e = 101
l = 108
c = 99
o = 111
m = 109
e = 101

Algorithm

1. Enter a string

2. for(int i=0; i<strlen(str); i++) {

    printf(" %c = %d\n",str[i],str[i]); }

CODE


/* Print all letters with their ASCII value */

#include<stdio.h>
#include<string.h>
int main()
{
      char str[] = {"welcome"};
      printf(" ASCII value of each character are : \n");
      for(int i=0;i<strlen(str);i++)
       printf(" %c = %d\n",str[i],str[i]);
    return 0;
}


/* Print all letters with their ASCII value */

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
      char str[] = {"welcome"};
      cout<<" ASCII value of each character are : "<<endl;
      for(int i=0;i<strlen(str);i++)
       cout<<" "<<str[i]<<" = "<<int(str[i])<<endl;
    return 0;
}

OutPut

ASCII value of each character are :

w = 199
e = 101
l = 108
c = 99
o = 111
m = 109
e = 101