Daisypath Anniversary tickers

Daisypath Anniversary tickers

Thursday, April 7, 2011

EXERCISE!!

Soklan 1)

Change the if...else statement below to switch...case statement

#include
main()
{
int nomA,nomB,selection,value;
cout<<"Enter 2 number:";
cin>>nomA>>nomB;
cout<<"1.Addition";
cout<<"2.Substraction";
cout<<"3.Multiply";
cout<<"4.Division";
cout<<"Enter selection=";
cin>>selection;
if(selection==1)
{value=nomA+nomB;}
else if(selection==2)
{value=nomA-nomB;}
else if(selection==3)
{value=nomA*nomB;}
else if(selection==4)
{value=nomA/nomB;}
else
{cout<<"Invalid selection";}
cout<<"The value is:"<return 0;
}

Jawapan 1)

#include
main()
{
int nomA,nomB,selection,value;
cout<<"Enter 2 number:";
cin>>nomA>>nomB;
cout<<"1.Addition";
cout<<"2.Substraction";
cout<<"3.Multiply";
cout<<"4.Division";
cout<<"Enter selection=";
cin>>selection;
switch(selection)
{
case1:"value=nomA+nomB";break;
case2:"value=nomA-nomB";break;
case3:"value=nomA*nomB";break;
case4:"value=nomA/nomB";break;
default:cout<<"Invalid selection";
cout<<"The value is:"<}
return 0;
}

Soklan 2)

Change the for loop statement below to while loop

#include
main()
{
int i,power2;
for(i=1;i<=9;i++)
{
power2=i*i;
cout<<" "<}
return 0;
}

Jawapan 2)

#include
main()
{
int i,power2;
//while
i=1;
while(i<=9)
{
power2=i*i;
cout<<" "<i++;
}
return 0;
}

Soklan 3)

Write a program that input 2 numbers. Compare the numbers & display only the biggest number. If the numbers are same, display the numbers are same.

Jawapan 3)

#include
main()
{
int nomA,nomB;
cout<<"Enter 2 numbers=";
cin>>nomA>>nomB;
if(nomA>nomB)
{cout<<"nom A is bigger than nom B";}
else if(nomA==nomB)
{cout<<"nom A is same as nom B";}
else
{cout<<"Invalid";}
return 0;
}

Soklan 4)

Write a program using for loop that receive prices of 3 products. Calculate the price that must be paid after 10% discount is given to every customer.

Jawapan 4)

#include
main()
{
int x,price,total,discount;
for(x=1;x<4;x++)
{
cout<<"price["<cin>>price;
total=total+price;
}
cout<<"total="<discount=total*0.10;
cout<<"discount"<return 0;
}

Soklan 5)

What is the output for the following program segment

5.i) int x=20;
for(int i=5;i<=x;i++)
{
cout< i=i+4;
}

Jawapan 5.i)

5
10
15
20

5.ii) int mul=1;
do
{
mul=mul*2;
cout<<"The mul is:"< mul++;
}
while(mul<10)

Jawapan 5.ii)

1
3
7

No comments:

Post a Comment