Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
6Activity
0 of .
Results for:
No results containing your search query
P. 1
c++

c++

Ratings: (0)|Views: 136 |Likes:
Published by jennyrosesison

More info:

Published by: jennyrosesison on May 19, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

02/01/2013

pdf

text

original

C++ sample problem (conditional and loop structure
combine)
\u2022

Write a c++ program that prompts the user to
enter an integer between 5 to 10. The program
will then output the number type in by the user
except that the user is limited to three trials only,
if the user exceeds the limit, the program
terminates.

#include <iostream.h>
#include <conio.h>
#include <process.h>
main()
{

int num, try=1;
clrscr();
cout<<"\n Enter number from 5-10: ";
cin>>num;
while(num <5 || num>10 )

{++try;
if(try >3)
{cout<<\u201cYou Exceed the number of trials\u201d;
cout<<"\n Program Exiting";
getch();
exit(0);

}
clrscr();
cout<<"\n\nINvalid number!!ty again\n\n ";
cout<<"Enter an integer between 5 and 10 :";
cin>>num;
}

cout<<"The number you entered is: "<<num;
getch();
return 0;

}Nested Loop
\u2022
When there\u2019s a need to repeat a loop several
times, we simply put the loop inside another loop.
\u2022

Nested loop can be a bit confusing but if you
know the basic concepts well it shouldn\u2019t be a
problem.

\u2022

As an analogy, we can think of a nested loop like
the hands of a clock. Where the hands represent
each loop, the hour hand is the outer loop,
followed by the minute hand, then the second
hand.

Sample:
\u2022

A c++ program that prints a multiplication table.
#include <iostream.h>
#include <conio.h>
main()
{int x,y;

clrscr();
for(x=1;x<=5;++x)
{

for(y=1;y<5;++y)
{cout<<" "<<x*y;
}
cout<<"\n";

}
getch();
return 0;
}

\u2022

The inner loop is executed many times compared with the outer loop. However, it is the outer loop that dictates whether the inner loop will execute or not.

#include <iostream.h>
#include <conio.h>
main()
{int i,j;

clrscr();
for(i=1;i<5;++i)
{cout<<"\n"<<i;
for(j=i+1;j<5;++j)
cout<<\u201c "<<j;

}
getch();
return 0;
}Using a break statement with Nested loops

#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
main()
{int i,j,m;

clrscr();
for(i=1;i<6;++i)
{for(j=1;j<6;++j)

if(j>i)
break;

else
cout<<setw(4)<<i*j;
cout<<"\n";

}
getch();
return 0;
}The switch statement

\ue000The switch statement can be used instead of the
else-if construct to implement a sequence of
parallel alternatives.
\ue000Its syntax is:
switch(expression)
{
case constant1: statement1;break;
case constant2: statement2;break;
case constant3: statement3;break;
::
case constantN: statementN;break;
default: statement 0;
}
\ue000This evaluates the expression and then looks for
its value among the case constants. If the value is

Activity (6)

You've already reviewed this. Edit your review.
1 hundred reads
sangita phunde liked this
ppkumar2009 liked this
Ajeya Shetti liked this
yuvraj liked this
srmanisekhar liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->