[go: up one dir, main page]

100% found this document useful (1 vote)
511 views7 pages

Pba Solution (10TH)

Uploaded by

pleasenoads770
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
100% found this document useful (1 vote)
511 views7 pages

Pba Solution (10TH)

Uploaded by

pleasenoads770
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/ 7

PRACTICAL BASED ASSESMENT SOLUTION(PBA)

Q1(i)

#include <stdio.h>

int main() {

int num, I;

// Ask the user to enter a number

Printf(“Enter a number to print its multiplication table: “);

Scanf(“%d”, &num);

// Print the multiplication table

Printf(“Multiplication table of %d:\n”, num);

For(I = 1; I <= 10; i++) {

Printf(“%d x %d = %d\n”, num, I, num * i);

Return 0;

Explanation:

The program asks the user to input a number.


It then uses a for loop to iterate from 1 to 10.

In each iteration, it multiplies the given number by the current loop index and prints the
result.

ii) To convert the multiplication table program into a do-while loop, here’s how it would
look:

Original Program with do-while Loop

#include <stdio.h>

Int main() {

Int num, I = 1;

// Ask the user to enter a number

Printf(“Enter a number to print its multiplication table: “);

Scanf(“%d”, &num);

// Print the multiplication table using a do-while loop

Printf(“Multiplication table of %d:\n”, num);

Do {

Printf(“%d x %d = %d\n”, num, I, num * i);

I++;

} while(I <= 10);

Return 0;
}

Explanation:

The do-while loop ensures that the multiplication table prints at least once, even if I is
initialized to a different value.

The loop continues as long as I is less than or equal to 10.

Part iii)Output if the User Enters 5:

If the user enters 5, the output will be:

Enter a number to print its multiplication table: 5

Multiplication table of 5:

5x1=5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50

This output remains the same as with a for loop because the logic for printing the table is
identical.

Q2 (i)
#include <stdio.h>

Int main() {

Int score = 0;

Int input;

Printf(“Game Program: Enter actions to update score.\n”);

Printf(“1 for scoring a point\n”);

Printf(“2 for penalty\n”);

Printf(“0 to stop the game\n”);

While (1) {

Printf(“Enter action: “);

Scanf(“%d”, &input);

If (input == 1) {

Score++; // Increment score for scoring a point

} else if (input == 2) {

Score--; // Decrement score for penalty

} else if (input == 0) {

Break; // Stop the game

} else {

Printf(“Invalid input. Please enter 1, 2, or 0.\n”);

Continue;

}
Printf(“Updated score: %d\n”, score);

Printf(“Final score: %d\n”, score);

Return 0;

Explanation:

1. The program initializes the score to zero.


2. It prompts the user to enter actions:

1 to score a point, which increments the score.

2 for a penalty, which decrements the score.

0 to stop the game.

3. After each valid input, the program displays the updated score.
4. The program exits when the user enters 0, and it displays the final score.

Q2(ii)

The original code contains several syntax issues. Here’s the corrected version:

#include <stdio.h>

Int main() {
Int day;
Printf(“Enter a number (1-7) to display the day of the week: “);
Scanf(“%d”, &day);

Switch(day) {
Case 1:
Printf(“Monday\n”);
Break;
Case 2:
Printf(“Tuesday\n”);
Break;
Case 3:
Printf(“Wednesday\n”);
Break;
Case 4:
Printf(“Thursday\n”);
Break;
Case 5:
Printf(“Friday\n”);
Break;
Case 6:
Printf(“Saturday\n”);
Break;
Case 7:
Printf(“Sunday\n”);
Break;
Default:
Printf(“Invalid input\n”);
}

Return 0;
}

Modification for Part (ii)

To allow the program to accept lowercase letters for days, such as ‘m’ for Monday,
we would need to change the input type to accept characters and then match those
characters. However, given the current structure (numeric input for days), this part
doesn’t apply directly unless we change the entire structure of the program.

If the intention is to accept characters instead, you could rework the code to use
char inputs and map ‘m’, ‘t’, etc., to days. Please clarify if this is what is needed.
Output for Part (iii)

If we modify the program to accept lowercase characters and the user enters ‘m’ as
input, the program should display “Monday” as output after making the necessary
changes to handle character inputs.

You might also like