1) Complete the code that can calculate total marks for 3 students
for(int stud=1;____(a)____;stud++)
{
cout<<"Enter Student name:";
____(b)____name;
for(int mark=1;mark<=3;____(c)____)
{
cout<<"Enter assigment marks:";
cin>>marks;
total=____(d)____+marks;
}
}
cout<<"Total marks are"<<____(e)____;
(a) stud<=3
(b) cin>>
(c) mark++
(d) total
(e) total
2) Based on the short code below :
Change for loop statement below to while loop
for(x=1;x<=5;x++)
cout<<x<<"\t"<<x*2<<"In";
x=1;
while(x<=5)
cout<<x<<"\t"<<x*2<<"In";
x++
3) Write the code using while loop to get the output as below !
20
15
10
5
x=20
while(x<=5)
cout<<" "<<x;
x--;
yg niy aq tataw betol ke tak . sbb nyeer mis blom check . so klu aq ade sala, tlg sesaper tego aq k ? tnx :))
3/26/2011
last exercise b4 our examination
1) Change the if...else statement below to switch...case statement
#include<iostream.h>
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:"<<value;
return 0;
}
#include<iostream.h>
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:"<<value;
}
return 0;
}
2) Change the for loop statement below to while loop
#include<iostream.h>
main()
{
int i,power2;
for(i=1;i<=9;i++)
{
power2=i*i;
cout<<" "<<i<<"power2"<<power2;
}
return 0;
}
#include<iostream.h>
main()
{
int i,power2;
//while
i=1;
while(i<=9)
{
power2=i*i;
cout<<" "<<i<<"power2"<<power2;
i++;
}
return 0;
}
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.
#include<iostream.h>
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;
}
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.
#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;
discount=total*0.10;
cout<<"discount"<<discount;
return 0;
}
5) What is the output for the following program segmment
i) int x=20;
for(int i=5;i<=x;i++)
{
cout<<i;
i=i+4;
}
5
10
15
20
ii) int mul=1;
do
{
mul=mul*2;
cout<<"The mul is:"<<mul;
mul++;
}
while(mul<10)
1
3
7
#include<iostream.h>
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:"<<value;
return 0;
}
#include<iostream.h>
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:"<<value;
}
return 0;
}
2) Change the for loop statement below to while loop
#include<iostream.h>
main()
{
int i,power2;
for(i=1;i<=9;i++)
{
power2=i*i;
cout<<" "<<i<<"power2"<<power2;
}
return 0;
}
#include<iostream.h>
main()
{
int i,power2;
//while
i=1;
while(i<=9)
{
power2=i*i;
cout<<" "<<i<<"power2"<<power2;
i++;
}
return 0;
}
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.
#include<iostream.h>
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;
}
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.
#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;
discount=total*0.10;
cout<<"discount"<<discount;
return 0;
}
5) What is the output for the following program segmment
i) int x=20;
for(int i=5;i<=x;i++)
{
cout<<i;
i=i+4;
}
5
10
15
20
ii) int mul=1;
do
{
mul=mul*2;
cout<<"The mul is:"<<mul;
mul++;
}
while(mul<10)
1
3
7
Exercise niy ktorang wt tyme mis wt extra class b4 exam . mis suh student wt kt white board . aq kne wt num 2 . pas uh aq sala sbb lupe na ltx semikolon . hmm cuai aq niy )= sume soalan niy mis da check n discuss nan ktorang . so jwpn dy insyallah betol (:
for loop & while loop pulaaak !
FOR LOOP
#include<iostream.h>
main()
{
//declare variable
int x,y;
cout<<"Enter an integer=";
cin>>x;
//loop for
for(y=1;y<x;y++)
{
cout<<" "<<x;
}
return 0;
}
WHILE LOOP
#include<iostream.h>
main()
{
//declare variable
int x,y;
cout<<"Enter an integer=";
cin>>x;
//while
y=x;
while(y<x)
{
cout<<" "<<x;
y++;
}
return 0;
}
#include<iostream.h>
main()
{
//declare variable
int x,y;
cout<<"Enter an integer=";
cin>>x;
//loop for
for(y=1;y<x;y++)
{
cout<<" "<<x;
}
return 0;
}
WHILE LOOP
#include<iostream.h>
main()
{
//declare variable
int x,y;
cout<<"Enter an integer=";
cin>>x;
//while
y=x;
while(y<x)
{
cout<<" "<<x;
y++;
}
return 0;
}
iklan jap ..
weyhhhh ! sedey gyleerr . aq tye kak syaza td, mcm ner xm . ok ke ko . then dy suh aq tgok fb kak lia . dy ckp mis ade komen kt wall kak lia about our xm tuhhh . aq check, aq bace dan aq kecewe )): hmm ciap sebut name aq lagi . adeeh miss kecewa mesty aq wt terok . hmm susa nye na score subject niy T T hmm lg satuu pasal ass yg mis bg uhhh . knp susa sgt haa ? mcm mne la aq na ciap kn . adeeh
homework !!
QUESTION 1
a) Initializing variable Pi with the value 3.14
const Pi=3.14;
b) Declare a variable named perimeter with double data type
double perimeter;
c) Give instruction that allowed user to input data
cout<<"Enter data=";
cin>>data;
d) Input number of computer using variable
cout<<"Enter number=";
cin>>nimber;
QUESTION 2
Change the if...else statement below to switch...case statement
#include<iostream.h>
main()
{
int selection,quantity;
float price;
cout<<"1.pen=rm0.50";
cout<<"2.pencil=rm0.30;
cout<<"3.ruler=rm0.20";
cout<<"4.Erasor=rm0.10";
cin>>selection;
cin>>quantity;
if(selection==1)
{price=quantity*0.50;}
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 selection";}
cout<<"the price is :"<<price;
return 0;
}
#include<iostream.h>
main()
{
int selection,quantity;
float price;
cout<<"1.pen=rm0.50";
cout<<"2.pencil=rm0.30;
cout<<"3.ruler=rm0.20";
cout<<"4.Erasor=rm0.10";
cin>>selection;
cin>>quantity;
switch(quantity,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;
}
a) Initializing variable Pi with the value 3.14
const Pi=3.14;
b) Declare a variable named perimeter with double data type
double perimeter;
c) Give instruction that allowed user to input data
cout<<"Enter data=";
cin>>data;
d) Input number of computer using variable
cout<<"Enter number=";
cin>>nimber;
QUESTION 2
Change the if...else statement below to switch...case statement
#include<iostream.h>
main()
{
int selection,quantity;
float price;
cout<<"1.pen=rm0.50";
cout<<"2.pencil=rm0.30;
cout<<"3.ruler=rm0.20";
cout<<"4.Erasor=rm0.10";
cin>>selection;
cin>>quantity;
if(selection==1)
{price=quantity*0.50;}
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 selection";}
cout<<"the price is :"<<price;
return 0;
}
#include<iostream.h>
main()
{
int selection,quantity;
float price;
cout<<"1.pen=rm0.50";
cout<<"2.pencil=rm0.30;
cout<<"3.ruler=rm0.20";
cout<<"4.Erasor=rm0.10";
cin>>selection;
cin>>quantity;
switch(quantity,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;
}
jom blaja pasal if...else statement & switch...case statement
ok bdasarkan table kt ats . kite bole wt program dlm 2 jenis bentuk . sama ade dlm bntuk if...else ataupon switch...case . ok now kite wt dlm bentuk if..else statement dlu k ..
IF...ELSE STATEMENT
#include<iostream.h>
main()
{
//declare variable
int age;
//input
cout<<"Enter your age=";
cin>>age;
//if...else statement
if(age>=1||age<=6)
{cout<<"you are kids !!";}
else if(age>=7||age<=12)
{cout<<"you are child !!";}
else if(age>=13||age<=21)
{cout<<"you are teenager !!";}
else if(age>=22||age<=30)
{cout<<"you are adult !!";}
else if(age>30)
{cout<<"you are old !!";}
else
{cout<<"Invalid selection";}
return 0;
}
SWITCH...CASE STATEMENT
#include<iostream.h>
main()
{
//declare variable
int age;
//input
cout<<"Enter your age=";
cin>>age;
//switch...case statement
switch(age)
{
case('age'>=1||'age'<=6):cout<<"you are kids !!";break;
case('age'>=7||'age'<=12):cout<<"you are kids !!";break;
case('age'>=13||'age'<=21):cout<<"you are kids !!";break;
case('age'>=22||'age'<=30):cout<<"you are kids !!";break;
case('age'>30):cout<<"you are kids !!";break;
default:cout<<"Invalid selection";
}
return 0;
}
hmm kadang soklan suh kite tuka if...else jd switch...case . kadang tuka switch...case g if...else . tp kadang soklan ta bg programming, tp kite kene develop sndry..
mid term da lepas .. alhamdulillah :D
hmm td tepat pkul 1p.m xm FOP . jantong aq mcm na gugo .tkot yg teramat sgt . sbb subject niy kan susa . tp alhamdulillah aq dpt jwb . cume yg variable name tuh aq btol btol lupe . terpakse tembak ;P hehe hm lw stakat na lulus bole la, tp lw na dpt highest mimpi jelaaa ): hmm btw, td mis ade tulis kt white board suh siap kn blog, sume ass n sume weekly reflection . tekejot aq bc . hmm due date ary niyy lak tuhh . adehh hope mis check blog aq nt tga tga mlm . lw skg, mmg ta lengkap ag la . jp g na wt k (:
Subscribe to:
Posts (Atom)