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

Programming Task

The document outlines two programming tasks: one for swapping vowels in a user-input string and another for calculating the nth Harshad number. It includes examples, expected outputs, and detailed mark schemes for evaluating the programming solutions. Additionally, it provides source code in various programming languages to demonstrate the required functionality.

Uploaded by

User.9463820
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Programming Task

The document outlines two programming tasks: one for swapping vowels in a user-input string and another for calculating the nth Harshad number. It includes examples, expected outputs, and detailed mark schemes for evaluating the programming solutions. Additionally, it provides source code in various programming languages to demonstrate the required functionality.

Uploaded by

User.9463820
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Q1.

Write a program that asks the user to enter a string. It should then change the order of the
vowels in the string and display the result.

If there are n vowels in the string, the 1st vowel in the string should swap with the nth
vowel in the string, the 2nd vowel in the string should swap with the (n-1)th vowel in the
string, and so on.

The letters a, e, i, o and u are the only vowels.

Examples

If the user enters the string horse then the program should display
the string herso.

If the user enters the string goose then the program should display
the string geoso.

If the user enters the string pinkfairyarmadillo then the program


should display the string ponkfiaryarmidalli.

If the user enters the string nakedmolerat then the program should
display the string nakedmolerat.

If the user enters the string lynx then the program should display
the string lynx.

If the user enters the string pig then the program should display the
string pig.

You may assume the string that the user enters will only contain lowercase letters.

Evidence that you need to provide

(a) Your PROGRAM SOURCE CODE.


(12)

(b) SCREEN CAPTURE(S) showing the results of three tests of the program by
entering the strings persepolis, darius and xerxes.
(1)
(Total 13 marks)

Q2.
A Harshad number is a positive integer which is exactly divisible by the sum of its digits.
The first twelve Harshad numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12 and 18

• 36 is a Harshad number. The digits of 36 are 3 and 6; the sum of these digits is 9 as
3 + 6 = 9 and 36 is exactly divisible by 9 (36 ÷ 9 = 4)

• 300 is a Harshad number. The digits of 300 are 3, 0 and 0; the sum of these digits is
3 as 3 + 0 + 0 = 3 and 300 is exactly divisible by 3 (300 ÷ 3 = 100)

Page 1 of 21
• 15 is not a Harshad number. The digits of 15 are 1 and 5; the sum of these digits is
6 as 1 + 5 = 6 and 15 is not exactly divisible by 6

Write a program that asks the user to enter a number, n, and will then calculate and
display the nth Harshad number.

Example
If the user enters the number 12 then the program should calculate
and display the twelfth Harshad number. The twelfth Harshad
number is 18

You may assume that the number that the user enters will be a positive integer.

Evidence that you need to provide

(a) Your PROGRAM SOURCE CODE.


(12)
(b) SCREEN CAPTURE(S) showing the result of testing the program by entering the
number 600
(1)
(Total 13 marks)

Page 2 of 21
Mark schemes

Q1.
(a) 4 marks for AO3 (design) and 8 marks for AO3 (programming)

Mark Scheme

Level Description Mark


Range

4 A line of reasoning has been followed to arrive at 10–12


a logically structured working or almost fully
working programmed solution that meets most of
the requirements. All of the appropriate design
decisions have been taken. To award 12 marks,
all of the requirements must be met.
3 There is evidence that a line of reasoning has 7–9
been followed to produce a logically structured
program. The program displays relevant
prompts, inputs the required string, has at least
one iterative structure and at least one selection
structure and uses appropriate variables to store
most of the needed data. An attempt has been
made to swap the positions of vowels in the
string, although this may not work correctly
under all circumstances. The solution
demonstrates good design work as most of the
correct design decisions have been made.

2 A program has been written and some 4–6


appropriate, syntactically correct programming
language statements have been written. There is
evidence that a line of reasoning has been
partially followed as although the program may
not have the required functionality, it can be
seen that the response contains some of the
statements that would be needed in a working
solution. There is evidence of some appropriate
design work as the response recognises at least
one appropriate technique that could be used by
a working solution, regardless of whether this
has been implemented correctly.
1 A program has been written and a few 1–3
appropriate programming language statements
have been written but there is no evidence that a
line of reasoning has been followed to arrive at a
working solution. The statements written may or
may not be syntactically correct. It is unlikely that
any of the key design elements of the task have
been recognised.

