Home > Computers & Tech > Detail
search_icon
Question

using the scanf function, write a program that does the following: reads two characters one after the other, one float and one whole number from standard input. all the input is in one line, separated by spaces. outputs the input read, in separate lines.

Answer

input read: c1 c2 3.14 10

  • What is the format specifier of scanf for reading characters? A:%c
  • What is the format specifier of scanf for reading float? A:%f
  • What is the format specifier of scanf for reading whole number? A:%d
  • What is the solution approach? A:Initialize 2 character variables, 1 float variable and 1 whole number variable. Use the scanf function to read the input from standard input in the mentioned format, store it in respective variables, and output each of the variables in separate lines using printf function.
  • What are the steps followed in the solution approach? A:1. Declare 2 character variables, char1 and char2. 2. Declare 1 float variable, float1. 3. Declare 1 whole number variable, wholeNum. 4. Use scanf function to read the input using the format string "%c %c %f %d", and store it in respective variables. For reading characters, use %c twice. 5. Use printf function to output each variable in separate lines.