Convert the following program to standard C <stdio.h> with scanf and printf and
ID: 3551959 • Letter: C
Question
Convert the following program to standard C <stdio.h> with scanf and printf and NOT cpp syntac!
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
char ch;
cout<<"Enter 'B' for Berlin 'S' foe Seoul 'T' for Tehran to be compared with Louisville: ";
cin>>str;
ch = getchar();
if(str[0]=='B' || str[0]=='b')
{
cout<<" To see Population comparison, enter P To see Area comparison , Enter A : ";
cin>>ch;
if(ch == 'P' || ch == 'p')
{
cout<<"Berlin: 3.502 Million ";
cout<<"Louisville: 253,128 ";
}
else
if(ch == 'A' || ch == 'a')
{
cout<<"Berlin: 891.85 KM sq ";
cout<<"Louisville: 1032 KM sq ";
}
else
{
cout<<"The character you entered was invalid ";
}
}
else
if(str[0]=='T' || str[0]=='t')
{
cout<<" To see Population comparison, enter P To see Area comparison , Enter A : ";
cin>>ch;
if(ch == 'P' || ch== 'p')
{
cout<<"Tehran: 8,244,535 (Urban) + 13,828,365(Metro) ";
cout<<"Louisville: 253,128 ";
}
else
if(ch == 'A' || ch== 'a')
{
cout<<"Tehran: 730 KM sq (Urban) + 1274 KM sq (Metro) ";
cout<<"Louisville: 1032 KM sq ";
}
else
{
cout<<"The character you entered was invalid ";
}
}
else
if(str[0]=='S' || str[0]=='s')
{
cout<<" To see Population comparison, enter P To see Area comparison , Enter A : ";
cin>>ch;
if(ch == 'P' || ch == 'p')
{
cout<<"Seoul: 10,442,426 ";
cout<<"Louisville: 253,128 ";
}
else
if(ch == 'A' || ch == 'a')
{
cout<<"Seoul: 605.21 KM sq ";
cout<<"Louisville: 1032 KM sq ";
}
else
{
cout<<"The character you entered was invalid ";
}
}
return 0;
}
Explanation / Answer
#include <stdio.h>
int main()
{
char str[2];
char ch[2];
printf("Enter 'B' for Berlin 'S' foe Seoul 'T' for Tehran to be compared with Louisville:");
scanf("%s",str);
if(str[0]=='B' || str[0]=='b')
{
printf(" To see Population comparison, enter P To see Area comparison , Enter A : ");
scanf("%s",ch);
if(ch[0] == 'P' || ch[0] == 'p')
{
printf("Berlin: 3.502 Million ");
printf("Louisville: 253,128 ");
}
else if(ch[0] == 'A' || ch[0] == 'a')
{
printf("Berlin: 891.85 KM sq ");
printf("Louisville: 1032 KM sq ");
}
else
{
printf("The character you entered was invalid ");
}
}
else if(str[0]=='T' || str[0]=='t')
{
printf(" To see Population comparison, enter P To see Area comparison , Enter A : ");
scanf("%s",ch);
if(ch[0] == 'P' || ch[0]== 'p')
{
printf("Tehran: 8,244,535 (Urban) + 13,828,365(Metro) ");
printf("Louisville: 253,128 ");
}
else if(ch[0] == 'A' || ch[0] == 'a')
{
printf("Tehran: 730 KM sq (Urban) + 1274 KM sq (Metro) ");
printf("Louisville: 1032 KM sq ");
}
else
{
printf("The character you entered was invalid ");
}
}
else if(str[0]=='S' || str[0]=='s')
{
printf(" To see Population comparison, enter P To see Area comparison , Enter A : ");
scanf("%s",ch);
if(ch[0] == 'P' || ch[0] == 'p')
{
printf("Seoul: 10,442,426 ");
printf("Louisville: 253,128 ");
}
else if(ch[0] == 'A' || ch[0] == 'a')
{
printf("Seoul: 605.21 KM sq ");
printf("Louisville: 1032 KM sq ");
}
else
{
printf("The character you entered was invalid ");
}
}
return 0;
}