We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,692 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

pseudocode

hi I am new to programming, I will like to know if my code are ok:

CALCULATE THE FACTORIAL OF A NUMBER PROVIDED BY THE USER.

  1. Data Counter as a Wholenumber
  2. Data Number as a Wholenumber
  3.   Data Factorial as a Wholenumber
    
  4.    Output  “Enter Number”
    
  5.    Input    Number
    
  6.    Counter =Number
    
  7.     Factorial=Number;
    
  8.      Loop until counter is equal to 1
    
  9.                Counter= Counter-1
    
  10. Factorial= Factorial * Counter
  11. Next Loop
  12. Output Factorial

LINE Number Factorial Counter Notes
1 0 0 0
2 0 0 0
3 0 0 0
4 0 0 0 Output
5 5 0 0 User Enters 5
6 5 0 5
7 5 5 5
8 5 5 5 Loop until counter is equal to 1
9 5 5 4
10 5 20 4
11 5 20 4 Back to Line 8
8 5 20 4
9 5 20 3
10 5 60 3
11 5 60 3 Back to Line 8
8 5 60 3
9 5 60 2
10 5 120 2
11 5 120 2 Back to Line 8
8 5 120 2
9 5 120 1
12 5 120 1 Output

2
Contributors
1
Reply
40 Minutes
Discussion Span
9 Months Ago
Last Updated
2
Views
pascallavante
Newbie Poster
1 post since Jul 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

The algorithm is correct, though two things stand out:

  1. You don't account for a number that's less than 1. In real code this can cause problems.

  2. The loop stops when counter is equal to 1, but due to the way the factorial and counter interact, you could stop the loop at 2 and still get a correct result. For this program it's not a big deal, but in complex loops you can benefit from not doing unnecessary work.

There's also the matter of factorial growth and potentially overflowing the number data type, but that's not really a concern for pseudocode. However, it couldn't hurt to recognize that when translated to real code, you have to deal with the limitations of the computer.

deceptikon
Challenge Accepted
Administrator
3,427 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0584 seconds using 2.67MB