A Pseudo Code is an informal way of writing the steps that should be followed for writing a program for human understanding. It should be just written in the English language to make the complex program easy to understand.
For graphical representation, we use the alternative of pseudo-code which is Flow Charts.
An algorithm for the factorial of a given number:
- Start
- Initialize fact = 1
- Input the value of n from the user
- For i = 0 to i <= n repeat the process
- fact = fact * i
- i++ (increment i by one)
- print the fact value
- stop
Now its pseudo code will be:
- Start the program
- Declare variables fact and n
- Enter the number for n
- for i=1 to i <=n
- Perform fact = fact * i
- Output fact
- End the program
By using the above pseudo-code, creating a program for the factorial of a given number using for loop:
#include <stdio.h>
void main() {
int n, fact=1,i;
printf("enter value for n");
scanf("%d",&n);
for(i=1; i<=n; i++) {
fact=fact*i;
}
printf("\n factorial of %d is %d", n, fact);
}