Write a program to convert string to integer without library function.

#include<stdio.h>

char input[100];

int Convert() {
    int sum = 0, i, p = 1, digit;
    for(i = strlen(input) - 1; i>=0; i--) {
        digit = input[i] - '0';
        sum += digit*p;
        p *= 10;
    }
    return sum;
}
void main() {
    int n;
    clrscr();
    printf("\n Enter Number (Input as String) : - ");
    scanf("%s",input);
    n = Convert();
    printf("\n Integer Number ( Not String ): - ");
    printf("%d\n",n);
    getch();
}

0 comments:

Post a Comment