• Register

We can perform different conversions between different variables like we can convert float variable to integer variable. And these conversions are really helpful in numerous ways. We can easily convert int to char but there is something to notice, take a look.

 

Approach

The approach for the conversion of int to char is to use casting and by casting I mean implicit casting.

 

Program

#include<iostream>
using namespace std;
int main()
{
   int n;
   cout << "enter int variable :";
   cin >> n;
   char a;
   a = static_cast<char>(n);
   cout << "\n your num is converted to char and here it is " << a;
   return 0;
}

OUTPUT

enter int variable :65

 your num is converted to char and here it is A


enter int variable :56

 your num is converted to char and here it is 8

Note: when we give input as integer then the program will convert the int to char but it will not return output as expected. It will provide output according to the ASCII table and according to its ASCII code. You can take a look at table by going to this site http://www.asciitable.com/

 

Overview

Ask users to enter input.

Take input.

Casting.

Return output.

 

Hope this might help you.

posted Jul 5 in c++ 100 points