1) Change the if...else to swicth case ... statement
#include<iostream.h>
main( )
{
int nomA,nomB,selesction,value;
cout<<"Enter 2 numbers :";
cin>>nomA>>nomB;
cout<<"1.addition";
cout<<"2.substraction";
cout<<"3.multiply";
cout<<"4.divison";
cout<<"enter selection";
cin>>selection;
{
if(seletion==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: " <<value
}
return0;
}
ANSWER:
#include<iostream.h>
main( )
{
int nomA,nomB,selesction,value;
cout<<"Enter 2 numbers :";
cin>>nomA>>nomB;
cout<<"1.addition";
cout<<"2.substraction";
cout<<"3.multiply";
cout<<"4.divison";
cout<<"enter selection";
cin>>selection;
{
//swicth ...case statement
(swictth selection)
{case1:nomA+nomB;break;
case2:nomA-nomB;break;
case3:nomA*nomB;break;
case4:nomA/nomB;break;
default:cout<<"invalid selection";
cout<<"The value is: " <<value;
}
return0;
}
2)Change for loop below to while loop
#include<iostream.h>
main( )
{
int i,power2;
for(i:1;i<9;i++)
{
power2=i*i;
cout<<" " <<i<<"power2"<<power2;
}
}
ANSWER:
#include<iostream.h>
main( )
{
int i,power2;
//While
i=1;
while(i<=9)
{
power2=i*i;
cout<<" "<<i<<"power2"<<power2;
i++;
}
}
3)Write a program that input 2 numbers . compare the numbers and display only the biggest number . If the number are same display "the number are same"
#include<iostream.h>
{
main()
int num1,num2
cout<<"enter an integer";
cin>>num;
//if (num2>num1)
{cout<<"num2 is bigger than num1;}
ele if (num2==num1)
cout<<"num2 is same as num 1";
else
{cout<<"invalid ";}
return0;
}
4.write a program using for loop hat receive prices of 3 products, calculate the price that must be paid after 16% discount is given to every customer
#include<iostream.h>
main( )
{
int x , price , total , discount;
for (x=1,x<4,x++)
{
cout <<"price ["<<x<<"]";
cin >>price;
total= total+price
}cout<<total=<<total;
dicount=total *0.01;
cout<<"discount"
return0;
}
5. what is the output for following program
a) intx=20
for (int i=5,i<=x,i++)
{
cout<< i;
}
answer: i =5,10,15,20
b) int mul=1
do
{
mul=mul*2;
cout<<"the mul is :" <<mul;
mul++;
while (mul10)
answer : mul = 1 ,3, 7
Sunday, March 27, 2011
EXERCISE BEFORE MIDTERM
Posted by Nor Kolifatun Kariadi at 1:16 PM
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment