// -- C ++ shortcut tutorial -- Chapter 4 -- program control statement (Part 2)
// -- Chapter 4 -- program control statement
// -- 11/9/2005 wed.
// -- Computer lab
// -- Liwei
// -- Program #14 do -- while usage
# Include <iostream>
Using namespace STD;
Int main ()
{
Int num;
Do {
Cout <"enter a number (100 to stop ):";
Cin> num;
} While (num! = 100 );
// Getchar ();
Return 0;
}
// -- Program #15 magic Digital Program Improvement
# Include <iostream>
# Include <cstdlib>
Using namespace STD;
Int main ()
{
Int magic;
Int guess;
Magic = rand ();
Cout <"magic is:" <magic <Endl;
Do {
Cout <"Enter your guess :";
Cin> guess;
If (guess = magic)
{
Cout <"** right **" <Endl;
Cout <magic <"is the magic number." <Endl;
}
Else
{
Cout <"... Sorry, you are wrong." <Endl;
If (guess> magic)
Cout <"Your guess is too high." <Endl;
Else
Cout <"Your guess is too low." <Endl;
}
} While (guess! = Magic );
// Getchar ();
Return 0;
}
// -- Program #16 use of continue
# Include <iostream>
Using namespace STD;
Int main ()
{
Int X;
For (x = 0; X <= 100; X ++)
{
If (X % 2) continue;
Cout <x <''; // only the even number is output.
}
Cout <Endl;
// Getchar ();
Return 0;
}
// -- Program #17 breake usage
# Include <iostream>
Using namespace STD;
Int main ()
{
Int T;
For (t = 0; t <100; t ++)
{
If (t = 10) break;
Cout <t <'';
}
Cout <Endl <"================================== = "<Endl;
// Getchar ();
Return 0;
}
// -- Program #18 breake usage
# Include <iostream>
Using namespace STD;
Int main ()
{
Int T, count;
For (t = 0; t <100; t ++)
{
Count = 1;
For (;;)
{
Cout <count <'';
Count ++;
If (COUNT = 10)
Break;
}
Cout <Endl;
}
Cout <Endl <"================================== = "<Endl;
// Getchar ();
Return 0;
}
// -- Program #19 locate the prime number between 2-
# Include <iostream>
Using namespace STD;
Int main ()
{
Int I, J;
For (I = 2; I <100; I ++)
{
For (j = 2; j <= (I/J); j ++)
If (! (I % J ))
Break;
If (j> (I/J ))
Cout <I <"is prime./N ";
}
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #19 locate the prime number between 2-
# Include <iostream>
Using namespace STD;
Int main ()
{
Int I, J;
For (I = 2; I <100; I ++)
{
For (j = 2; j <= (I/J); j ++) // Classic is in j <= (I/J)
If (! (I % J ))
Break;
If (j> (I/J ))
Cout <I <"is prime./N ";
}
Cout <Endl <"=========================" <Endl;
For (I = 2; I <100; I ++)
{
For (j = 1; j <= I; j ++)
If (! (I % J ))
Break;
If (j> = I)
Cout <I <"is prime./N ";
}
Cout <Endl <"=========================" <Endl;
For (I = 2; I <100; I ++)
{
For (j = 2; j <= I/2; j ++)
If (! (I % J ))
Break;
If (j> I/2)
Cout <I <"is prime./N ";
}
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
// -- Program #20 magic digit Program
# Include <iostream>
# Include <cstdlib>
Using namespace STD;
Void play (INT m );
Int main ()
{
Int option;
Int magic;
Magic = rand ();
Cout <"magic is:" <magic <Endl;
Do {
Cout <"1. Get a new magic number." <Endl;
Cout <"2. Play." <Endl;
Cout <"3. Quit." <Endl;
Do {
Cout <"Enter your choice:" <Endl;
Cin> option;
} While (option <1 | option> 3 );
Switch (option)
{
Case 1: Magic = rand (); break;
Case 2: Play (MAGIC); break;
Case 3: cout <"goodbye." <Endl; break;
}
} While (option! = 3 );
Cout <Endl <"=========================" <Endl;
// Getchar ();
Return 0;
}
Void play (INT m)
{
Int T, X;
For (t = 0; t <100; t ++)
{
Cout <"guess the number :";
Cin> X;
If (x = m)
{
Cout <"** right **" <Endl;
Return;
}
Else
{
If (x <m) cout <"too low." <Endl;
Else cout <"too high." <Endl;
}
}
Cout <"you 've used up all your guesses. Try again." <Endl;
}