Guidance

Evidence of AO3 design – 4 points:

Page 3 of 21
Evidence of design to look for in responses:

1. Identifying that string concatenation is needed when swapping vowels in


the string // identifying that swapping items in a list of characters is
needed.
2. Identifying that a loop is needed that repeats a number of times
determined by the word entered by the user // identifying that a loop is
needed that repeats a number of times determined by the number of
vowels in the word entered by the user.
3. Identifying that two integer variables are needed to store positions of
characters in the string // identifying that an ordered list of vowels in the
string needs to be created // identifying one integer variable is needed to
show the distance from the start and end of the string (R. if no attempt to
use this integer with the start and end positions of the string).
4. Selection structure that checks if a character is a vowel A. more than
one selection structure used R. if no attempt at comparing with each of
the five vowels.

Note that AO3 (design) points are for selecting appropriate techniques to use
to solve the problem, so should be credited whether the syntax of
programming language statements is correct or not and regardless of whether
the solution works.

Evidence for AO3 programming – 8 points:

Evidence of programming to look for in response:

5. Suitable prompt asking user to enter a string followed by user input


being assigned to appropriate variable.
6. Iterative structure that repeats a number of times that is sufficient to
check all the characters in the string.
7. Correctly checks if a character is a vowel.
8. Correctly checks all characters in the string to see if they are vowels.
9. Swaps/moves the position of two characters in the string.
10. Program only moves/changes the position of vowels.
11. Program works correctly if a string contains one vowel and works
correctly if a string contains no vowels. R. if program does not attempt to
swap positions of vowels or identify that there are less than two vowels.
12. Program works correctly under all circumstances.

I. additional loop to get program to repeat multiple times.

DPT. mark points 7 and 8 if only checks for some vowels or includes at most
one non-vowel character.

Max 11 if any errors

VB.Net

Console.Write("Enter a string: ")


Dim UserString As String = Console.ReadLine()
Dim EndPos As Integer = UserString.Length - 1
Dim StartPos As Integer = 0
While EndPos > StartPos
If "aeiou".Contains(UserString(EndPos)) Then
If "aeiou".Contains(UserString(StartPos)) Then
Dim NewString As String = UserString.Substring(0, StartPos) +
UserString(EndPos) + UserString.Substring(StartPos + 1, EndPos -
StartPos - 1) + UserString(StartPos) + UserString.Substring(EndPos +
1, UserString.Length - 1 - EndPos)

Page 4 of 21
UserString = NewString
StartPos += 1
EndPos -= 1
Else
StartPos += 1
End If
Else
EndPos -= 1
If Not "aeiou".Contains(UserString(StartPos)) Then
StartPos += 1
End If
End If
End While
Console.WriteLine(UserString)
Console.ReadLine()

Alternative answer

Console.Write("Enter a string: ")


Dim UserString As String = Console.ReadLine()
Dim EndPos As Integer = UserString.Length - 1
Dim StartPos As Integer = 0
Dim StartNewWord As String = ""
Dim EndNewWord As String = ""
While EndPos > StartPos
If "aeiou".Contains(UserString(EndPos)) Then
If "aeiou".Contains(UserString(StartPos)) Then
EndNewWord = UserString(StartPos) + EndNewWord
StartNewWord += UserString(EndPos)
StartPos += 1
EndPos -= 1
Else
StartNewWord += UserString(StartPos)
StartPos += 1
End If
Else
EndNewWord = UserString(EndPos) + EndNewWord
EndPos -= 1
If Not "aeiou".Contains(UserString(StartPos)) Then
StartNewWord += UserString(StartPos)
StartPos += 1
End If
End If
End While
If StartPos = EndPos Then
Console.WriteLine(StartNewWord + UserString(StartPos) +
EndNewWord)
Else
Console.WriteLine(StartNewWord + EndNewWord)
End If
Console.ReadLine()

Python 3

UserString = input("Enter a string: ")


EndPos = len(UserString) - 1
StartPos = 0
while EndPos > StartPos:
if UserString[EndPos] in "aeiou":
if UserString[StartPos] in "aeiou":
UserString = UserString[0: StartPos] + UserString[EndPos] +
UserString[StartPos+1: EndPos] + UserString[StartPos] +
UserString[EndPos+1:len(UserString)]

Page 5 of 21
StartPos += 1
EndPos -= 1
else:
StartPos += 1
else:
EndPos -= 1
if UserString[StartPos] not in "aeiou":
StartPos += 1
print(UserString)

Alternative answer

UserString = input("Enter a string: ")


EndPos = len(UserString) - 1
StartPos = 0
StartNewString = ""
EndNewString = ""
while EndPos > StartPos:
if UserString[EndPos] in "aeiou":
if UserString[StartPos] in "aeiou":
EndNewString = UserString[StartPos] + EndNewString
StartNewString += UserString[EndPos]
StartPos += 1
EndPos -= 1
else:
StartNewString += UserString[StartPos]
StartPos += 1
else:
EndNewString = UserString[EndPos] + EndNewString
EndPos -= 1
if UserString[StartPos] not in "aeiou":
StartNewString += UserString[StartPos]
StartPos += 1
if StartPos == EndPos:
print(StartNewString + UserString[StartPos] + EndNewString)
else:
print(StartNewString + EndNewString)

Python 2

UserString = raw_input("Enter a string: ")


EndPos = len(UserString) - 1
StartPos = 0
while EndPos > StartPos:
if UserString[EndPos] in "aeiou":
if UserString[StartPos] in "aeiou":
UserString = UserString[0: StartPos] + UserString[EndPos] +
UserString[StartPos+1: EndPos] + UserString[StartPos] +
UserString[EndPos+1:len(UserString)]
StartPos += 1
EndPos -= 1
else:
StartPos += 1
else:
EndPos -= 1
if UserString[StartPos] not in "aeiou":
StartPos += 1
print UserString

Alternative answer

UserString = raw_input("Enter a string: ")


EndPos = len(UserString) - 1

Page 6 of 21
StartPos = 0
StartNewString = ""
EndNewString = ""
while EndPos > StartPos:
if UserString[EndPos] in "aeiou":
if UserString[StartPos] in "aeiou":
EndNewString = UserString[StartPos] + EndNewString
StartNewString += UserString[EndPos]
StartPos += 1
EndPos -= 1
else:
StartNewString += UserString[StartPos]
StartPos += 1
else:
EndNewString = UserString[EndPos] + EndNewString
EndPos -= 1
if UserString[StartPos] not in "aeiou":
StartNewString += UserString[StartPos]
StartPos += 1
if StartPos == EndPos:
print StartNewString + UserString[StartPos] + EndNewString
else:
print StartNewString + EndNewString

C#

Console.Write("Enter a string: ");


string userString = Console.ReadLine();
int endPos = (userString.Length - 1);
int startPos = 0;
while (endPos > startPos)
{
if ("aeiou".Contains(userString[endPos]))
{
if ("aeiou".Contains(userString[startPos]))
{
string newString = userString.Substring(0, startPos)
+ userString[endPos]
+ userString.Substring(startPos + 1, endPos - startPos -
1)
+ userString[startPos]
+ userString.Substring(endPos + 1, userString.Length - 1
- endPos);
userString = newString;
startPos++;
endPos--;
}
else
{
startPos++;
}
}
else
{
endPos--;
if (!"aeiou".Contains(userString[startPos]))
{
startPos++;
}
}
}
Console.WriteLine(userString);

Page 7 of 21
Alternative answer

Console.Write("Enter a string: ");


