Write a Program to create a loading bar

Posted in

Write a Program to create a loading bar

Vinay Khatri
Last updated on September 14, 2022

    Problem Statement

    We need to write a program in C, C++, and Python that creates a loading bar on the console window. All these three programming languages support third-party packages or libraries to print a loading progress bar, but we will use the basic coding to implement one of our own.

    Algorithm

    The idea behind creating a process and loading bar is quite straightforward. We will create a loop that will print a specific character (ASCII code 219) again and again, but we will put a time delay between printing the character so that it could give a loading or processing effect.

    C Program to create a loading Bar

    #include <stdio.h>
    #include <windows.h>
    
    int main()
    {	int process;
    	
    	//ascii code to print the loading bar
    	int ascii_code = 219;
    
    	printf("\t\t\t\t***************Loading Please Wait**********\n\t\t\t");
    		
    	for( process=0; process<=60; process++)
    	{
    		//add delay to execute the next statement
    		Sleep(20);
    		
    		// print the equivalent chracter for ascii code 219
    		printf("%c", ascii_code);
    	}
    	
    	printf("\n \t\t\t\t\t\Loading Completed!");
    
        return 0;
    }

    Output

                                    ***************Loading Please Wait**********
                            ?????????????????????????????????????????????????????????????
                                                    Loding Completed!

    C++ Program to create a loading Bar

    #include<iostream>
    #include<windows.h>
    using namespace std;
    
    int main()
    {
    	int process;
    	
    	//ascii code to print the loading bar
    	int ascii_code = 219;
    	char ch;
    	
    	cout<<"\t\t\t\t***************Loading Please Wait**********\n\t\t\t";
    		
    	for( process=0; process<=60; process++)
    	{
    		//add delay to execute the next statement
    		Sleep(20);
    		
    		// print the equivalent chracter for ascii code 219
    		ch = ascii_code;
    		cout<<ch;
    	}
    	
    	cout<<"\n \t\t\t\t\t\tLoading Completed!";
    
    
        return 0;
    }

    Output

                                    ***************Loading Please Wait**********
                            ?????????????????????????????????????????????????????????????
                                                    Loding Completed!

    Python Program to create a loading Bar

    import time
    #ascii code for blocks
    ascii_code = 9608
    # convert the ascii code into equivalent chracter
    ch = chr(ascii_code)
    
    print("\t\t\t\t***************Loading Please Wait**********\n\t\t\t", end ="")
    
    for process in range(61):
        time.sleep(0.0)
        print(ch, end ="")
    print("\n\t\t\t\t\t\tLoading Completed!")

    Output

    				***************Loading Please Wait**********
    			?????????????????????????????????????????????????????????????
    						Loading Completed!

    Wrapping Up!

    In the above program, we just print the character of ASCII code 219 (C, C++) and 9680(python) as process blocks for the loading bar. To give it the effect of the loading bar, we have put a time delay between two print statements. In your upcoming programming interviews, you may be asked to print a loading bar with the programming language you are comfortable with, so learn the logic and try the above programs for yourself.

    People are also reading:

    Leave a Comment on this Post

    0 Comments