Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a function to read a sequence of character digits and convert it to decima

ID: 3929636 • Letter: W

Question



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.

Explanation / Answer

#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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote