top

Search

C Tutorial

.

UpGrad

C Tutorial

Pseudo-Code In C

Overview

To ensure error-free output in C language, it is inevitable to follow syntax during the program’s execution. Occasionally, it may seem challenging to understand a complex program’s logic. You can write the program in pseudocode first to better understand the program logic before implementing it. The following tutorial discusses more on Pseudo-Code In C.

What Is Pseudo-Code in C?

The Pseudo-Code In C refers to an informal way of writing a program to improve human understanding. The purpose of writing a pseudocode is to facilitate easy comprehension of the program's logic by both programmers and non-programmers.

In other words, it is a false code that even a layman (with some basic programming language) can understand easily.

To make it easier to understand the complex program, it is written in simple English, using informative text and annotations.

Pseudocode does not adhere to the syntax of the C programming language and cannot be directly interpreted or compiled.

Writing a concise, clear, and straightforward pseudocode can greatly aid in the transition from ideation to the implementation of the program. 

Here’s the source code example:

#include <stdio.h>

int main() {
    int num = 5;
    
    for (int i = 0; i < num; i++) {
        printf("%d\n", num);
    }
    
    return 0;
}

 For better understanding, You can transform the source code into the following pseudo-code.

Assign the value 5 to the variable num.
For each value of i from 0 to 4:
Display the value of num.

How to write a Pseudo Code?

Since humans read the pseudocode and not the computer, everybody has their unique style of presenting the program’s logic. The pseudocode’s rules are less stringent than that of a programming language. But you can follow certain simple rules as discussed below to effectively write a Pseudo-Code In C.

  • Organise the sequence of tasks and accordingly write its pseudocode.

  • Begin the pseudocode by writing a statement that defines the program’s goal. 

  • Use simple, distinct, and proper naming conventions for easy understanding.

  • Explain each step that will happen in the actual source code. Make sure your pseudocode is not abstract.

  • Make sure to use standard programming structures like ‘for’,  ‘if-then’, ‘while’, ‘cases’, etc., in the manner we use them in programming.

  • Ensuring that all the sections of a pseudo code are complete and easy to understand.

  • Don’t include ample technical terms otherwise, non-programmers or clients will find it difficult to understand.

  • Writing only one statement per line for readability.

  • Always terminate multi-line sections using an END keyword (like ENDWHILE, ENDIF, etc.).

  • Always capitalise the initial word of your pseudocode statement.

  • Indent the statements according to the manner, the if-else, while, for loops are indented in a particular program. This approach helps you to easily understand the execution mechanism and decision control. Also, it significantly improves the code’s readability. Here’s an example: 

input = read user input
if input is equal to 1 then
    display "I am case 1"
else if input is equal to 2 then
    display "I am case 2"

Pseudocode examples

The following pseudocode examples help you better understand how to write the one for your program.

Example-1:

If a student's marks are greater than or equal to 40
    Print "passed"
else
    Print "failed"
end if

As the above example shows, the first step checks whether the student’s mark is more than or equal to 40. If yes, then the output prints “passed” else, it prints “failed”.

Example-2:

Enter a number (num)
IF num % 2 == 0
    print "The number is even"
ELSE
    print "The number is odd"

As the above example shows, the first step asks the user to input a number. The next step performs a modulus operation on it. If the mod 2 value is 0, it prints that the number is even, else it prints that the number is odd.

Pseudocode in C compiler (examples)

Example-1: 

Here’s the algorithm for a pseudocode in the C compiler that calculates the factorial of the input number. 

Step 1: Start

Step 2: Initialise f= 1

Step 3: Accept input from the user (n)

Step 4: for i=1 to i <= n iterate the process

Step 5: f = f * i

Step 6: i++ [increment i by one]

Step 7: print the value of f

Step 8: Stop 

Check out the pseudo-code for the given algorithm

Start program
Declare variables f and n
Accept input for n from the user
Initialize f as 1
For i = 1 to i <= n
    Perform f = f * i
Display the value of f
End program

Based on the above algorithm and pseudo-code, here is a C program to calculate the factorial of the input number. 

#include <stdio.h>
void main()
{
int n, f=1,i;
printf("Please enter a number: ");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
f=f*i;
}
printf("\n The factorial of %d is %d", n, f);
}

Output:

Please enter a number: 4
The factorial of 4 is 24

The value of f is multiplied by i, and the value of i is incremented by 1. The process iterates until i<=n.  Once this condition is not satisfied, the loop ends and the value of f is displayed at the output. 

Example-2:

