assignment c++ [najmi].docx
DESCRIPTION
Jump hook punchTRANSCRIPT
7/17/2019 assignment C++ [Najmi].docx
http://slidepdf.com/reader/full/assignment-c-najmidocx 1/6
Assignment C++
Programminga) Go tob) Whilec) Break and Continued) While Truee) Do / Whilef) Jump / Loopg) If / Ele
a. /* C++ program to demonstrate the working of goto statement. */
/* This program calculates the average of numbers entered by user. */
/* f user enters negative number! it ignores that number and
calculates the average of number entered before it.*/
" include #iostream$
using namespace std%
int main&' (
)oat num! average! sum .%
7/17/2019 assignment C++ [Najmi].docx
http://slidepdf.com/reader/full/assignment-c-najmidocx 2/6
int i! n%
cout##,-aimum number of inputs ,%
cin$$n%
for&i0% i # n% ++i' (
cout##,1nter n,##i##, ,%
cin$$num%
if&num # .' (
goto 2ump% /* Control of the program moves to 2ump% */
3
sum + num%
3
2ump
averagesum/&i40'%
cout##,5nAverage ,##average%
return %
-aimum number of inputs 0
1nter n0 6.7
1nter n6 8.9
1nter n7 48.9
Average 7.:8
b. // while_statement.cpp
#include <string.h>#include <stdio.h>
7/17/2019 assignment C++ [Najmi].docx
http://slidepdf.com/reader/full/assignment-c-najmidocx 3/6
char *trim( char *szSource )
{
char *psz!S " $
// Set pointer to character be%ore terminating &'
psz!S " szSource strlen( szSource ) +$
// iterate bac,wards until non -_- is %ound
while( (psz!S >" szSource) (*psz!S "" -_-) )
*psz!S " --$
return szSource$
0
int main()
{
char szbu%12 " 3+4567_____3$
print%_s(3n8e%ore trim9 :s3; szbu%)$
print%_s(3n%ter trim9 :sn3; trim(szbu%))$
0
The test of expression takes place before each eecution of the loop% therefore!
a while loop eecutes ;ero or more times. expression must be of an integral type! a
pointer type! or a class type with an unambiguous conversion to an integral or pointer
type.
A while loop can also terminate when a break !goto or return within the statement
body is eecuted. <se continue to terminate the current iteration without eiting
the while loop.continue passes control to the net iteration of the while loop.
c. // C++ Program to demonstrate working of break statement
"include #iostream$using namespace std%int main&' ( )oat number! sum .%
while &true' ( // test epression is always true cout##,1nter a number ,% cin$$number%
if &number = .' ( sum + number% 3 else ( break% // terminating the loop if number e>uals to .
7/17/2019 assignment C++ [Najmi].docx
http://slidepdf.com/reader/full/assignment-c-najmidocx 4/6
3
3 cout##,?um ,##sum% return %3
1nter a number @
1nter a number 7.@
1nter a number 9.
1nter a number [email protected]
1nter a number
?um :.9
d. while&true'
( //Bo -y oop ?tuD 3
I think that this may be easier to read and is definitely the standard for use in C#:
e."include #iostream$
using namespace std%
int main&' ( int number! i 0! factorial 0% cout## ,1nter a positive integer ,% cin $$ number%
while & i # number' ( factorial * i% //factorial factorial * i% ++i% 3
cout##,Eactorial of ,##number##, ,##factorial% return %3
1nter a positive integer @Eactorial of @ 6@
In this program, user is asked to enter a positive integer which is stored in variable
number (supposed user entered 4). Here is the working of while loop in this program:
7/17/2019 assignment C++ [Najmi].docx
http://slidepdf.com/reader/full/assignment-c-najmidocx 5/6
0. nitially! i = 1! test epression i # number is true! factorial becomes 0.
6. Fariable i is updated to 6! test epression is true! factorial becomes 6.
7. Fariable i is updated to 7! test epression is true! factorial becomes 9.
@. Fariable i is updated to @! test epression is true! factorial becomes 6@.
8. Fariable i is updated to 8! test epression is false and while loop is
terminated.
f. The following eample shows how to write a simple for loop in C++/C. This
eample has eactly the same eDect as the while loop.
%or (int count " +$ count <" 7$ count)
{
=onsole99riteine(count * count)$
0
=onsole99riteine(3?he end3)$
The parentheses after the for keyword contain three epressions separated by
semicolons. The Grst epression performs loop initiali;ation! such as initiali;ing the loop
counter. This initiali;ation epression is eecuted once only! at the start of the loop.
0. Continue working with the pro2ect from the previous eercise.
6. -odify the code in the main function to use a for loop rather than a while loop! as
shown here
5. =onsole99riteine(3elcome to @our calendar assistant3)$
6.
7. %or (int count " +$ count <" 7$ count)
A. {
B. =onsole99rite(3nClease enter date 3)$
D. =onsole99riteine(count)$
E.
+. int @ear " FetGear()$
++. int month " FetHonth()$+4. int da@ " FetIa@(@ear; month)$
+5. Iispla@Iate(@ear; month; da@)$
0
Hotice that there is no count++ statement after displaying the date. This is because
the for statement takes care of incrementing the loop counter.
0@. Iuild and run the application. The application asks you to enter Gve dates! as
before.
7/17/2019 assignment C++ [Najmi].docx
http://slidepdf.com/reader/full/assignment-c-najmidocx 6/6
g. "include #iostream$
using namespace std%
int main&' ( int number% cout## ,1nter an integer ,% cin$$ number%
if & number $ ' ( cout ## ,Jou entered a positive integer ,##number##endl% 3
else ( cout##,Jou entered a negative integer ,##number##endl% 3
cout##,This statement is always eecuted because itKs outside if...elsestatement.,% return %
3
1nter an integer 4@
Jou entered a negative integer 4@ This statement is always eecuted because itKs outside if...else statement.
The end