0% found this document useful (0 votes)
3 views4 pages

Numeric Algorithms Assignment

Uploaded by

taha.bilal20010
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)
3 views4 pages

Numeric Algorithms Assignment

Uploaded by

taha.bilal20010
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

Assignment: Numeric Algorithms

Q1. Arithmetic Progression (AP)


An Arithmetic Progression (AP) is a sequence of numbers in which each term after the first
is obtained by adding a fixed number (called the common difference) to the previous term.

If:
- First term = a
- Common difference = d
- Number of terms = n

Then the terms of the AP are:


a, a+d, a+2d, a+3d, … , a+(n-1)d

The formula for the nth term is:


Tn = a + (n-1)d

Example:
If a = 3, d = 4, and n = 5, the sequence is:
3, 7, 11, 15, 19

Task:
- Inputs the first term, the common difference, and the number of terms (N).
- Generates and outputs the sequence of N terms in the arithmetic progression.

Q2. Sum of Digits and Digital Root


When working with numbers, we can extract the digits of an integer using the following
operations:
- Number MOD 10 → gives the last digit.
- Number DIV 10 → removes the last digit.

The digital root of a number is the single-digit value obtained by repeatedly summing its
digits until only one digit remains.

Example:
987 → 9+8+7=24 → 2+4=6
So, the digital root of 987 is 6.

Task:
- Inputs a positive integer.
- Uses MOD and DIV to calculate the sum of its digits.
- Repeats the process until a single-digit result is obtained.
- Outputs the final digital root.
Q3. Decimal to Binary Conversion
The binary representation of a number can be obtained using:
- Number MOD 2 → gives the remainder (0 or 1).
- Number DIV 2 → reduces the number until it becomes 0.

Example: To convert 13 into binary:


13 MOD 2 = 1, 13 DIV 2 = 6
6 MOD 2 = 0, 6 DIV 2 = 3
3 MOD 2 = 1, 3 DIV 2 = 1
1 MOD 2 = 1, 1 DIV 2 = 0

Reading remainders backwards gives: 1101.

Task:
- Inputs a positive integer in decimal form.
- Uses MOD and DIV repeatedly to generate its binary equivalent.
- Outputs the binary number.

Q5. Lucky Number (Even and Odd Digits)


A number can be considered lucky if it contains more even digits than odd digits.
We can identify each digit by:
- Number MOD 10 → gives the last digit.
- Number DIV 10 → removes the last digit.

Example:
If the number is 4827:
Digits: 4 (even), 8 (even), 2 (even), 7 (odd)
There are 3 even digits and 1 odd digit → so the number is lucky.

Task:
- Inputs a positive integer.
- Uses MOD and DIV to count how many digits are even and how many are odd.
- Outputs whether the number is lucky (more even digits than odd digits) or not.
Q3. Triangular Numbers – Nth Term
A Triangular Number is the sum of the first N natural numbers. The sequence starts as:
1, 3, 6, 10, 15, 21, …

These numbers are formed by adding each new natural number to the previous total:
• 1st term = 1
• 2nd term = 1 + 2 = 3
• 3rd term = 3 + 3 = 6
• 4th term = 6 + 4 = 10
• 5th term = 10 + 5 = 15

Task:
Write an algorithm that:
1. Asks the user to enter a number N.
2. Uses a loop to calculate the Nth Triangular Number by adding step by step.
3. Outputs the Nth Triangular Number.

Example Run:
If the user enters 5, the program should output 15.
Section B: Random Number Generator

QX. Lucky 7 Game


The computer generates random numbers between 1 and 10 until the number 7 appears.

Task:
- Repeatedly generates random numbers.
- Counts how many numbers are generated before a 7 appears.
- Outputs the count.

QY. Jackpot Triple Match


Slot machines display three digits at once.

Task:
- Generates three random digits between 0 and 9.
- If all three digits are the same, output 'Jackpot!'.
- Otherwise, output the three numbers and 'Try again.'

QZ. Race to 100


Two players take turns rolling a dice (numbers between 1 and 6).

Task:
- Keeps a running total score for each player.
- Continues rolling until one player’s score reaches 100 or more.
- Outputs the winner and both players’ final scores.

You might also like