Question

WRITE A PROGRAM IN C THAT DOES THE FOLLOWING Read a floating point number without using...

WRITE A PROGRAM IN C THAT DOES THE FOLLOWING

Read a floating point number without using scanf or conversion functions from the C library. You would need to write a utility function that processes console input using only getc and/or getchar and flow of control logic to process character-based input from the console to process the input.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Points to consider:

  1. First, we need to take the integer input for the number of characters in the floating-point number.
  2. Then we need to take input for that many numbers of characters.
  3. Then we need to convert the characters into the floating-point number.

Code

#include<stdio.h>
#include<string.h>
int main()
{
   printf("Enter the number of characters in the floating point number: ");
   float result= 0.0f;
   int dotpos = 0;
   int len;
   int n;
   int dot_start = 0;
   int after_dot = 0;
   scanf("%d", &len);
   getchar();
   for (n = 0; n < len; n++) {
       char c = (char)getchar();
       if(dot_start == 1){
           after_dot += 1;
       }
       if (c == '.') {
           dotpos = len - n - 1;
           dot_start = 1;
       }
       else {
           result = result * 10 + (c-'0');
       }
      
         
   }
   result /= power(10,dotpos);
   printf("Converted to Floating Point number: %.*f", after_dot, result);

}

int power(int n,int m)
{
   if(m==1)
       return n;
   else
       return n*(power(n,m-1));
}

Output Snippet

That was a nice question to answer
Friend, If you have any doubts in understanding do let me know in the comment section. I will be happy to help you further.
Please like it if you think effort deserves like.
Thanks

Add a comment
Know the answer?
Add Answer to:
WRITE A PROGRAM IN C THAT DOES THE FOLLOWING Read a floating point number without using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands...

    (50 pts) Write a program that asks user an arithmetic operator('+','−','∗' or '/') and two operands and perform the corresponding calculation on the operands. Specific requirements: (1) Your main function accepts inputs and display calculation result. (2) Write four functions to perform the four calculations. Call these functions in your main function. (3) Repeatedly asks for inputs, calculate and display result until user input nonnumeric value for operands. Hint: you can put getchar() right after scanf() to get rid of...

  • Write a C++ program using user defined functions that will read in the lengths of two...

    Write a C++ program using user defined functions that will read in the lengths of two side of a right triangle and then calculate the length of the hypotenuse. The program should do this twice. The program should the following user defined functions: Function readA will read in a value for side A from the user and return it to main. (it will not receive any input data from main) Function readB will read in a value for side B...

  • USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number s...

    USING C# 1. Write a program that takes outputs a string, an integer and a floating-point number separated by commas. Sample output: Bob Marley, 20, 5.2 2. 2. Write a program that asks the user for a string of letters of any size (no spaces), and finally a special character (values 33 to 47 in the Ascii table, or ‘!’ to ‘/’). Generate a random number of any size, integer or floating point, and combine those three pieces of information...

  • WRITE A PROGRAM IN C THAT DOES THE FOLLOWING: The numbers are real numbers, read in...

    WRITE A PROGRAM IN C THAT DOES THE FOLLOWING: The numbers are real numbers, read in as double precision floating-point numbers, one number perline, possibly with some white space before or after. Hint: use scanf. There are guaranteed to be no more than one million (that’s 10^6) numbers in the input, and no fewer than two legitimate numbers. Any number that is equal to zero should be ignored (discarded, not stored in the array or counted in any way).

  • c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers...

    c++ no pointers just follow instructions Write a program (array.cpp) that read positive floating point numbers into an array of capacity 100. The program will then ask for one of three letters(A, M or S). if the user inputs something other than one of these letters, then the program should ask for that input until an A, M, or S is entered. A do-while loop should be used for this. If the user inputs an A, then the program should...

  • Write a program in C++ that will read a sentence from the keyboard, terminated with a...

    Write a program in C++ that will read a sentence from the keyboard, terminated with a period '.' and write the sentence to an output file with all white spaces replaced by the symbol '*'. You may use the “isspace()” function of the “cctype” library to carry out the determination of whether the character read is 'white space'. Show any #include files you may need. Make sure to handle conditions where the file may be missing so your program does...

  • Please help me write down a program to check email address valid or not. Tip:parse the...

    Please help me write down a program to check email address valid or not. Tip:parse the string and count the special characters like '@', '.' .and at least one character. Suggested libc functions: printf() 1.Have to be by the argc/argv of main function 2.Connot use scanf/getchar and other user input functions 3.Please explain the details and steps for me cause I'm a beginner hhhhhh Thank you soooo much for helping me!

  • In this program, you will be using C++ programming constructs, such as overloaded functions. Write a...

    In this program, you will be using C++ programming constructs, such as overloaded functions. Write a program that requests 3 integers from the user and displays the largest one entered. Your program will then request 3 characters from the user and display the largest character entered. If the user enters a non-alphabetic character, your program will display an error message. You must fill in the body of main() to prompt the user for input, and call the overloaded function showBiggest()...

  • Write a C++ Program to do following: 1. Read every character from file char.txt using "get"...

    Write a C++ Program to do following: 1. Read every character from file char.txt using "get" and write it to a file char.bak using "put" 2. Read every second character from file char.txt using "get" and write it to a file charalternate.txt using "put" and "ignore" 3. Take input from user for offset and number of characters. For example if user enters 5 for offset and 10 for number of characters, then your program should copy 10 bytes from file...

  • A. File I/O using C library functions File I/O in C is achieved using a file...

    A. File I/O using C library functions File I/O in C is achieved using a file pointer to access or modify files. Processing files in C is a four-step process: o Declare a file pointer. o Open the desired file using the pointer. o Read from or write to the file and finally, o Close the file. FILE is a structure defined in <stdio.h>. Files can be opened using the fopen() function. This function takes two arguments, the filename and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT