C Programs & Examples
Here’s a curated and efficient set of C programs for all the listed topics. Due to
the large number of problems, I’ll provide them in batches, ensuring optimal
solutions for each. Here you go manishawww 🕺
Basic Programs
Hello World
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Taking Input from User
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("You entered: %d\n", num);
return 0;
}
Find ASCII Value of Character
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
printf("ASCII value of '%c' is %d\n", c, c);
return 0;
}
Using gets() Function
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
gets(str); // Avoid in newer C standards; consider fgets() for
safety.
printf("You entered: %s\n", str);
return 0;
}
If-Else
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("Positive\n");
else if (num < 0)
printf("Negative\n");
else
printf("Zero\n");
return 0;
}
Switch Case
#include <stdio.h>
int main() {
int choice;
printf("Enter a number (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You chose One.\n");
break;
case 2:
printf("You chose Two.\n");
break;
case 3:
printf("You chose Three.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
Checking for Vowel
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' ||
c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') {
printf("%c is a vowel.\n", c);
} else {
printf("%c is not a vowel.\n", c);
}
return 0;
}
Reversing Case of Character
#include <stdio.h>
int main() {
char c;
printf("Enter a character: ");
scanf("%c", &c);
if (c >= 'a' && c <= 'z')
c = c - 32; // Convert to uppercase
else if (c >= 'A' && c <= 'Z')
c = c + 32; // Convert to lowercase
printf("Reversed case: %c\n", c);
return 0;
}
Swapping Two Numbers
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
return 0;
}
Largest and Smallest using Global Declaration
#include <stdio.h>
int a, b, c;
void findLargestSmallest() {
int largest = (a > b) ? (a > c ? a : c) : (b > c ? b : c);
int smallest = (a < b) ? (a < c ? a : c) : (b < c ? b : c);
printf("Largest: %d, Smallest: %d\n", largest, smallest);
}
int main() {
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
findLargestSmallest();
return 0;
}
Loops
Basic For Loop
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
return 0;
}
Basic While Loop
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
Basic Do-While Loop
#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
printf("\n");
return 0;
}
Nested For Loops
#include <stdio.h>
int main() {
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("(%d, %d) ", i, j);
}
printf("\n");
}
return 0;
}
Program to Find Factorial of a Number
#include <stdio.h>
int main() {
int num, fact = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
fact *= i;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}
Fibonacci Series Program
#include <stdio.h>
int main() {
int n, a = 0, b = 1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
printf("%d ", a);
next = a + b;
a = b;
b = next;
}
printf("\n");
return 0;
}
Palindrome Program
#include <stdio.h>
int main() {
int num, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &num);
original = num;
while (num != 0) {
remainder = num % 10;
reversed = reversed * 10 + remainder;
num /= 10;
}
if (original == reversed)
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}
Number Crunching
Program to Find Average of n Numbers
#include <stdio.h>
int main() {
int n;
float sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the numbers: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Average = %.2f\n", sum / n);
return 0;
}
Armstrong Number
#include <stdio.h>
#include <math.h>
int main() {
int num, original, remainder, result = 0, n = 0;
printf("Enter an integer: ");
scanf("%d", &num);
original = num;
while (original != 0) {
original /= 10;
n++;
}
original = num;
while (original != 0) {
remainder = original % 10;
result += pow(remainder, n);
original /= 10;
}
if (result == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
Checking Input Number for Odd or Even
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is Even\n", num);
else
printf("%d is Odd\n", num);
return 0;
}
Print Factors of a Number
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factors of %d are: ", num);
for (int i = 1; i <= num; i++) {
if (num % i == 0)
printf("%d ", i);
}
printf("\n");
return 0;
}
Find Sum of n Numbers
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the numbers: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum = %d\n", sum);
return 0;
}
Print First n Prime Numbers
#include <stdio.h>
int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i * i <= n; i++) {
if (n % i == 0) return 0;
}
return 1;
}
int main() {
int n, count = 0, num = 2;
printf("Enter the number of prime numbers: ");
scanf("%d", &n);
while (count < n) {
if (isPrime(num)) {
printf("%d ", num);
count++;
}
num++;
}
printf("\n");
return 0;
}
Find Largest Among n Numbers
#include <stdio.h>
int main() {
int n, largest;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the numbers: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest)
largest = arr[i];
}
printf("Largest number = %d\n", largest);
return 0;
}
Exponential Without pow() Method
#include <stdio.h>
int main() {
int base, exp, result = 1;
printf("Enter base and exponent: ");
scanf("%d %d", &base, &exp);
for (int i = 1; i <= exp; i++) {
result *= base;
}
printf("%d^%d = %d\n", base, exp, result);
return 0;
}
Find Whether Number is Integer or Float
#include <stdio.h>
int main() {
float num;
printf("Enter a number: ");
scanf("%f", &num);
if ((int)num == num)
printf("The number is an integer.\n");
else
printf("The number is a float.\n");
return 0;
}
Print Multiplication Table of Input Number
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
:
Concept of Arrays
Reverse an Array
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Reversed array: ");
for (int i = n - 1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Insert Element to Array
#include <stdio.h>
int main() {
int n, pos, elem;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n + 1];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the position to insert (1 to %d): ", n + 1);
scanf("%d", &pos);
printf("Enter the element to insert: ");
scanf("%d", &elem);
for (int i = n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = elem;
printf("Array after insertion: ");
for (int i = 0; i <= n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Delete Element from Array
#include <stdio.h>
int main() {
int n, pos;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the position of the element to delete (1 to %d): ",
n);
scanf("%d", &pos);
for (int i = pos - 1; i < n - 1; i++) {
arr[i] = arr[i + 1];
}
printf("Array after deletion: ");
for (int i = 0; i < n - 1; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Largest and Smallest Element in Array
#include <stdio.h>
int main() {
int n, largest, smallest;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
largest = smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest)
largest = arr[i];
if (arr[i] < smallest)
smallest = arr[i];
}
printf("Largest element: %d\n", largest);
printf("Smallest element: %d\n", smallest);
return 0;
}
Sum of N Numbers Using Arrays
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum of elements: %d\n", sum);
return 0;
}
Sort Array Elements
#include <stdio.h>
void sortArray(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
sortArray(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
Remove Duplicate Elements
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements: ", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
int temp[n], k = 0;
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < k; j++) {
if (arr[i] == temp[j]) {
flag = 1;
break;
}
}
if (!flag) {
temp[k++] = arr[i];
}
}
printf("Array after removing duplicates: ");
for (int i = 0; i < k; i++) {
printf("%d ", temp[i]);
}
printf("\n");
return 0;
}
Sparse Matrix
#include <stdio.h>
int main() {
int m, n, count = 0;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0)
count++;
}
}
if (count > (m * n) / 2)
printf("The matrix is a sparse matrix.\n");
else
printf("The matrix is not a sparse matrix.\n");
return 0;
}
Square Matrix
#include <stdio.h>
int main() {
int n;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int matrix[n][n];
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
printf("The square matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
Concept of Matrices
Determinant of 2x2 Matrix
#include <stdio.h>
int main() {
int matrix[2][2];
printf("Enter elements of 2x2 matrix:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
scanf("%d", &matrix[i][j]);
}
}
int determinant = (matrix[0][0] * matrix[1][1]) - (matrix[0][1] *
matrix[1][0]);
printf("Determinant of the matrix: %d\n", determinant);
return 0;
}
Normal and Trace of a Square Matrix
#include <stdio.h>
#include <math.h>
int main() {
int n;
printf("Enter the size of the square matrix: ");
scanf("%d", &n);
int matrix[n][n];
printf("Enter elements of the matrix:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
int trace = 0;
double norm = 0.0;
for (int i = 0; i < n; i++) {
trace += matrix[i][i];
for (int j = 0; j < n; j++) {
norm += matrix[i][j] * matrix[i][j];
}
}
norm = sqrt(norm);
printf("Trace of the matrix: %d\n", trace);
printf("Norm of the matrix: %.2f\n", norm);
return 0;
}
Addition and Subtraction of Matrices
#include <stdio.h>
int main() {
int m, n;
printf("Enter the number of rows and columns of the matrices: ");
scanf("%d %d", &m, &n);
int mat1[m][n], mat2[m][n], sum[m][n], diff[m][n];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &mat2[i][j]);
}
}
printf("Matrix addition:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
printf("%d ", sum[i][j]);
}
printf("\n");
}
printf("Matrix subtraction:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
diff[i][j] = mat1[i][j] - mat2[i][j];
printf("%d ", diff[i][j]);
}
printf("\n");
}
return 0;
}
Matrix Multiplication
#include <stdio.h>
int main() {
int m1, n1, m2, n2;
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &m1, &n1);
printf("Enter rows and columns for second matrix: ");
scanf("%d %d", &m2, &n2);
if (n1 != m2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
int mat1[m1][n1], mat2[m2][n2], product[m1][n2];
printf("Enter elements of first matrix:\n");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
scanf("%d", &mat1[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (int i = 0; i < m2; i++) {
for (int j = 0; j < n2; j++) {
scanf("%d", &mat2[i][j]);
}
}
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
product[i][j] = 0;
for (int k = 0; k < n1; k++) {
product[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
printf("Product of matrices:\n");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}
return 0;
}
Sparse Matrix Representation
#include <stdio.h>
int main() {
int m, n, count = 0;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
printf("Enter elements of the matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
if (matrix[i][j] == 0) {
count++;
}
}
}
printf("Sparse Matrix Representation:\n");
if (count > (m * n) / 2) {
printf("The matrix is sparse. Zero elements count: %d\n",
count);
} else {
printf("The matrix is not sparse.\n");
}
return 0;
}
Pointers
Simple Program
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
printf("Value of x: %d\n", x);
printf("Address of x: %p\n", &x);
printf("Value of ptr: %p\n", ptr);
printf("Value pointed by ptr: %d\n", *ptr);
return 0;
}
Memory Management (Dynamic Allocation)
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int *arr = (int *)malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Array elements are:\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
free(arr);
return 0;
}
Array of Pointers
#include <stdio.h>
int main() {
const char *arr[] = {"Hello", "World", "Pointers"};
int n = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < n; i++) {
printf("%s\n", arr[i]);
}
return 0;
}
Pointer Increment and Decrement
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40};
int *ptr = arr;
printf("Using pointer increment:\n");
for (int i = 0; i < 4; i++) {
printf("%d ", *ptr);
ptr++;
}
printf("\n");
printf("Using pointer decrement:\n");
for (int i = 3; i >= 0; i--) {
printf("%d ", *(arr + i));
}
printf("\n");
return 0;
}
Pointer Comparison
#include <stdio.h>
int main() {
int x = 10, y = 20;
int *ptr1 = &x, *ptr2 = &y;
if (ptr1 > ptr2) {
printf("ptr1 points to a higher address\n");
} else {
printf("ptr2 points to a higher address\n");
}
return 0;
}
Pointer to a Pointer
#include <stdio.h>
int main() {
int x = 10;
int *ptr = &x;
int **pptr = &ptr;
printf("Value of x: %d\n", x);
printf("Value using single pointer (ptr): %d\n", *ptr);
printf("Value using double pointer (pptr): %d\n", **pptr);
printf("Address of x: %p\n", &x);
printf("Value of ptr (address of x): %p\n", ptr);
printf("Value of pptr (address of ptr): %p\n", pptr);
return 0;
}
Concatenate Strings using Pointer
#include <stdio.h>
#include <string.h>
void concatenate(char *str1, char *str2) {
while (*str1) str1++;
while (*str2) {
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
}
int main() {
char str1[100] = "Hello, ";
char str2[] = "World!";
concatenate(str1, str2);
printf("Concatenated String: %s\n", str1);
return 0;
}
Reverse a String using Pointer
#include <stdio.h>
#include <string.h>
void reverseString(char *str) {
char *start = str, *end = str + strlen(str) - 1, temp;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str);
printf("Reversed String: %s\n", str);
return 0;
}
Swapping Two Numbers using Pointer
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int main() {
int x = 5, y = 10;
printf("Before swap: x = %d, y = %d\n", x, y);
swap(&x, &y);
printf("After swap: x = %d, y = %d\n", x, y);
return 0;
}
Pointer to a Function
#include <stdio.h>
int add(int a, int b) {
return a + b;
}
int main() {
int (*funcPtr)(int, int) = add;
int result = funcPtr(10, 20);
printf("Sum: %d\n", result);
return 0;
}
Null Pointer
#include <stdio.h>
int main() {
int *ptr = NULL;
if (ptr == NULL) {
printf("Pointer is NULL.\n");
} else {
printf("Pointer is not NULL.\n");
}
return 0;
}
Concept of Recursion
Adding Two Numbers
#include <stdio.h>
int add(int a, int b) {
if (b == 0)
return a;
return add(a + 1, b - 1);
}
int main() {
int num1 = 5, num2 = 10;
printf("Sum of %d and %d is %d\n", num1, num2, add(num1, num2));
return 0;
}
Factorial
#include <stdio.h>
int factorial(int n) {
if (n == 0 || n == 1)
return 1;
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Fibonacci Series
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int terms;
printf("Enter the number of terms: ");
scanf("%d", &terms);
printf("Fibonacci Series: ");
for (int i = 0; i < terms; i++) {
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
Sum of First N Numbers
#include <stdio.h>
int sumOfN(int n) {
if (n == 0)
return 0;
return n + sumOfN(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of first %d numbers is %d\n", num, sumOfN(num));
return 0;
}
Sum of Digits
#include <stdio.h>
int sumOfDigits(int n) {
if (n == 0)
return 0;
return (n % 10) + sumOfDigits(n / 10);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of digits of %d is %d\n", num, sumOfDigits(num));
return 0;
}
Palindrome
#include <stdio.h>
#include <string.h>
int isPalindrome(char str[], int start, int end) {
if (start >= end)
return 1;
if (str[start] != str[end])
return 0;
return isPalindrome(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isPalindrome(str, 0, strlen(str) - 1))
printf("The string is a palindrome.\n");
else
printf("The string is not a palindrome.\n");
return 0;
}
Power of N
#include <stdio.h>
int power(int base, int exp) {
if (exp == 0)
return 1;
return base * power(base, exp - 1);
}
int main() {
int base, exp;
printf("Enter the base: ");
scanf("%d", &base);
printf("Enter the exponent: ");
scanf("%d", &exp);
printf("%d^%d = %d\n", base, exp, power(base, exp));
return 0;
}
Largest Element in an Array
#include <stdio.h>
int findLargest(int arr[], int n, int max) {
if (n == 0)
return max;
if (arr[n - 1] > max)
max = arr[n - 1];
return findLargest(arr, n - 1, max);
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements: ");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
int largest = findLargest(arr, n, arr[0]);
printf("Largest element in the array is: %d\n", largest);
return 0;
}
Prime or Composite
#include <stdio.h>
int isPrime(int n, int i) {
if (n <= 2)
return (n == 2) ? 1 : 0;
if (n % i == 0)
return 0;
if (i * i > n)
return 1;
return isPrime(n, i + 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (isPrime(num, 2))
printf("%d is a prime number.\n", num);
else
printf("%d is a composite number.\n", num);
return 0;
}
LCM of Two Numbers
#include <stdio.h>
int findLCM(int a, int b, int lcm) {
if (lcm % a == 0 && lcm % b == 0)
return lcm;
return findLCM(a, b, lcm + 1);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
int lcm = findLCM(num1, num2, (num1 > num2) ? num1 : num2);
printf("LCM of %d and %d is: %d\n", num1, num2, lcm);
return 0;
}
GCD of Two Numbers
#include <stdio.h>
int findGCD(int a, int b) {
if (b == 0)
return a;
return findGCD(b, a % b);
}
int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is: %d\n", num1, num2, findGCD(num1,
num2));
return 0;
}
Reverse a String
#include <stdio.h>
#include <string.h>
void reverseString(char str[], int start, int end) {
if (start >= end)
return;
char temp = str[start];
str[start] = str[end];
str[end] = temp;
reverseString(str, start + 1, end - 1);
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
reverseString(str, 0, strlen(str) - 1);
printf("Reversed string: %s\n", str);
return 0;
}
Next Section: File and Streams
List Files in Directory
#include <stdio.h>
#include <dirent.h>
int main() {
struct dirent *de; // Pointer for directory entry
DIR *dr = opendir("."); // Open current directory
if (dr == NULL) {
printf("Could not open current directory.\n");
return 1;
}
printf("Files in current directory:\n");
while ((de = readdir(dr)) != NULL) {
printf("%s\n", de->d_name);
}
closedir(dr);
return 0;
}
Size of File
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
long size;
printf("Enter the file name: ");
scanf("%s", filename);
file = fopen(filename, "rb");
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
fclose(file);
printf("Size of file '%s' is: %ld bytes.\n", filename, size);
return 0;
}
Write in File
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
char text[1000];
printf("Enter the file name: ");
scanf("%s", filename);
getchar(); // Consume newline character
printf("Enter text to write in the file:\n");
fgets(text, sizeof(text), stdin);
file = fopen(filename, "w");
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
fprintf(file, "%s", text);
fclose(file);
printf("Text written successfully to '%s'.\n", filename);
return 0;
}
Reverse Content of File
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
long size;
char ch;
printf("Enter the file name: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("Unable to open file.\n");
return 1;
}
fseek(file, 0, SEEK_END);
size = ftell(file);
printf("Reversed content of the file:\n");
for (long i = size - 1; i >= 0; i--) {
fseek(file, i, SEEK_SET);
ch = fgetc(file);
printf("%c", ch);
}
fclose(file);
return 0;
}
Copy File to Another File
#include <stdio.h>
int main() {
FILE *source, *destination;
char sourceFile[100], destFile[100];
char ch;
printf("Enter the source file name: ");
scanf("%s", sourceFile);
printf("Enter the destination file name: ");
scanf("%s", destFile);
source = fopen(sourceFile, "r");
if (source == NULL) {
printf("Unable to open source file.\n");
return 1;
}
destination = fopen(destFile, "w");
if (destination == NULL) {
printf("Unable to open destination file.\n");
fclose(source);
return 1;
}
while ((ch = fgetc(source)) != EOF) {
fputc(ch, destination);
}
fclose(source);
fclose(destination);
printf("File copied successfully from '%s' to '%s'.\n", sourceFile,
destFile);
return 0;
}
Next Section: Mixed Programs-1
Adding Two Numbers Using Pointers
#include <stdio.h>
int main() {
int a, b, sum;
int *p1, *p2;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
p1 = &a;
p2 = &b;
sum = *p1 + *p2; // Adding the numbers using pointers
printf("Sum of %d and %d is: %d\n", a, b, sum);
return 0;
}
Area and Circumference of Circle
#include <stdio.h>
#define PI 3.14159265359
int main() {
float radius, area, circumference;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
circumference = 2 * PI * radius;
printf("Area of the circle: %.2f\n", area);
printf("Circumference of the circle: %.2f\n", circumference);
return 0;
}
Area of Triangle
#include <stdio.h>
int main() {
float base, height, area;
printf("Enter the base of the triangle: ");
scanf("%f", &base);
printf("Enter the height of the triangle: ");
scanf("%f", &height);
area = 0.5 * base * height;
printf("Area of the triangle: %.2f\n", area);
return 0;
}
Basic Arithmetic Operations
#include <stdio.h>
int main() {
float num1, num2;
char operator;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Enter operator (+, -, *, /): ");
scanf(" %c", &operator);
switch (operator) {
case '+':
printf("Result: %.2f\n", num1 + num2);
break;
case '-':
printf("Result: %.2f\n", num1 - num2);
break;
case '*':
printf("Result: %.2f\n", num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("Result: %.2f\n", num1 / num2);
} else {
printf("Error! Division by zero.\n");
}
break;
default:
printf("Invalid operator.\n");
}
return 0;
}
Conversion Between Number Systems
#include <stdio.h>
#include <math.h>
int main() {
int num, choice;
printf("Choose the number system conversion:\n");
printf("1. Decimal to Binary\n2. Decimal to Hexadecimal\n3. Decimal
to Octal\n");
scanf("%d", &choice);
printf("Enter the number: ");
scanf("%d", &num);
switch (choice) {
case 1:
printf("Binary: ");
for (int i = 31; i >= 0; i--) {
printf("%d", (num >> i) & 1);
}
printf("\n");
break;
case 2:
printf("Hexadecimal: %X\n", num);
break;
case 3:
printf("Octal: %o\n", num);
break;
default:
printf("Invalid choice.\n");
break;
}
return 0;
}
Celsius to Fahrenheit
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9/5) + 32;
printf("Temperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}
Simple Interest
#include <stdio.h>
int main() {
float principal, rate, time, interest;
printf("Enter principal amount: ");
scanf("%f", &principal);
printf("Enter rate of interest: ");
scanf("%f", &rate);
printf("Enter time period (in years): ");
scanf("%f", &time);
interest = (principal * rate * time) / 100;
printf("Simple Interest: %.2f\n", interest);
return 0;
}
Greatest Common Divisor (GCD)
#include <stdio.h>
int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("GCD of %d and %d is: %d\n", a, b, gcd(a, b));
return 0;
}
Next Section: Mixed Programs-2
Roots of Quadratic Equation
#include <stdio.h>
#include <math.h>
int main() {
float a, b, c, discriminant, root1, root2;
printf("Enter coefficients a, b, and c: ");
scanf("%f %f %f", &a, &b, &c);
discriminant = (b * b) - (4 * a * c);
if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and different.\n");
printf("Root 1 = %.2f\n", root1);
printf("Root 2 = %.2f\n", root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and the same.\n");
printf("Root = %.2f\n", root1);
} else {
printf("No real roots.\n");
}
return 0;
}
Identifying a Perfect Square
#include <stdio.h>
#include <math.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (sqrt(num) == (int)sqrt(num)) {
printf("%d is a perfect square.\n", num);
} else {
printf("%d is not a perfect square.\n", num);
}
return 0;
}
Calculate nPr and nCr
#include <stdio.h>
int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
int main() {
int n, r, nPr, nCr;
printf("Enter values of n and r: ");
scanf("%d %d", &n, &r);
nPr = factorial(n) / factorial(n - r);
nCr = factorial(n) / (factorial(r) * factorial(n - r));
printf("nPr = %d\n", nPr);
printf("nCr = %d\n", nCr);
return 0;
}
Windows Shutdown
#include <stdio.h>
#include <stdlib.h>
int main() {
system("shutdown /s /t 0"); // Command for Windows shutdown
return 0;
}
Without Main Function
#include <stdio.h>
void _start() {
printf("This program does not have a main function.\n");
return;
}
(Note: This approach works in some compilers but is not recommended for
standard practice.)
Menu Driven Program
#include <stdio.h>
int main() {
int choice;
do {
printf("Menu:\n");
printf("1. Print Hello World\n");
printf("2. Add Two Numbers\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Hello, World!\n");
break;
case 2:
{
float num1, num2;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("Sum: %.2f\n", num1 + num2);
}
break;
case 3:
printf("Exiting program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 3);
return 0;
}
Changing Text Background Color
#include <stdio.h>
int main() {
printf("\033[1;31mThis text has a red foreground.\033[0m\n"); //
Red text
printf("\033[1;44mThis text has a blue background.\033[0m\n"); //
Blue background
return 0;
}
Current Date and Time
#include <stdio.h>
#include <time.h>
int main() {
time_t t;
struct tm *tm_info;
time(&t);
tm_info = localtime(&t);
printf("Current Date and Time: %s", asctime(tm_info));
return 0;
}
That’s all for C language programs, 🐮