How to convert char to int number in C language ?
To Convert char(Character) Number to int (Integer) in C Language
Char - > Int
Introduction :
// Credit to logical.blogspot.com
// Originally created
The conversion can by done by simple programming in c language. It's an easy trick prepared by logical.blogspot.com . Before proceeding to conversion of char to int of character number. we must have knowledge about ASCII numbers , basic c language and c programming , functions , loops and data types as well as exponent of any numbers . Among them most essential thing is ASCII numbers.
If you have good knowledge about ASCII numbers , you will able to solve the conversion very quickly. ASCII numbers of 0 to 9 are : 48 to 57 respectively.
If you have good knowledge about the numbers you can solve easily , for example : 123 = 1 X (10)^2 + 2 X (10)^1 + 3 X (10) ^0
Basics in c language : array , data types, for and while loops , increments.
You need knowledge about stack. Push , pop , show operation.
C language code :
// Credit to logical.blogspot.com
// Originally created
#include <stdio.h>
int sq(int,int);
char stack[10];
int main() {
int i=0,x;
int CharToNum, AddNumNum=0 , exponent=-1;
int NumChar=0;
printf("Enter any char interger :\n");
gets(stack);
printf(" Your expression is : %s\n ",stack);
while(stack[i] != '\0')
{
if ( stack[i] >=48 && stack[i] <= 57)
exponent++;
i++;
}
i=0;
while(stack[i] != '\0')
{
if ( stack[i] >=48 && stack[i] <= 57)
{
CharToNum = stack[i] - '0';
NumChar = NumChar+(CharToNum*sq(10,exponent));
exponent--;
}
i++;
}
printf("Your integer : %d",NumChar);
}
int sq(int num , int power)
{
int i , sqn = 1;
if (power == 1)
return num;
for (i=0;i<power;i++)
sqn = sqn*num;
return sqn;
}
Output :
--------------------------------------------------------
Comments
Post a Comment
Give your feedback!
DO NOT SPAM !!
NO THIRD PARTY PROMOTIONAL LINK !!!! (else comment will be deleted.)