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

P2 - Programming Assessment

Uploaded by

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

P2 - Programming Assessment

Uploaded by

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

Programming Question 1 (a)

Programming Question 1 (b) [4 marks]

using System;
class Program
{
static void Main()
{
int number = 0;
while (number <= 0)
{
Console.Write("Enter a number greater than 0: ");
number = int.Parse(Console.ReadLine());
}

if (IsIncreasing(number))
{
Console.WriteLine($"{number} is not a bouncy number because it is an increasing
number.");
}
else if (IsDecreasing(number))
{
Console.WriteLine($"{number} is not a bouncy number because it is a decreasing
number.");
}
else if
(IsPerfectlyBouncy(number))
{
Console.WriteLine($"{number} is a perfectly bouncy number.");
}
Else
{
Console.WriteLine($"{number} is a bouncy number.");
}

static bool IsIncreasing(int number)


{

Demonstrate your program works correctly by testing and screenshotting the


following numbers
Number Screenshot
13578

973

98657

13521

Programming Question 2 [ 15 marks ]

Write a C# program that prompts the user to enter a positive integer greater than 1.
The program should then find all the prime factors of the entered number and count
how many times each prime factor divides the number.

The program should:


 Keep prompting the user to enter a positive integer greater than 1 until they
enter a valid number.
 Once a valid number is entered, display each prime factor and how many
times it divides the number (its exponent in the factorization).
 The program should use loops to perform the factorization and if-statements
to check conditions such as divisibility.
 Output the results as shown below

Output Example
Enter a positive integer greater than 1: 60

Prime factors of 60 are:


2 appears 2 times
3 appears 1 time
5 appears 1 time

using System;
class Program
{
static void Main()
{
int inputValue = 0;
while((inputValue <= 1))
{
Console.Write("Enter a positive integer greater than 1: ");
inputValue = Convert.ToInt32(Console.ReadLine());
if (inputValue <= 1)
{
Console.WriteLine("Invalid input. Please enter a valid number greater than
1.");
}
}
Console.WriteLine($"\nPrime factors of {inputValue} are:");
for (int i = 2; i <= inputValue; i++)
{
int count = 0; while (inputValue % i == 0)
{
count++; inputValue /= i;
}
if (count > 0)
{
Console.WriteLine($"{i} appears {count} time(s)");
}
}
}
}

Programming Question 2 (b) [4 marks]

Demonstrate your program works correctly by testing and screenshotting the


following numbers
Numb Screenshot
er
35

75

6.5

Twent
y
Asking
for
anothe
r
numbe
r if an
int is
not
entere
d
Programming Question 3 [ 5 marks ]

Write a C# program that manages student grades using a queue. The program
should:
1. Input:
 Prompt the user to enter the number of students (n)
 Followed by n grades (integers).
 Each grade should be between 0 and 100.
2. Queue: Use a queue to store the grades entered by the user.
3. Output: After all grades have been entered, output the grades in the order
they were entered.

Output example
Enter the number of students: 3

Enter grade for student 1: 85


Enter grade for student 2: 92
Enter grade for student 3: 78

Grades in the order entered: 85 92 78


using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
Queue<int> scores = new Queue<int>();
Console.Write("Enter the number of students: ");
int numberOfStudents = int.Parse(Console.ReadLine());
for (int i = 1; i <= numberOfStudents; i++)

{
int score = -1;
while (score < 0 || score > 100)
{
Console.Write($"Enter score for student {i}: ");
string input = Console.ReadLine();
bool isValidInteger = true;
if (isValidInteger)
{
score = int.Parse(input);
if (score >= 0 && score <= 100)
{
scores.Enqueue(score);
}
else
{
Console.WriteLine("Please enter a score between 0 and 100.");
score = -1;
}
}
else
{
Console.WriteLine("Please enter a valid integer."); score = -1;
}
}
}
Console.Write("Scores in the order entered: ");
foreach (int score in scores)
{
Console.Write(score + " ");
}
}
}
Programming Question 3 (b) [4 marks]

Demonstrate your program works correctly by testing and screenshotting the


following information
Numbe Screenshot
r
Enter 3
studen
ts

Grade
1 - 50
Grade
2 – 2.4
Grade
3 - 101
Asking
for
anothe
r
numbe
r if an
int is
not
entere
d

You might also like