Here’s the pseudo-code for checking whether the number is even or odd.

// The following pseudocode program checks if a number is even or odd
// Declare variable
number: Integer
// Prompts the user to enter a number
Display "Enter a number: "
Read number
// Checks if the number is divisible by 2
if the number modulo 2 equals 0 then
// The number is even
Display "The input number is even"
else
// The number is odd
Display "The input number is odd"
endif 

Based on the above pseudo-code, here is a C program to check whether the input number is odd or even.

To implement this pseudocode in C, you can use the following code:

#include <stdio.h>
int main() {
// Declare variable
int number;

// Prompt the user to enter a number
printf("Enter a number: ");
scanf("%d", &number);

// Check if the number is divisible by 2
if (number % 2 == 0) {
    // The number is even
    printf("The number is even\n");
} else {
    // The number is odd
    printf("The number is odd\n");
}

return 0;
}

Output:

Enter a number: 45
The number is odd

This C program will prompt the user to enter a number, and then it checks whether the number is even or odd using the modulus operator. Finally, it displays the appropriate message indicating whether the number is even or odd.

Advantages and Disadvantages of Pseudo-Code in C 

Advantages:

  • Pseudo-Code In C facilitates a quick and comprehensive understanding of even intricate programs.

  • Pseudo-Code In C does not adhere to the strict syntax of the C programming language/

  • You can easily modify a pseudocode.

  • It enhances your program’s readability by first writing the program’s algorithm.

  • It works as a bridge between the program and the flowchart or algorithm.

  • It acts as a rough documentation. Hence, non-programmers, clients, and any laymen can easily understand its purpose. Documentation is essential in industries, and this is where pseudocode proves useful.

  • Its key goal is to illustrate what each line of the program must do. Thus, it simplifies coding for the programmer.

  • Developing a habit of writing pseudocode for all your programs will ensure you get all the steps and details. Hence, you can gradually practise writing error-free programs. 

Disadvantages:

  • Since pseudo-code cannot be compiled or interpreted, unlike the c programs. Hence, you can’t identify the errors.

  • The flexibility of writing pseudo-code in any order can make it challenging to grasp the flow of a program.

  • It can be time-consuming to generate a pseudocode, especially when your program is lengthy.

  • The flexibility does make it easier to understand and convey the program’s logic. However, translating pseudocodes into actual C code can lead to inconsistencies and ambiguities.

  • Developers may decode the pseudo-code differently, leading to implementation variations.

  • It lacks the error reporting mechanisms and debugging features available in C.

  • The lack of error handling and syntax in pseudo-code can make it harder to detect and fix issues in the implementation

Conclusion

Converting programming code into pseudocode helps the program to effectively share the implemented logic with other programmers as well as non-programmers. Consequently, they can obtain reviews and suggestions or infer changes to be implemented in the intended program. Writing a pseudocode before implementing the actual code is always a good practice.

Tutorials certainly assist you in strengthening your C fundamentals, including the pseudocode examples for beginners. However, you should frequently practise and implement these concepts to realise their usefulness in practical scenarios.

In addition to going through the tutorials, you can pursue upGrad’s Master of Science in Computer Science from Liverpool John Moores University to enhance your career advancement in the tech industry. The best-in-class content delivered by this program's industry experts ensures you reinforce your software engineering fundamentals. Since the program encompasses essential and in-demanding topics, it facilitates your career advancement in the growing field of STEM!

Enroll now to begin your journey! 

FAQs

Q. What are the differences between algorithm and pseudocode?

An algorithm is a methodical procedure created to solve a problem, whereas a pseudocode is a technique of creating an algorithm before the actual implementation of the program. An algorithm is a formal representation that follows the rules of a specific programming language. On the other hand, a pseudocode is an informal representation of an algorithm that doesn’t follow the syntax of the programming language.

Q. How does pseudocode help you quickly detect a bug in a C program?

The pseudocode is written in a human-readable format. So it is easier to modify and identify bugs before actually implementing the code. You can edit pseudocode more efficiently than testing and debugging the actual code.

Q. Is pseudocode a false code?

Yes, pseudocode is fake code that simply imitates the real code. It uses simple English statements to illustrate a program’s purpose. It is fake because no compiler can translate the pseudo code into a machine language. 

Q. Does pseudocode follow the rules?

The pseudocode is an informal representation of a program, so it doesn’t follow any rules. You can set out your own rules to write an easy-to-understand pseudocode in C.

Leave a Reply

Your email address will not be published. Required fields are marked *