string userString = Console.ReadLine();
int endPos = (userString.Length - 1);
int startPos = 0;
string startNewWord = "";
string endNewWord = "";
while ((endPos > startPos))
{
if ("aeiou".Contains(userString[endPos]))
{
if ("aeiou".Contains(userString[startPos]))
{
endNewWord = (userString[startPos] + endNewWord);
startNewWord = (startNewWord + userString[endPos]);
startPos++;
endPos--;
}
else
{
startNewWord = (startNewWord + userString[startPos]);
startPos++;
}
}
else
{
endNewWord = userString[endPos] + endNewWord;
endPos--;
if (!"aeiou".Contains(userString[startPos]))
{
startNewWord = (startNewWord + userString[startPos]);
startPos++;
}
}
}
if ((startPos == endPos))
{
Console.WriteLine((startNewWord + (userString[startPos] +
endNewWord)));
}
else
{
Console.WriteLine((startNewWord + endNewWord));
}

Alternative answer

Console.Write("Enter a string: ");


string userString = Console.ReadLine();
int endPos = userString.Length - 1;
int startPos = 0;
char [] charArray = userString.ToCharArray(0,userString.Length);
while (endPos > startPos)
{
if (IsaVowel(charArray[startPos]))
{
while (!IsaVowel(charArray[endPos]) && endPos > startPos)
{
endPos--;
}
if (endPos > startPos)
{

Page 8 of 21
char temp = charArray[startPos];
charArray[startPos] = charArray[endPos];
charArray[endPos] = temp;
endPos--;
}
}
startPos++;
}
userString = new string (charArray);
Console.WriteLine(userString);

private static bool IsaVowel(char v)


{
if (v == 'a' || v == 'e' || v == 'i' || v == 'o' || v == 'u')
{
return true;
}
return false;
}

Pascal/Delphi

var
UserString, Vowels, NewString : string;
EndPos, StartPos : integer;
begin
Vowels := 'aeiou';
write('Enter a string: ');
readln(UserString);
EndPos := length(UserString);
StartPos := 1;
while EndPos > StartPos do
begin
if pos(UserString[EndPos], Vowels) > 0 then
begin
if pos(UserString[StartPos], Vowels) > 0 then
begin
NewString := copy(UserString, 1, StartPos - 1) +
UserString[EndPos] + copy(UserString, StartPos + 1, EndPos -StartPos
- 1) + UserString[StartPos] + copy(UserString, EndPos + 1,
length(UserString) - EndPos);
UserString := NewString;
inc(StartPos);
dec(EndPos);
end
else
inc(StartPos);
end
else
begin
dec(EndPos);
if pos(UserString[StartPos], Vowels) = 0 then
inc(StartPos);
end;
end;
writeln(UserString);
readln;
end.

Alternative

var
UserString, Vowels, StartNewWord, EndNewWord : string;

Page 9 of 21
EndPos, StartPos : integer;
begin
Vowels := 'aeiou';
write('Enter a string: ');
readln(UserString);
EndPos := length(UserString);
StartPos := 1;
StartNewWord := '';
EndNewWord := '';
while EndPos > StartPos do
begin
if pos(UserString[EndPos], Vowels) > 0 then
begin
if pos(UserString[StartPos], Vowels) > 0 then
begin
EndNewWord := UserString[StartPos] + EndNewWord;
StartNewWord := StartNewWord + UserString[EndPos];
inc(StartPos);
dec(EndPos);
end
else
begin
StartNewWord := StartNewWord + UserString[StartPos];
inc(StartPos);
end;
end
else
begin
EndNewWord := UserString[EndPos] + EndNewWord;
dec(EndPos);
if pos(UserString[StartPos], Vowels) = 0 then
begin
StartNewWord := StartNewWord + UserString[StartPos];
inc(StartPos);
end;
end;
end;
if StartPos = EndPos then
writeln(StartNewWord + UserString[StartPos] + EndNewWord)
else
writeln(StartNewWord + EndNewWord);
readln;
end.

Java

Console.writeLine("Enter a string: ");


