Friday, 27 October 2017

User defined atoi()

Userdefined atoi( ) function explained with example:

Program:Sum of Digits of a Number 

#include<stdio.h>
int main(int argc,char *argv[])
{
       int num,sum=0,rem;
      if(argc!=2)
         exit(1);
      for(i=0;argv[1][i]!='\0';i++)// This is your own atoi function
      {
          num=num*10+(argv[1][i]-'0');
       }
printf("The entered Number is %d",num);
// This will display your entered number.

while(num!=0)// Code for determing sum of digits of number
{
    rem=num%10;
    num=num/10;
    sum=sum+rem;
}
printf("Sum of digits of your entered number is %d",sum);
return 0;
}



No comments:

Post a Comment

Featured Post

TCS Latest Test Patern ( For On-Campus Drives )

Tata Consultancy Services is software services consulting company whose  headquarter is situated in Mumbai, India. It is the Asia’s larges...