0% found this document useful (0 votes)
136 views

C++ Program.

The document contains C++ code examples demonstrating the use of various looping and flow control statements including while, for, do-while, break, continue, and switch. The examples calculate sums, powers, patterns, increment variables, count grades, and terminate loops early. Loops are used to repeatedly output numbers, calculate values, and make decisions based on conditional logic.

Uploaded by

Ahmed Mahmoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
136 views

C++ Program.

The document contains C++ code examples demonstrating the use of various looping and flow control statements including while, for, do-while, break, continue, and switch. The examples calculate sums, powers, patterns, increment variables, count grades, and terminate loops early. Loops are used to repeatedly output numbers, calculate values, and make decisions based on conditional logic.

Uploaded by

Ahmed Mahmoud
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Chapter 4 :

(1)

// Exercise 4.5: Calculate.cpp


// Calculate the sum of the integers from 1 to 10

#include <iostream>
using namespace std;
int main() {

unsigned int sum=0;


unsigned int x=1;

while (x <= 10) {


sum += x;
++x;
}

cout << "The sum is: " << sum << endl;
cout << "X= " << x << endl;

}
(2)

// Exercise 4.8 Solution: power.cpp


// Raise x to the y power.

#include <iostream>
using namespace std;
int main() {

unsigned int i=1;


unsigned int power=1;

cout << "Enter base as an integer: ";


unsigned int x;
cin >> x;

cout << "Enter exponent as an integer: ";


unsigned int y;
cin >> y;

while (i <= y) {
power *= x;
++i;
}

cout << power << endl;


}
(3)

// Exercise 4.12: Mystery.cpp

#include <iostream>
using namespace std;
int main() {
unsigned int x=1;
unsigned int total=0;

while (x <= 10) {


int y = x * x;
cout << y << endl;
total += y;
++x;
}

cout << "Total is " << total << endl;


}
(4)

// Fig. 4.17: Increment.cpp


// Prefix increment and postfix increment operators.

#include <iostream>
using namespace std;
int main() {

unsigned int c=5;


cout << "c before postincrement: " << c << endl; // prints 5
cout << "postincrementing c: " << c++ << endl; // prints 5
cout << "c after postincrement: " << c << endl; // prints 6

cout << endl;

c = 5;
cout << " c before preincrement: " << c << endl; // prints 5
cout << " preincrementing c: " << ++c << endl; // prints 6
cout << " c after preincrement: " << c << endl; // prints 6
}
(5)

// Exercise 4.21: Mystery2.cpp

#include <iostream>
using namespace std;
int main() {

unsigned int count=1;

while (count <= 10) {


cout << (count % 2 == 1 ? "****" : "++++++++") << endl;
++count;
}
}
(6)

// Exercise 4.22: Mystery3.cpp

#include <iostream>
using namespace std;
int main() {

unsigned int row=10;

while (row >= 1) {


unsigned int column=1;

while (column <= 10) {


cout << (row % 2 == 1 ? "<" : ">");
++column;
}

--row;
cout << endl;
}
}
Chapter 5 :

)1(

// Fig. 5.1: WhileCounter.cpp


// Counter-controlled iteration with the while iteration
statement.

#include <iostream>
using namespace std;
int main() {

unsigned int counter=1;

while (counter <= 10) {


cout << counter << " ";
++counter;
}

cout << endl;


}
)2(

// Fig. 5.2: ForCounter.cpp


// Counter-controlled iteration with the for iteration
statement.

#include <iostream>
using namespace std;
int main() {

for (int counter=1; counter <= 10; counter++) {


cout << counter << " ";
}

cout << endl;


}
)3(

// Fig. 5.5: Sum.cpp


// Summing integers with the for statement.

#include <iostream>
using namespace std;
int main() {

unsigned int total=0 ;

for (unsigned int number=2; number <= 20; number += 2)


{
total += number;
}

cout << "Sum is " << total << endl;


}
)4(

// Fig. 5.9: DoWhileTest.cpp


// do...while iteration statement.

#include <iostream>
using namespace std;
int main() {

unsigned int counter=1;

do {
cout << counter << " ";
++counter;
} while (counter <= 10);

cout << endl;


}
)5(

// Exercise 5.10: Printing.cpp

#include <iostream>
using namespace std;
int main() {

for (int i=1; i <= 10; i++) {


for (int j=1; j <= 5; j++) {
cout << "@";
}

cout << endl;


}
}
)6(

// Fig. 5.11: LetterGrades.cpp


// Using a switch statement to count letter grades.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {

int total=0;
unsigned int gradeCounter=0;
unsigned int aCount=0;
unsigned int bCount=0;
unsigned int cCount=0;
unsigned int dCount=0;
unsigned int fCount=0;

cout << "Enter the integer grades in the range 0-100.\n"


<< "Type the end-of-file indicator to terminate input:\n"
<< " On UNIX/Linux/Mac OS X type <Ctrl> d then press Enter\n"
<< " On Windows type <Ctrl> z then press Enter\n";

int grade;

while (cin >> grade) {


total += grade;
++gradeCounter;

switch (grade / 10) {


case 9:
case 10:
++aCount;
break;

case 8:
++bCount;
break;
case 7:
++cCount;
break;

case 6:
++dCount;
break;

default:
++fCount;
break;
}
}

cout << fixed << setprecision(2);

cout << "\nGrade Report:\n";

if (gradeCounter != 0) {

double average = static_cast<double>(total) / gradeCounter;

cout << "Total of the " << gradeCounter << " grades entered is "
<< total << "\nClass average is " << average
<< "\nNumber of students who received each grade:"
<< "\nA: " << aCount << "\nB: " << bCount << "\nC: " << cCount
<< "\nD: " << dCount << "\nF: " << fCount << endl;
}
else {
cout << "No grades were entered" << endl;
}
}
)7(

// Fig. 5.13: BreakTest.cpp


// break statement exiting a for statement.

#include <iostream>
using namespace std;
int main() {

unsigned int count;

for (count = 1; count <= 10; count++) {


if (count == 5) {
break; // terminates loop if count is 5
}

cout << count << " ";


}

cout << "\nBroke out of loop at count = " << count << endl;
}
)8(

// Fig. 5.14: ContinueTest.cpp


// continue statement terminating an iteration of a for statement.

#include <iostream>
using namespace std;
int main() {

for (unsigned int count=1; count <= 10; count++) {

if (count == 5) {
continue; // skip remaining code in loop body if count is 5
}

cout << count << " ";


}

cout << "\nUsed continue to skip printing 5" << endl;


}

You might also like