String input = Console.readLine();
int startPos = 0;
int endPos = input.length()-1;
String startNewWord = "", endNewWord = "";
while (endPos>startPos) {
if (input.charAt(endPos) == 'a' || input.charAt(endPos) == 'e' ||
input.charAt(endPos) == 'i'
|| input.charAt(endPos) == 'o' || input.charAt(endPos) == 'u')
{
if (input.charAt(startPos) == 'a' || input.charAt(startPos) ==
'e' || input.charAt(startPos) == 'i'
|| input.charAt(startPos) == 'o' || input.charAt(startPos) ==
'u') {
endNewWord = input.charAt(startPos) + endNewWord;
startNewWord += input.charAt(endPos);
startPos++;
endPos--;

Page 10 of 21
}
else {
startNewWord += input.charAt(startPos);
startPos++;
}
}
else {
endNewWord = input.charAt(endPos) + endNewWord;
endPos--;
if (input.charAt(startPos) == 'a' || input.charAt(startPos) ==
'e' || input.charAt(startPos) == 'i'
|| input.charAt(startPos) == 'o' || input.charAt(startPos) ==
'u') {
startNewWord += input.charAt(startPos);
startPos++;
}
}
}
if (startPos == endPos) {
Console.writeLine(startNewWord + input.charAt(startPos) +
endNewWord);
}
else {
Console.writeLine(startNewWord + endNewWord);
}
12

(b) Mark is for AO3 (evaluate)

**** SCREEN CAPTURE ****


Must match code from (a), including prompts on screen capture matching
those in code.
Code for (a) must be sensible.

Screen capture showing the string persepolis being entered and then the
string pirsopeles being displayed and screen capture showing the string
darius being entered and then the string durias being displayed and screen
capture showing the string xerxes being entered and then the string xerxes
being displayed;
I. order of tests

1
[13]

Page 11 of 21
Q2.
(a) 4 marks for AO3 (design) and 8 marks for AO3 (programming)

Mark
Level Description
Range

4 A line of reasoning has been followed to arrive at a 10-12


logically structured working or almost fully working
programmed solution that meets most of the
requirements. All of the appropriate design decisions
have been taken. To award 12 marks, all of the
requirements must be met.

3 There is evidence that a line of reasoning has been 7-9


followed to produce a logically structured program. The
program displays relevant prompts, inputs the required
number, has at least one iterative structure and one
selection structure and uses appropriate variables to
store most of the needed data. An attempt has been
made to determine if a number is a Harshad number,
although this may not work correctly under all
circumstances. The solution demonstrates good design
work as most of the correct design decisions have
been made.

2 A program has been written and some appropriate, 4-6


syntactically correct programming language statements
have been written. There is evidence that a line of
reasoning has been partially followed as although the
program may not have the required functionality, it can
be seen that the response contains some of the
statements that would be needed in a working solution.
There is evidence of some appropriate design work as
the response recognises at least one appropriate
technique that could be used by a working solution,
regardless of whether this has been implemented
correctly.

1 A program has been written and a few appropriate 1-3


programming language statements have been written
but there is no evidence that a line of reasoning has
been followed to arrive at a working solution. The
statements written may or may not be syntactically
correct. It is unlikely that any of the key design
elements of the task have been recognised.

Guidance

Evidence of AO3 design – 4 points:

Evidence of design to look for in responses:

1. Identifying that integer division is needed when calculating


the sum of the digits // identifying that a character in string
needs to be converted to a number data type when
calculating the sum of the digits
2. Identifying that a loop is needed that repeats a number of
times determined by the number entered by the user //
identifying that a loop is needed that repeats until the nth

Page 12 of 21
Harshad number is found
3. Identifying that nested iteration is needed
4. Selection structure that compares sum of digits (I. incorrectly
calculated) with a number

Note that AO3 (design) points are for selecting appropriate


techniques to use to solve the problem, so should be credited
whether the syntax of programming language statements is correct
or not and regardless of whether the solution works.

Evidence for AO3 programming – 8 points:

Evidence of programming to look for in response:

5. Suitable prompt asking user to enter a number followed by


user input being assigned to appropriate variable
6. Iterative structure that repeats a number of times sufficient to
find all the digits of a number
7. Calculates the sum of all the digits of a number
8. Calculates the remainder from dividing a number by its sum
of digits A. incorrect calculation for sum of digits
9. Resets the variable used to store the sum of digits to 0 in an
appropriate place
10. Program works correctly for the first nine Harshad numbers
(1 to 9)
11. Program will display 10/12/18 if the user enters the number
10/11/12
12. Program displays the correct value for the nth Harshad
number under all circumstances I. displaying Harshad
numbers that appear before the nth Harshad number

Alternative mark scheme

This mark scheme is to be used if solution uses a recursive


subroutine to calculate the sum of the digits.

3. Identifying that a recursive subroutine is needed to calculate


the sum of the digits.
6. Recursive subroutine has an appropriate base case.
9. Sets the variable used to store the sum of digits to the result
returned by the call to the recursive subroutine in an
appropriate place.

Max 11 if any errors.


12

VB.Net

Console.Write("Enter value for n: ")


Dim Number As Integer = Console.ReadLine
Dim NumbersFoundSoFar As Integer = 0
Dim CurrentNumber As Integer = 1
Dim SumOfDigits As Integer
While NumbersFoundSoFar <> Number
SumOfDigits = 0
Dim NumAsString As String = CStr(CurrentNumber)
For count = 0 To NumAsString.Length - 1
SumOfDigits += Val(NumAsString(count))
Next
If CurrentNumber Mod SumOfDigits = 0 Then

Page 13 of 21
NumbersFoundSoFar += 1
If NumbersFoundSoFar = Number Then
Console.WriteLine(CurrentNumber)
End If
End If
CurrentNumber += 1
End While

Alternative answer

Console.Write("Enter value for n: ")


Dim Number As Integer = Console.ReadLine
Dim NumbersFoundSoFar As Integer = 0
Dim CurrentNumber As Integer = 1
Dim SumOfDigits As Integer
While NumbersFoundSoFar <> Number
Dim Temp As Integer = CurrentNumber
SumOfDigits = 0
While Temp > 0
SumOfDigits += Temp Mod 10
Temp \= 10
End While
If CurrentNumber Mod SumOfDigits = 0 Then
NumbersFoundSoFar += 1
If NumbersFoundSoFar = Number Then
Console.WriteLine(CurrentNumber)
End If
End If
CurrentNumber += 1
End While

Recursive answer

Function SumDigits(ByVal Num As Integer) As Integer


Dim Sum As Integer
If Num = 0 Then
Sum = 0
Else
Sum = Num Mod 10 + SumDigits(Num\10)
End If
Return Sum
End Function

Sub Main()
Console.Write("Enter value for n: ")
Dim n As Integer = Console.ReadLine()
Dim NthHarshad As Integer = 0
Dim Counter As Integer = 1
Dim Value As Integer
While n <> NthHarshad
Value = SumDigits(Counter)
If Counter Mod Value = 0 Then
NthHarshad += 1
End If
If n = NthHarshad Then
Console.WriteLine(Counter)
End If
Counter = Counter + 1
End While
End Sub

Python2

Page 14 of 21
Number = int(raw_input("Enter value for n: "))
NumbersFoundSoFar = 0
CurrentNumber = 1
while NumbersFoundSoFar != Number:
SumOfDigits = 0
NumAsString = str(CurrentNumber)
for count in range (0, len(NumAsString)):
SumOfDigits += int(NumAsString[count])
if CurrentNumber % SumOfDigits == 0:
NumbersFoundSoFar += 1
if NumbersFoundSoFar == Number:
print CurrentNumber
CurrentNumber += 1

Alternative answer

Number = int(raw_input("Enter value for n: "))


NumbersFoundSoFar = 0
CurrentNumber = 1
while NumbersFoundSoFar != Number:
Temp = CurrentNumber
SumOfDigits = 0
while Temp > 0:
SumOfDigits += Temp % 10
Temp = Temp // 10
if CurrentNumber % SumOfDigits == 0:
NumbersFoundSoFar += 1
if NumbersFoundSoFar == Number:
print CurrentNumber
CurrentNumber += 1

Recursive answer

