Write a program that prompts the user to enter a floating point number. Then ask
ID: 3695807 • Letter: W
Question
Write a program that prompts the user to enter a floating point number. Then ask them if they want to print the value using 1 digit of precision or 4. Add some error checking that alerts the user if they enter an invalid choice (not 1 or 4).
Example #1 Please enter a floating point number: 23.789456
Do you want to print with 1 or 4 digits of precision (Enter 1 or 4): 4 23.7890
Example #2 Please enter a floating point number: 23.789456
Do you want to print with 1 or 4 digits of precision (Enter 1 or 4): 1 23.7
Example #3 Please enter a floating point number: 23.789456
Do you want to print with 1 or 4 digits of precision (Enter 1 or 4): 3 That is not a valid choice!
Explanation / Answer
this is a simple program which will take input as a floating value and print it with your choice of precisions
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<conio.h>
void main()
{
float number=0;
int choice=0;
clrscr();
printf(" Hello sir pls enter a floating point number");
scanf("%f",&number);
printf(" Your entered number is %f",number);
printf(" do you want to print the value using 1 digit of precision or 4 digit of precision..(enter 1 or 4)");
scanf("%d",&choice);
printf(" %d",choice);
if(choice==1)
printf(" your %d digit precision number is %10.1f",choice,number);
else if(choice==4)
printf(" your %d digit precision number is %10.4f",choice,number);
else
printf(" this is not a valid choice.. pls enter 1 or 4 only ");
getch();
}
this code gets executed on turbo c++ compiler with perfect results.
thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.