- C Programs
- C Programs - Home
Basics of C Programming (Introductory Programs)
- Hello World
- Print your name, age, and address
- Display "C Programming is Powerful"
- Input and display integer, float, and character
- Swap two numbers (using third variable)
- Swap two numbers (without third variable)
- Find ASCII value of a character
- Find size of data types (int, float, etc.)
- Convert Celsius to Fahrenheit
- Convert Fahrenheit to Celsius
- Convert kilometers to miles
- Area and perimeter of a rectangle
- Area and circumference of a circle
- Menu-Driven Shape Calculations
- Menu-Driven Geometry Calculations
- Menu-Driven Extended Geometry Calculations
- Complete 2D + 3D Geometry Calculator
- Volume of a Cube
- Volume of a Cuboid
- Volume of a Sphere
- Volume of a Cylinder
- Volume of a Cone
- Surface Area of a Cube
- Surface Area of a Cuboid
- Surface Area of a Sphere
- Surface Area of a Cylinder
- Surface Area of a Cone
- Surface Area of a Hemisphere
- Financial Programs in C
- Simple interest calculation
- Compound interest calculation
- Simple Interest and Compound Interest Difference Calculation
- Simple Interest vs Compound Interest Year wise Table Calculation
- Graphically Compare of Simple Interest vs Compound Interest using ASCII bars Program
- Menu Driven Simple Interest and Compound Interest Program
- Mini Banking System with Simple Interest and Compound Interest Program
- EMI Calculator Program
- Menu Driven Loan Calculator
- Loan Amortization Table Program
- Total Payment and Total Interest Paid on a Loan Program
- EMI Comparison with Different Tenures and Rates Program
- Calculate gross salary
- Convert days to years, weeks, days
- Evaluate arithmetic expression
- Display ASCII table
Control Statements & Loops in C
- Decision Making Programs in C
- Check whether a number is positive, negative, or zero
- Check whether a number is even or odd
- Check whether a number is Positive, Negative, or Zero and Even or Odd
- Menu-Driven Number Check of Positive, Negative, or Zero and Even or Odd
- Find the largest of two numbers
- Find the largest of three numbers
- Check whether a year is a leap year
- Check whether a character is a vowel or consonant
- Check whether a character is uppercase, lowercase, digit, or special symbol
- Find the grade of a student (marks-based)
- Check whether a person is eligible to vote
- Find absolute value of a number
- Simple calculator using if-else
- Simple calculator using switch-case
- Print day of week (switch-case)
- Check whether triangle is valid (angles sum = 180)
- Check whether triangle is equilateral, isosceles, or scalene
- Find roots of quadratic equation
- Electricity bill calculation (slab-based)
- Calculate bonus for employee based on salary & service years
- Check whether a character is alphabet or not
- Check whether a character is a digit or special character
- Check whether a number is divisible by 5 and 11
- Menu-driven program: area of circle, rectangle, triangle
- Find greatest among four numbers (nested if)
- Find greatest among four numbers (ternary operators)
- Loop Programs in C
- Print first N natural numbers
- Print Natural Numbers from N down to 1 (Reverse Order)
- Print first N even numbers
- Print first N odd numbers
- Print multiplication table of a number
- Print sum of first N natural numbers
- Print sum of first N even numbers
- Print sum of first N odd numbers
- Print factorial of a number (loop)
- Generate Fibonacci series up to N terms
- Print reverse of a number
- Print reverse of a Negative number
- Count digits in a number
- Sum of digits of a number
- Product of digits of a number
- Check whether a number is palindrome (loop)
- Check whether a number is Armstrong (loop)
- Check whether a number is Strong Number (loop)
- Check whether a number is Perfect Number (loop)
- Check whether a number is prime (loop)
- Print all prime numbers between 1 and N
- Print all Armstrong numbers between 1 and N
- Print all palindrome numbers in a given range
- Print all factors of a number
- Find HCF (GCD) of two numbers (loop)
- Find LCM of two numbers (loop)
- Print pyramid pattern (star *)
- Print inverted pyramid pattern
- Print Floyd’s triangle (1, 2, 3...)
- Print Pascal’s triangle (loop)
- Print Number Pyramid Pattern
- Print Inverted Number Pyramid Pattern
- Print Number Diamond Pattern
Functions
- Function to add two numbers
- Function to subtract two numbers
- Function to multiply two numbers
- Function to divide two numbers
- Function for factorial (iteration)
- Factorial (recursion)
- Fibonaaci Series (iteration)
- Fibonacci (recursion)
- GCD using function
- GCD using Recursive function
- LCM using function
- LCM using Recursive function
- LCM using GCD Recursive function
- Swap numbers using function
- Prime number check using function
- Prime number check using Recursive function
- Palindrome check using function
- Palindrome check using Recursive function
- Armstrong Number check using function
- Armstrong Number check using Recursive function
- Function returning array maximum
- Function returning Array Minimum
- Recursive Function to print sum of digits of a number
- Recursive Function to reverse a number
- Recursive Function for power calculation
- Function to check strong number
- Function for perfect number
- Function overloading using macros (simulate)
- Nested function calls example
Arrays
- Input and display array elements
- Sum of array elements
- Average of array elements
- Maximum in array
- Minimum in array
- Linear Search Program
- Binary Search Program
- Bubble Sort Program
- Selection Sort Program
- Insertion Sort Program
- Merge Sort Program
- Quick Sort Program
- Merge two arrays
- Merge Two Sorted Arrays (Sorted Merge)
- Copy array to another
- Reverse array elements
- Count even and odd numbers in array
- Sum of Even and Odd Elements in Array
- Count positive, negative, and zero elements in an array
- Sum of Positive and Negative Numbers in Array
- Frequency of array elements
- Find unique elements in an array
- Find duplicate elements in an array
- Remove duplicate elements
- Find second largest element
- Find second smallest element
- Rotate array left
- Rotate array right
- Matrix addition (2D array)
- Matrix Subtraction (2D array)
- Matrix multiplication (2D array)
- Transpose of a matrix
- Sum of diagonals of a Square Matrix
- Display Matrix Upper and Lower Triangle
- Matrix Sum (Row-wise and Column-wise)
Strings
- Input and display string
- Find string length (without strlen())
- Copy string (without strcpy())
- Concatenate two strings
- Compare two strings
- Reverse string
- Check palindrome string
- Count vowels, consonants
- Count digits, spaces, special chars
- Count words in string
- Convert string to uppercase
- Convert string to lowercase
- Toggle case of characters
- Find frequency of characters
- Remove vowels from string
- Remove spaces from string
- Remove All Digits from a String
- Remove Special Characters from a String
- Remove duplicate characters
- Check anagram strings
- Sort characters in string
- Sort array of strings
- Longest word in a sentence
- Shortest word in a sentence
- Count Occurrences of a Word in a Sentence
Pointers
- Pointer basics (address, value)
- Pointer arithmetic
- Add two numbers using pointers
- Swap numbers using pointers
- Sum of array using pointers
- Find length of string using pointers
- Reverse string using pointers
- Compare two strings using pointers
- Concatenate strings using pointers
- Pointer to pointer (double pointer)
- Function returning pointer
- Pass array to function using pointers
- Dynamic array with pointers
- Matrix addition using pointers
- Matrix multiplication using pointers
- Matrix Transpose using pointers
- Matrix Trace and Sum of Diagonals using Pointers
- Sum of Rows and Columns using Pointers
- Interchange rows and columns (Matrix Swap) using Pointers
- Matrix Scalar Multiplication using Pointers
- Compare Two Matrices using Pointers
- Pointer to structure
- Structure with Dynamic Memory Allocation using Pointer
- Access structure elements with pointer
- Array of Structures Using Pointers
- Array of pointers
- String array with pointers
- Pointer and function (call by reference)
- Swap two arrays using pointers
Structures and Unions
- Student structure (input & display)
- Array of structures (students list)
- Nested structures (employee with address)
- Array of Nested Structures (Multiple Employees with Address)
- Passing structure to function
- Returning structure from function
- Complex number addition using structures
- Compare two dates using structures
- Bank account management (structure)
- Store book details using structure
- Store employee details (salary, age, etc.)
- Find student with highest marks (array of structures)
- Sort students by marks
- Demonstrate Memory Sharing in Union
- Structure vs Union (Memory Allocation & Data Sharing)
- Store cricket players’ details
- Store car details (company, model, price)
- Store hospital patient details
- Online shopping cart using structures
- Store weather report data
- Library management using structures
- Store exam records with pass/fail result
Dynamic Memory Allocation
- Allocate array using malloc()
- Allocate array using calloc()
- Reallocate array using realloc()
- Free memory using free()
- Input & print dynamic array
- Average of dynamic array
- Dynamic string input
- Reverse string using dynamic memory
- Dynamic matrix allocation
- Matrix addition using dynamic memory
- Matrix multiplication using dynamic memory
- Dynamic structure allocation (student info)
- Linked list creation with malloc()
- Insert node dynamically in linked list
- Delete node dynamically in linked list
- Stack using dynamic memory
- Queue using dynamic memory
- Dynamic 2D array (pointer-to-pointer)
- Dynamic 2D array (integer matrix) using pointer to pointer and returning it from a function
- Dynamic array resizing at runtime
- Dynamic sorting of array
- Dynamic string concatenation
File Handling
- Create and write to a file
- Read from a file
- Append to a file
- Copy file contents
- Count characters in file
- Count words in file
- Count lines in file
- Count vowels, consonants, digits and space in file
- Check whether file exists
- Merge two files into one
- Find frequency of a word in file
- Store student records in file
- Retrieve student records from file
- Student Record Management System using File Handling
- Update student record in file
- Delete student record from file
- Sort student records stored in file
- Encrypt file content
- Decrypt file content
- Find largest word in a file
- Find smallest word in a file
- Store employee payroll system in file
- Payroll Generation for employee
- Employee Payroll System with Payslip File Export
- Implement mini database using file handling
Data Structures in C
- Singly linked list creation
- Display linked list
- Insert node at beginning
- Insert node at end
- Insert node at position
- Delete node from beginning
- Delete node from end
- Delete Node from a Specific Position
- Delete node by Value
- Search element in Linked List
- Count Total Nodes in a Linked List
- Sum of all Nodes in a Linked List
- Maximum and Minimum Element in Linked List
- Reverse a Singly Linked List
- Doubly Linked List - Creation
- Doubly Linked List - Insert Node at the Beginning
- Doubly Linked List - Insert Node at the End
- Doubly Linked List - Insert Node at a Specific Position
- Deletion Operations in a Doubly Linked List - Beginning, End, and Position
- Reversal of Doubly Linked List
- Circular Linked List Creation
- Circular Linked List - Insertion Operations
- Circular Linked List - Deletion Operations
- Circular Linked List - Insertion, Deletion, Traversal Operations
- Stack Implementation Using Array
- Stack Implementation Using Linked List
- Queue Implementation Using Array
- Queue Implementation Using Linked List
- Circular Queue Implementation Using Array
- Circular Queue Implementation Using Linked List
- Circular Queue Implementation - Real World Example
- Binary Tree Creation
- Inorder Traversal in Binary Tree
- Inorder Traversal - Iterative Method
- Preorder Traversal in Binary Tree
- Postorder Traversal in Binary Tree
- Binary Search Tree Insertion
- Binary Search Tree (BST) — Search Operation
- Binary Search Tree (BST) — Deletion Operation
- Binary Search Tree Traversals
- Binary Search Tree — Level Order Traversal (BFS)
- Binary Search Tree — Height and Leaf Node Count of BST
- Binary Search Tree — Counting Total Nodes and Internal Nodes of BST
- Binary Search Tree — Mirror Image Creation
- Binary Search Tree — Counting Left and Right Subtree Nodes
- Binary Search Tree — Finding Minimum and Maximum Element in a BST
- Binary Search Tree — Searching for an Element in a BST
- Binary Search Tree — Deleting a Node from a Binary Search Tree
- Binary Search Tree — Finding the Height (or Depth) of a BST
- Binary Search Tree — Counting Total Number of Nodes, Leaf Nodes, and Internal Nodes
- Graph Representation (Adjacency Matrix)
- Graph Representation using Adjacency List
- BFS (Breadth First Search)
- DFS (Depth First Search)
- Graph Cycle Detection using DFS (Depth-First Search)
- Bubble Sort
- Quick Sort
- Merge Sort
- Heap Sort
- Binary Search
- Hash Table Basics
Advanced C Programs
- Command line arguments sum
- Command line arguments word count
- Display environment variables
- Bitwise AND, OR, XOR
- Bitwise left shift, right shift
- Check if number is power of 2
- Find number of set bits (Brian Kernighan’s algorithm)
- Endianness check
- Macro definition demo
- Create header file and include it
- Error handling using errno
- Pointer to function example
- Function pointer for callback
- Matrix multiplication (optimized)
- Sparse matrix representation
- Polynomial addition using linked list
- Polynomial multiplication using linked list
- Mini calculator with functions + switch
- Tic-tac-toe game
- Snake game (console-based)
- Student grading system with structures + files
- Hospital management mini project
- Bank management mini project
User-Defined Number Programs in C
- Check whether a number is even or odd
- Find the largest of two numbers (without built-in max)
- Find the largest of three numbers
- Check whether a number is prime
- Display all prime numbers between 1 and N
- Check whether a number is palindrome
- Check whether a number is Armstrong
- Find factorial of a number (loop)
- Factorial using recursion
- Generate Fibonacci series (loop)
- Generate Fibonacci series (recursion)
- Reverse a number
- Sum of digits of a number
- Product of digits of a number
- Count digits in a number
- Find GCD of two numbers (loop)
- Find LCM of two numbers
- Check whether a number is strong (sum of factorial of digits = number)
- Check whether a number is perfect (sum of divisors = number)
- Find power of a number (without pow())
- Convert decimal to binary
- Convert binary to decimal
- Convert octal to decimal
- Convert decimal to octal
- Convert decimal to hexadecimal
- Check whether number is automorphic (square ends with same digits)
- Check whether number is neon (sum of digits of square = number)
- Print factors of a number
- Sum of N natural numbers (loop)
- Sum of N natural numbers (recursion)
User-Defined String Programs in C
- Find length of a string (without strlen())
- Copy one string to another (without strcpy())
- Concatenate two strings (without strcat())
- Compare two strings (without strcmp())
- Reverse a string (user-defined)
- Check whether a string is palindrome
- Convert string to uppercase (without strupr())
- Convert string to lowercase (without strlwr())
- Toggle case of each character
- Count vowels in a string
- Count consonants in a string
- Count digits in a string
- Count special characters in a string
- Count words in a string
- Remove all vowels from a string
- Remove all spaces from a string
- Remove duplicate characters
- Frequency of each character in string
- Find the longest word in a sentence
- Find the shortest word in a sentence
- Sort characters of string in ascending order
- Sort array of strings alphabetically
- Check whether two strings are anagrams
- Find first non-repeating character
- Replace a character in a string with another
- Remove particular character from string
- Count occurrences of a given word in string
- Check substring existence (without strstr())
- Extract substring (user-defined)
- Rotate string left/right by N characters
(using if, if-else, nested if, switch-case)
(using for, while, do-while loops)
C Programs
![]() Share with a Friend |
C Program: Swap Two Numbers (Using Third Variable)
Swapping two numbers is a fundamental programming concept in C. It means exchanging the values of two variables so that the first variable holds the value of the second, and the second variable holds the value of the first.
There are multiple ways to swap two numbers in C:
- Using a third (temporary) variable → the simplest and most common method.
- Without using a third variable → by applying arithmetic operations (+, -, *, /) or the bitwise XOR operator.
In this program, we demonstrate the first method: swapping using a temporary variable. This approach is simple and easy to understand, especially for beginners.
C Program: Swap Two Numbers (Using Third Variable)
#include <stdio.h> // Standard I/O header
int main() {
int a, b, temp; // variables for two numbers and a temporary variable
// Input from user
printf("Enter first number (a): ");
scanf("%d", &a);
printf("Enter second number (b): ");
scanf("%d", &b);
// Display before swapping
printf("\nBefore Swapping:\n");
printf("a = %d, b = %d\n", a, b);
// Swapping using third variable
temp = a; // store value of a in temp
a = b; // copy value of b into a
b = temp; // copy value of temp (original a) into b
// Display after swapping
printf("\nAfter Swapping:\n");
printf("a = %d, b = %d\n", a, b);
return 0; // Successful termination
}
Output
OUTPUT : Enter first number (a): 5 Enter second number (b): 10 Before Swapping: a = 5, b = 10 After Swapping: a = 10, b = 5
Explanation :
- #include <stdio.h>
- Includes Standard I/O functions (printf, scanf).
- int a, b, temp;
- Declares three integers:
- a → first number
- b → second number
- temp → temporary variable used for swapping
- Declares three integers:
- scanf("%d", &a); scanf("%d", &b);
- Takes input for a and b from the user.
- Before Swapping Output
- Displays original values of a and b.
- Swapping Process
- temp = a; → temporarily stores a
- a = b; → assigns value of b to a
- b = temp; → assigns original value of a (stored in temp) to b
- After Swapping Output
- Displays the new values of a and b.
- return 0;
- Ends the program successfully.
