Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Sunday, 5 June 2016

PROBLEM 32:

Write a C program to convert numeric string to an int of same value?
For Example, if you read a string “1524” then an integer variable in your program should be

assigned 1524 as an integer.

CODE:

#include<stdio.h>
main()
{
    // Variable declaration//
    char a[10];
    int i,sum=0;
    /*Input*/
    printf("Enter any integer:");
    gets(a);
    /* actual Working */
    for(i=0;a[i]!='\0';i++)   //this loop for cheak array index is less than 48 and greater than 57
    {
        if(a[i]<48 || a[i]>57)
        {
            printf("string cannot convert to integer\n");
            break;   //if condition is true so this loop will be break
        }
        else //if condition is not true
        {
            sum=sum*10+a[i]-48;  //here sum multiply by 10 and than array index subtaract 48 and add sum
        }

    }
 if(a[i]>57)  //if a[i] greater tha 57 so its wrong
        {
            printf("Please enter integer value");

        }
    else
    {
    printf("string succesuuly convert to integer:%d",sum);
    }
return 0;

}

Program Output:










For more problems click here

No comments:

Post a Comment