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

Number Problem Pseudocode

The document outlines pseudocode and flowcharts for various programming tasks, including checking factors, counting digits, identifying Buzz and Duck numbers, validating Dudeney numbers, and ATM PIN validation. Each task is broken down into clear steps with input prompts, validation checks, and output statements. Additionally, it includes a correction for an existing flowchart related to processing cents.

Uploaded by

Vaishali Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Number Problem Pseudocode

The document outlines pseudocode and flowcharts for various programming tasks, including checking factors, counting digits, identifying Buzz and Duck numbers, validating Dudeney numbers, and ATM PIN validation. Each task is broken down into clear steps with input prompts, validation checks, and output statements. Additionally, it includes a correction for an existing flowchart related to processing cents.

Uploaded by

Vaishali Mishra
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Draw a flowchart and write a pseudocode for the following questions:

1. Ask for two numbers from the user. Check if second is a factor of the first. Display appropriate
output. Use validation to make sure that the second number is not greater than the first
number.

// Step 1: Ask the user for two numbers

PRINT "Enter the first number:"

INPUT first_number

PRINT "Enter the second number:"

INPUT second_number

// Step 2: Validate that the second number is not greater than the first

IF second_number > first_number THEN

PRINT "The second number cannot be greater than the first number. Please try again."

EXIT

// Step 3: Check if the second number is a factor of the first

IF first_number MOD second_number = 0 THEN

PRINT second_number + " is a factor of " + first_number

ELSE

PRINT second_number + " is NOT a factor of " + first_number

END IF
2. Write a program to input a number and count the number of digits. The program further checks
whether the number contains odd number of digits or even number of digits.

Sample Input: 749


Sample Output: Number of digits=3
The number contains odd number of digits.

// Step 1: Ask the user for a number


PRINT "Enter a number:"
INPUT number

// Step 2: Initialize the count of digits


SET digit_count = 0

// Step 3: Count the number of digits in the number


WHILE number > 0
// Remove the last digit
number = number DIV 10
// Increment the digit count
digit_count = digit_count + 1
END WHILE

// Step 4: Display the number of digits


PRINT "The number has " + digit_count + " digits."

// Step 5: Check if the number of digits is odd or even


IF digit_count MOD 2 = 0 THEN
PRINT "The number has an even number of digits."
ELSE
PRINT "The number has an odd number of digits."
END IF
3. Write a pseudocode to display all the 'Buzz Numbers' between p and q (where p<q). A
'Buzz Number' is the number which ends with 7 OR is divisible by 7.

Examples of Buzz numbers: 47, 49, 63, 427

BEGIN

// Step 1: Ask the user for the values of p and q

PRINT "Enter the value of p:"

INPUT p

PRINT "Enter the value of q:"

INPUT q

// Step 2: Validate that p is less than q

IF p >= q THEN

PRINT "Error: p must be less than q."

EXIT

// Step 3: Iterate over all numbers from p to q

FOR number = p TO q DO

// Step 4: Check if the number is a Buzz Number

IF number MOD 10 = 7 OR number MOD 7 = 0 THEN

PRINT number + " is a Buzz Number"

END IF

END FOR

END
4. A number is said to be Duck if the digit zero is (0) present in it. Write a pseudocode to accept
a number and check whether the number is Duck or not. The program displays the message
accordingly. (The number must not begin with zero)

Sample Input: 5063


Sample Output: It is a Duck number.
Sample Input: 7453
Sample Output: It is not a Duck number.

BEGIN
// Step 1: Ask the user for a number
PRINT "Enter a number:"
INPUT number

// Step 2: Validate that the number does not begin with zero
IF number STARTS WITH "0" THEN
PRINT "Error: The number should not begin with zero."
EXIT

// Step 3: Check if the number contains zero


SET is_duck = FALSE
FOR EACH digit IN number DO
IF digit = "0" THEN
SET is_duck = TRUE
BREAK
END IF
END FOR

