Programming Examples and Source Code

Source code section features a diverse collection of programming examples and source code snippets in C and C++. The examples includes practical implementations, tutorials, and demonstrations aimed at helping programmers enhance to enhance their programming skills.

The best way to learn any programming language is by practicing examples. This source code section features a diverse collection of programming examples and source code snippets written C, C++ and other programming languages.

The examples includes practical implementations, tutorials, and demonstrations aimed at helping programmers enhance to enhance their programming skills.

Table of Contents

C Programming Source Code

  • Solving the Knapsack Problem with Code Examples
    The Knapsack Problem is a classic optimization problem in computer science and mathematics. The goal is to maximize the value of items placed in a knapsack without exceeding its weight capacity. This problem has many variations, but the most common are: 0/1 Knapsack Problem: Each item can either be included or excluded. Fractional Knapsack Problem: Items can be divided to maximize value.
  • LRU and FIFO L1 Cache Implementation using C
    This is a C program to demonstrate cache mechanism by simulating a cache in C. The source code can run in any C Compiler with minor modifications if required. It can run on real memory traces as input to your cache simulator. We have implemented two cache replacement policies i.e. least recently used (LRU) and First-in first-out (FIFO) replacement policies.
  • Random Number Generation in C/C++: Tricks for Generating, Seeding, and Working with Random Numbers
    C Standard Library provides two different methods to generate random numbers. They are: rand() and srand().
  • C Algorithms Library
    This article is about a collection of common Computer Science algorithms which may be used in C projects. The C Programming Language has a much smaller Standard Library as compared to other more modern programming languages such as Java or Python. The library provides a basic set of mathematical functions, string manipulation, type conversions, and file and console-based I/O.

C++ Programming Source Code

  • Rock, Paper, Scissors Game in C++ [C++ Game]
    This is a simple C++ program to demonstrate rock, paper, scissors game. In this game, players have to choose one of the three options to beat the opponent or the computer itself. The C++ code generates a random choice by creating a character to indicate that option. Options are: r is for ‘rock’, p is for ‘paper’, and s is for ‘scissors’.
  • C++ Code – Wave File Converter from Text File
    This C++ program is capable of turning a text file describing the notes of a piece of music into an audio file (.wav). The program reads the contents of the input file i.e. input.txt, constructs Song, Voice and Note objects… Read more: C++ Code – Wave File Converter from Text File
  • Vigenere Encryption and Decryption in C++
    The Vigenere cipher is a method of encrypting or decrypting alphabetic text by using a series of interwoven Caesar ciphers (Caesar Shift), based on the letters of a keyword. In cryptography, Caesar shift is one of the simplest known encryption techniques. It employs a form of polyalphabetic substitution, which is using multiple substitution alphabets.
  • C++ Program to Solve the Quadratic Equation
    This program will solve quadratic equations. It accepts coefficients of a quadratic equation from the user i.e. a, b and c and displays the roots. To compile the program name it quadratic_solver.cpp then type g++ -o quadratic_solver quadratic_solver.cpp You may need to use math.h like this: #include if you are using windows C++ programming software. (I tried it without the math.h and got an “undeclared identifier” error)

JavaScript Source Code

  • Difference Between [] and {}: Arrays vs. Objects in JavaScript
    In JavaScript, we use braces {} for creating an empty object and brackets [] for creating an empty array. I find the Developer console in Mozilla Firefox or Google Chrome more convenient to print out any object and study the details.
  • JavaScript Program to Show Current Time In Various Formats
    This is a small script that can display the current time in “Military Time” format or “12 Hour Time” format with the push of a button. It shows two radio buttons to change the time format and a text box to display the time.
  • JavaScript Program to Show Date and Timestamp
    This is a simple JavaScript Program to show Date and Time Stamp on a webpage. In this script  we define a JavaScript function named getFormattedDateTime. This function uses the Date object to get the current date and time. It then formats them into a human-readable string.
  • JavaScript Code to show Browser Information
    This JavaScript code does a great job of displaying information about your web browser. Information includes Browser Name, Version, your computer’s OS platform, and whether Java is enabled.

PHP Code Snippets

  • How to Send Email using PHP
    A detailed guide on PHP mail sending options with examples of using the built-in mail function(), PHPMailer, Symfony, and third-party mail service providers such as Amazon SES, SendGrid and GetResponse.
  • Search the LDAP Directory in PHP
    This PHP code connects to an LDAP server, performs a search for entries with the last name and then sorts and prints them alphabetically. The sorting is done by a custom function called compareEntry, which compares entries based on last names and first names. It uses the LDAP functions to communicate with the server and showcases basic error handling.

Java Source Code

Scroll to Top