1. Hello World:
#include <stdio.h>int main() {printf("Hello, World!\n");return 0;}
This classic program introduces the basic structure of a C program and the printf function.
2. Calculator:
#include <stdio.h>int main() {int num1, num2, sum, product;printf("Enter two numbers: ");scanf("%d %d", &num1, &num2);sum = num1 + num2;product = num1 * num2;printf("Sum: %d\n", sum);printf("Product: %d\n", product);return 0;}
This program takes two numbers as input, calculates their sum and product, and then prints the results.
3. Factorial Calculation:
#include <stdio.h>int factorial(int n) {if (n == 0 || n == 1)return 1;elsereturn n * factorial(n - 1);}int main() {int num;printf("Enter a number: ");scanf("%d", &num);if (num < 0)printf("Factorial is not defined for negative numbers.\n");elseprintf("Factorial of %d is %d\n", num, factorial(num));return 0;}
This program calculates the factorial of a given number using a recursive function.
4. Fibonacci Series:
#include <stdio.h>void fibonacci(int n) {int a = 0, b = 1, next;printf("Fibonacci Series: ");for (int i = 0; i < n; i++) {printf("%d, ", a);next = a + b;a = b;b = next;}printf("\n");}int main() {int terms;printf("Enter the number of terms in the Fibonacci series: ");scanf("%d", &terms);fibonacci(terms);return 0;}
This program generates the Fibonacci series up to a specified number of terms.
5. Sum of Two Numbers:
#include <stdio.h>int main() {int num1, num2, sum;printf("Enter two numbers: ");scanf("%d %d", &num1, &num2);sum = num1 + num2;printf("Sum: %d\n", sum);return 0;}
This program takes two numbers as input, calculates their sum, and prints the result.
6. Factorial Calculation:
#include <stdio.h>int factorial(int n) {if (n == 0 || n == 1)return 1;elsereturn 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;}
This program calculates the factorial of a given number using a recursive function.
7. Odd or Even:
#include <stdio.h>int main() {int num;printf("Enter an integer: ");scanf("%d", &num);if (num % 2 == 0)printf("%d is even.\n", num);elseprintf("%d is odd.\n", num);return 0;}
This program checks whether a given integer is odd or even.
8. Simple Calculator:
#include <stdio.h>int main() {char operator;double num1, num2, result;printf("Enter an operator (+, -, *, /): ");scanf(" %c", &operator);printf("Enter two numbers: ");scanf("%lf %lf", &num1, &num2);switch (operator) {case '+':result = num1 + num2;break;case '-':result = num1 - num2;break;case '*':result = num1 * num2;break;case '/':if (num2 != 0)result = num1 / num2;else {printf("Error: Division by zero is undefined.\n");return 1; // Exit with an error code}break;default:printf("Error: Invalid operator.\n");return 1; // Exit with an error code}printf("Result: %lf\n", result);return 0;}
This program implements a simple calculator that performs addition, subtraction, multiplication, and division.
9. Prime Number Check:
#include <stdio.h>#include <stdbool.h>bool isPrime(int n) {if (n <= 1)return false;for (int i = 2; i * i <= n; i++) {if (n % i == 0)return false;}return true;}int main() {int num;printf("Enter a number: ");scanf("%d", &num);if (isPrime(num))printf("%d is a prime number.\n", num);elseprintf("%d is not a prime number.\n", num);return 0;}
This program checks whether a given number is prime or not.
10. Palindrome Check:
#include <stdio.h>#include <stdbool.h>bool isPalindrome(int num) {int originalNum = num, reversedNum = 0, remainder;while (num > 0) {remainder = num % 10;reversedNum = reversedNum * 10 + remainder;num /= 10;}return originalNum == reversedNum;}int main() {int num;printf("Enter a number: ");scanf("%d", &num);if (isPalindrome(num))printf("%d is a palindrome.\n", num);elseprintf("%d is not a palindrome.\n", num);return 0;}
This program checks whether a given number is a palindrome or not.
Advance programmer
1. Linear Search:
#include <stdio.h>int linearSearch(int arr[], int n, int key) {for (int i = 0; i < n; i++) {if (arr[i] == key)return i; // Key found, return the index}return -1; // Key not found}int main() {int arr[] = {10, 20, 30, 40, 50};int n = sizeof(arr) / sizeof(arr[0]);int key;printf("Enter the element to search: ");scanf("%d", &key);int result = linearSearch(arr, n, key);if (result != -1)printf("Element %d found at index %d.\n", key, result);elseprintf("Element %d not found in the array.\n", key);return 0;}
This program implements linear search to find an element in an array.
2. Binary Search:
#include <stdio.h>int binarySearch(int arr[], int left, int right, int key) {while (left <= right) {int mid = left + (right - left) / 2;if (arr[mid] == key)return mid; // Key found, return the indexif (arr[mid] < key)left = mid + 1;elseright = mid - 1;}return -1; // Key not found}int main() {int arr[] = {10, 20, 30, 40, 50};int n = sizeof(arr) / sizeof(arr[0]);int key;printf("Enter the element to search: ");scanf("%d", &key);int result = binarySearch(arr, 0, n - 1, key);if (result != -1)printf("Element %d found at index %d.\n", key, result);elseprintf("Element %d not found in the array.\n", key);return 0;}
This program implements binary search to find an element in a sorted array.
3. Bubble Sort:
#include <stdio.h>void bubbleSort(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]) {// Swap elements if they are in the wrong orderint temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;}}}}int main() {int arr[] = {64, 34, 25, 12, 22, 11, 90};int n = sizeof(arr) / sizeof(arr[0]);printf("Original array: ");for (int i = 0; i < n; i++)printf("%d ", arr[i]);bubbleSort(arr, n);printf("\nSorted array: ");for (int i = 0; i < n; i++)printf("%d ", arr[i]);return 0;}
This program implements the bubble sort algorithm to sort an array in ascending order.
4. Linked List:
#include <stdio.h>#include <stdlib.h>struct Node {int data;struct Node* next;};void printList(struct Node* node) {while (node != NULL) {printf("%d ", node->data);node = node->next;}}int main() {struct Node* head = NULL;struct Node* second = NULL;struct Node* third = NULL;head = (struct Node*)malloc(sizeof(struct Node));second = (struct Node*)malloc(sizeof(struct Node));third = (struct Node*)malloc(sizeof(struct Node));head->data = 1;head->next = second;second->data = 2;second->next = third;third->data = 3;third->next = NULL;printf("Linked List elements: ");printList(head);return 0;}
This program demonstrates the creation and printing of a simple linked list.
5. Factorial using Recursion:
#include <stdio.h>int factorial(int n) {if (n == 0 || n == 1)return 1;elsereturn 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;}
This program calculates the factorial of a given number using recursion.
Social Plugin