// Step 4: Display the result


IF is_duck THEN
PRINT "The number is a Duck number."
ELSE
PRINT "The number is NOT a Duck number."
END IF
END

5. A Dudeney number is a positive integer that is a perfect cube such that the sum of its digits is equal
to the cube root of the number. Write a program to input a number and check and print whether it
is a Dudeney number or not.

Example:
Consider the number 512.
Sum of digits = 5 + 1 + 2 = 8
Cube root of 512 = 8
As Sum of digits = Cube root of Number hence 512 is a Dudeney number.
BEGIN
// Step 1: Ask the user for a number
PRINT "Enter a number:"
INPUT number

// Step 2: Check if the number is a perfect cube


SET cube_root = CUBE ROOT of number
IF cube_root is not an integer THEN
PRINT "The number is NOT a perfect cube."
EXIT

// Step 3: Calculate the sum of digits of the number


SET sum_of_digits = 0
SET temp_number = number

WHILE temp_number > 0 DO


SET digit = temp_number MOD 10
sum_of_digits = sum_of_digits + digit
temp_number = temp_number DIV 10
END WHILE

// Step 4: Check if the sum of digits is equal to the cube root of the number
IF sum_of_digits = cube_root THEN
PRINT "The number is a Dudeney number."
ELSE
PRINT "The number is NOT a Dudeney number."
END IF
END

6. Write a pseudocode for an ATM Machine. Following are the rules laid out by the bank:
a. PIN should be a 4 digit number positive number (first digit non 0)
b. PIN cannot be a palindrome.
Your pseudocode should validate the input and display appropriate output.

Examples of invalid PIN: 312, 0452, 1221, 7887


Examples of valid PIN: 4142, 5123, 1234, 5567 etc.
Read and try to understand what the following Pseudocode is trying to accomplish?

INPUT CENTS
// input 36

WHILE CENTS > 99 them


OUTPUT “ Enter a valid number of cents: “
INPUT CENTS
ENDWHILE

QUARTERS  DIV(CENTS, 25) //1


REMAINING  MOD(CENTS, 25) //11
OUTPUT “Quarters: “, QUARTERS

DIMES  DIV(REMAINING, 10) //1


REMAINING  MOD(REMAINING, 10) //1
OUTPUT “Dimes”, DIMES

NICKELS  REMAINING div 5 //0 REMAINING


 REMAINING mod 5 //1
output(‘Nickels’, NICKELS)

PENNIES  DIV(REMAINING ,1) //1


REMAINING  MOD(CENTS, 1) //0
OUTPUT “Pennies: “, PENNIES
Below flowchart is incorrect and has few mistakes, draw the corrected flowchart

+ +
| Start |
+ +
|
v
+ +
| Input CENTS |
+ +
|
v
+ +
| CENTS > 99 ? |
+ +
/ \
yes no
/ \
v v
+ + + +
| Output "Enter a valid number of | | QUARTERS ← CENTS DIV 25 |
| cents:" | +------------------------------+
+ + |
| v
| + +
| | REMAINING ← CENTS MOD 25 |
| + +
| |
+ v
| + +
| | Output "Quarters: ", QUARTERS |
| +------------------------------+
| |
v v
+ + + +
| Input CENTS | | DIMES ← REMAINING DIV 10 |
+ + + +
| |
v v
+ + + +
| REMAINING ← REMAINING MOD 10 | + Output "Dimes: ", DIMES |
+ + + +
| |
v v
+ + + +
| NICKELS ← REMAINING DIV 5 | | REMAINING ← REMAINING MOD 5 |
+ + + +
| |
v v
+ + + +
| Output "Nickels: ", NICKELS | | PENNIES ← REMAINING DIV 1 |
+ + + +
| |
v v
+ + + +
| REMAINING ← REMAINING MOD 1 | + Output "Pennies: ", PENNIES |
+ + + +
|
v
+ +
| End |
+ +

You might also like