BLOGGER TEMPLATES AND TWITTER BACKGROUNDS »

Sunday, March 27, 2011

EXECISE 2 MAR 2011

1) a - initializing variable pie with the value 3.14
    answer : float pie = 3.14
    b- declare a variable and perimeter with double data type
    answer : Double perimeter ;
    c- give instruction that allowed user to input data
    answer : cout << " enter your data " ;
                   cin >> data ;
    d- input number of computer using variable
    answer : cout << " enter computer number " ;
    cin >> number ;


2) change else statement to swicth..case

#include <iostream.h>
main ( )
{
int selection , quantity ;
float price ;
cout << "1.pen = rm 0.50" ;
cout<< "2.pencil = rm 0.30";
cout<< "3.ruler = rm0.20";
cout<< "4.eraser=rm0.10';
cin>>selection;
cin>>quantity;
if (selection==1)
{price = quantity*0.20}
else if (selection ==2)
{price = quantity*0.30}
else if (selection ==3)
{price = quantity*0.20}
else if (selection ==4)
{price = quantity*0.10}
else
{cout << "invalid seletion" ;}
cout<< "the price is :      " << price ;
return 0;
}

answer :

#include <iostream.h>
main ( )
{
int selection , quantity ;
float price ;
cout << "1.pen = rm 0.50" ;
cout<< "2.pencil = rm 0.30";
cout<< "3.ruler = rm0.20";
cout<< "4.eraser=rm0.10';
cin>>selection;
cin>>quantity;
swicth (selection)
{
case 1 : price=quantity*0.50;break;
case 2 : price=quantity*0.30;break;
case 3 : price=quantity*0.20;break;
case 4 : price=quantity*0.10;break;
default : cout << "invalid selection";
}
cout << the price is :      " << price;
return 0;
}

0 comments: