Write a C++ program that accepts a number followed by a space and then the lette
ID: 3883263 • Letter: W
Question
Write a C++ program that accepts a number followed by a space and then the letter. If the letter following the number is an "i" then the program is to treat the number entered as a dimension in inches and convert the number to an equivalent length in centimeters and display a suitable message indicating the conversion. If the letter following the number is a "c", then the program is to treat the number entered as a dimension in centimeters and convert the number to the equivalent length in inches, and display a suitable message indicating the conversion. The if, else if statement must be used.
Explanation / Answer
#include<iostream>
#include<string.h>
using namespace std;
int main(){
double n;
char str[100];
double val=0;
cout<<"ENter a number and letter followed by space :";
cin>>n;
cin>>str;
if(strcmp(str,"i") == 0){
val = n*2.54;
cout<<"Conversion from inches to centimeters "<<val<<endl;
}
else if(strcmp(str,"c")==0){
val = n/2.54;
cout<<"Conversion from centimeters to inches "<<val<<endl;
}else{
cout<<"Invalid conversion "<<endl;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.