def SumDigits(Num):
if Num == 0:
Sum = 0
else:
Sum = Num % 10 + SumDigits(Num//10)
return Sum

n = int(raw_input("Enter value for n: "))


NthHarshad = 0
Counter = 1
while n != NthHarshad:
Value = SumDigits(Counter)
if Counter % Value == 0:
NthHarshad += 1
if n == NthHarshad:
print Counter
Counter = Counter + 1

Python3

Number = int(input("Enter value for n: "))


NumbersFoundSoFar = 0
CurrentNumber = 1
while NumbersFoundSoFar != Number:
SumOfDigits = 0
NumAsString = str(CurrentNumber)
for count in range (0, len(NumAsString)):
SumOfDigits += int(NumAsString[count])
if CurrentNumber % SumOfDigits == 0:
NumbersFoundSoFar += 1

Page 15 of 21
if NumbersFoundSoFar == Number:
print(CurrentNumber)
CurrentNumber += 1

Alternative answer

Number = int(input("Enter value for n: "))


NumbersFoundSoFar = 0
CurrentNumber = 1
while NumbersFoundSoFar != Number:
Temp = CurrentNumber
SumOfDigits = 0
while Temp > 0:
SumOfDigits += Temp % 10
Temp = Temp // 10
if CurrentNumber % SumOfDigits == 0:
NumbersFoundSoFar += 1
if NumbersFoundSoFar == Number:
print(CurrentNumber)
CurrentNumber += 1

Recursive answer

def SumDigits(Num):
if Num == 0:
Sum = 0
else:
Sum = Num % 10 + SumDigits(Num//10)
return Sum

n = int(input("Enter value for n: "))


NthHarshad = 0
Counter = 1
while n != NthHarshad:
Value = SumDigits(Counter)
if Counter % Value == 0:
NthHarshad += 1
if n == NthHarshad:
print(Counter)
Counter = Counter + 1

C#

Console.Write("Enter value for n: ");


int number = Convert.ToInt32(Console.ReadLine());
int numbersFoundSoFar = 0;
int currentNumber = 1;
int sumOfDigits;
while ((numbersFoundSoFar != number))
{
int temp = currentNumber;
sumOfDigits = 0;
string numAsString = currentNumber.ToString();
for (int count = 0; (count <= (numAsString.Length - 1));
count++)
{
sumOfDigits = (sumOfDigits +
Convert.ToInt32(numAsString[count].ToString()));
}

if (((currentNumber % sumOfDigits) == 0))


{
numbersFoundSoFar++;

Page 16 of 21
if ((numbersFoundSoFar == number))
{
Console.WriteLine(currentNumber);
}
}
currentNumber++;
}
Console.ReadLine();

Alternative answer

Console.Write("Enter value for n: ");


int number = Convert.ToInt32(Console.ReadLine());
int numbersFoundSoFar = 0;
int currentNumber = 1;
int sumOfDigits;
while ((numbersFoundSoFar != number))
{
int temp = currentNumber;
sumOfDigits = 0;
while (temp > 0)
{
sumOfDigits +=temp % 10;
temp = temp / 10;
}
if (currentNumber % sumOfDigits == 0)
{
numbersFoundSoFar++;
if (numbersFoundSoFar == number)
{
Console.WriteLine(currentNumber);
}
}
currentNumber++;
}
Console.ReadLine();

Recursive answer

static int SumDigits(int num)


{
int sum;
if (num == 0)
{
sum = 0;
}
else
{
sum = num % 10 + SumDigits(num / 10);
}
return sum;
}

static void Main(string[] args)


{
Console.Write("Enter value for n: ");
int n = Convert.ToInt32(Console.ReadLine());
int nthHarshad = 0;
int counter = 1;
int value;
while ((n != nthHarshad))
{
value = SumDigits(counter);

Page 17 of 21
if ((counter % value) == 0)
{
nthHarshad++;
}
if ((n == nthHarshad))
{
Console.WriteLine(counter);
}
counter = (counter + 1);
}
Console.ReadLine();
}

PASCAL/Delphi

var
Number : integer;
NumbersFoundSoFar : integer;
CurrentNumber : integer;
SumOfDigits : integer;
NumAsString : string;
Count : integer;
begin
write('Enter value for n: ');
readln(Number);
NumbersFoundSoFar := 0;
CurrentNumber := 1;
while (NumbersFoundSoFar <> Number) do
begin
SumOfDigits := 0;
NumAsString := inttostr(CurrentNumber);
for Count := 1 to length(NumAsString) do
SumOfDigits := SumOfDigits + strtoint(NumAsString[Count]);
if CurrentNumber mod SumOfDigits = 0 then
begin
inc(NumbersFoundSoFar);
if NumbersFoundSoFar = Number then
writeln(CurrentNumber);
end;
inc(CurrentNumber);
end;
readln;
end.

Alternative answer

var
Number : integer;
NumbersFoundSoFar : integer;
CurrentNumber : integer;
SumOfDigits : integer;
Temp : integer;
begin
write('Enter value for n: ');
readln(Number);
NumbersFoundSoFar := 0;
CurrentNumber := 1;
while NumbersFoundSoFar <> Number do
begin
Temp := CurrentNumber;
SumOfDigits := 0;
while Temp > 0 do
begin

Page 18 of 21
SumOfDigits := SumOfDigits + (Temp mod 10);
Temp := Temp div 10;
end;
if CurrentNumber mod SumOfDigits = 0 then
begin
inc(NumbersFoundSoFar);
if NumbersFoundSoFar = Number then
writeln(inttostr(CurrentNumber));
end;
inc(CurrentNumber);
end;
readln;
end.

Recursive answer

function SumDigits(Num : integer) : integer;


var
Sum : integer;
begin
if Num = 0 then
Sum := 0
else
Sum := (Num mod 10) + SumDigits(Num div 10);
SumDigits := Sum;
end;

var
N, NthHarshad, Counter, Value : integer;
begin
write('Enter value for n: ');
readln(N);
NthHarshad := 0;
Counter := 1;
while (N <> NthHarshad) do
begin
Value := SumDigits(Counter);
if Counter mod Value = 0 then
inc(NthHarshad);
if N = NthHarshad then
writeln(inttostr(Counter));
inc(Counter);
end;
readln;
end.

JAVA

Console.write("Enter value for n: ");


int number = Integer.parseInt(Console.readLine());
int numbersFoundSoFar = 0;
int currentNumber = 1;
int sumOfDigits;
while (numbersFoundSoFar != number) {
int temp = currentNumber;
sumOfDigits = 0;
String numAsString = currentNumber + "";
for (int count = 0; count < numAsString.length(); count++) {
sumOfDigits +=
Integer.parseInt(numAsString.charAt(count)+"");
}
if (currentNumber % sumOfDigits == 0) {
numbersFoundSoFar++;

Page 19 of 21
if (numbersFoundSoFar == number) {
Console.writeLine(currentNumber);
}
}
currentNumber++;
}

Alternative answer

Console.write("Enter value for n: ");


int number = Integer.parseInt(Console.readLine());
int numbersFoundSoFar = 0;
int currentNumber = 1;
int sumOfDigits;
while (numbersFoundSoFar != number) {
int temp = currentNumber;
sumOfDigits = 0;
while (temp > 0) {
sumOfDigits += temp % 10;
temp /= 10;
}
if (currentNumber % sumOfDigits == 0) {
numbersFoundSoFar++;
if (numbersFoundSoFar == number) {
Console.writeLine(currentNumber);
}
}
currentNumber++;
}

Recursive answer

static int sumDigits(int num) {


int sum;
if (num == 0) {
sum = 0;
} else {
sum = num % 10 + sumDigits(num/10);
}
return sum;
}

public static void main(String[] args) {

Console.write("Enter value for n: ");


int number = Integer.parseInt(Console.readLine());
int nthHarshad = 0;
int counter = 1;
int value;
while (number != nthHarshad) {
value = sumDigits(counter);
if (counter % value == 0) {
nthHarshad += 1;
}
if (number == nthHarshad) {
Console.writeLine(counter);
}
counter = counter + 1;
}
}

(b) Mark is for AO3 (evaluate)

Page 20 of 21
**** SCREEN CAPTURE ****
Must match code from (a), including prompts on screen capture
matching those in code.
Code for (a) must be sensible.

Screen capture showing the number 600 being entered and then a
message displayed saying 3102

1
[13]

Page 21 of 21

You might also like