Write a function to read a sequence of character digits and



Write a function to read a sequence of character digits and convert it to decimal (i.e; integer) and return the value to main program and print it! For example, look at the printf statement below. We will assume that the input will be a sequence of character digits such as \'1\', followed by \'4\' and so on, possibly preceded by white space(n) so conversion process will stop when a white space is encountered. int main printf(\"The value was: %d\ \", getnumO) exit(EXIT_SUCCESS) The function, getnum(), takes no arguments and returns an integer value.

Solution

#include <stdio.h>
int getnum()
{
   int ans=0;
   char c;
   printf(\"Enter a number(a space to end):\");
   //scanf(\"%c\",&c);
   int count=0;
   while(1)
   {
       scanf(\"%c\",&c);
       //printf(\"%c\ \",c);
       if(count==0 && c==\'\ \')
       {
           continue;
       }
       if(c==\' \')
       {
           break;
       }
       ans = ans*10+(c-\'0\');
       count++;
   }
   return ans;
}
int main()
{
   printf(\"The value was: %d\ \",getnum());
   return 0;
}

 Write a function to read a sequence of character digits and convert it to decimal (i.e; integer) and return the value to main program and print it! For example

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site