Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Assignment no: 03

Assignment Title: P-F Problems.

Submitted by: M. Shafi Ullah Khan

Roll no: 2902

Submitted to: Sir Awais Gondal

Faculty of Engineering and Computer Science

Department of Computer Science

National University of Modern Languages, Islamabad.


Problem 1:

1). The program should allow the user to input runs of a cricket innings.

2). Total balls for an innings should be 300.

3). Legitimate inputs are 0-6 for runs, and -1 for OUT.

4). The user should input -1 to consider a player OUT.

5). If the user enters any other number for runs, the ball should not be considered and runs
input for the same ball should be taken again. (Hint: do not let your iteration variable
increment in this case)

6). The innings should be ended if runs on 300 balls have been added or if 10 players have
been considered OUT. (Hint: Maintain a count of wickets also)

7). The program should display total score after each ball.

8). The program should display “End of Innings” once the innings has been ended.

Ans).

Code: By using For Loop

#include<iostream>

using namespace std;

int main()

int runs, i;

int innings = 300,Total_runs = 0, wickets = 0, player = 1;

for (i = 1; i <= innings; i++)

cout << "Enter Runs of " << "Player no " << player << ":
";
cin >> runs;

cout << "Total Balls: " << i << endl;

if (runs >= 0 && runs <= 6 || runs == -1)

if (runs >= 0 && runs <= 6)

Total_runs += runs;

else

wickets++;

player++;

if (runs > 6)

i--;

cout << "Total Runs: " << Total_runs << endl;

cout << "\n";

if (wickets == 10)

break;

cout << "End of Innings" << endl;

Outputs:

When a user enters runs and ten players have been dismissed. Then the innings will end.
When a user enters runs on 300 balls. Then the innings will end.

Problem 2:

Write a C++ program where the user tells a range and your program should display the
following:

1. Sum of all even numbers in that range

2. Sum of all odd numbers in that range

3. Sum of all multiples of 5 in that range

4. Sum of the numbers greater than 75 in that range (Hint: this may not even fall in the range)

Ans)

Code: By using For Loop.

#include<iostream>

using namespace std;

int main()
{

int i, start, end, sum_E = 0, sum_O = 0, sum_5 = 0, sum_75 =


0;

cout << "Range: " << endl;

cout << "Enter Starting point: ";

cin >> start;

cout << "Enter Ending point: ";

cin >> end;

for (i = start; i < end; i++)

if (i % 2 == 0)

sum_E += i;

else

sum_O += i;

if (i % 5 == 0)

sum_5 += i;

if (i > 75)

sum_75 += i;

cout << "Sum of all Even numbers in the range: " << sum_E <<
endl;

cout << "Sum of all Odd numbers in the range: " << sum_O <<
endl;
cout << "Sum of all Multiples of 5 in the range: " << sum_5 <<
endl;

cout << "Sum of all Numbers greater than 75 in the range: " <<
sum_75 << endl;

Outputs:

1). User entered a range: 120-160.

2). User entered a range: 10-100.


Problem 3:

Take a number as input from the user, and display its multiplication table from 1 to 10.

Ans).

Code:

#include<iostream>
using namespace std;
int main()
{
int number;
cout << "Enter number: ";
cin >> number;
cout << "Table of " << number << ": " << endl;
for (int i = 1; i <= 10; i++)
{
cout << number << " * " << i << " = " << number * i <<
endl;
}
}

Output:

Table of 85

You might also like