Tutorials Logic
Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

Basic C Language Programs — Hello World to Sorting

20 Basic C Programs with Code

Practice these beginner-friendly C programs to strengthen loops, conditionals, arrays, functions, and strings.

1) Hello World

Hello World - First C Program
#include <stdio.h>
int main() {
    printf("Hello, World!\\n");
    return 0;
}

2) Sum of Two Numbers

Sum of Two Numbers
#include <stdio.h>
int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    printf("Sum = %d\\n", a + b);
    return 0;
}

3) Even or Odd

Even or Odd Check
#include <stdio.h>
int main() {
    int n;
    scanf("%d", &n);
    if (n % 2 == 0) printf("Even\\n");
    else printf("Odd\\n");
    return 0;
}

4) Largest of Three Numbers

Largest of Three Numbers
#include <stdio.h>
int main() {
    int a, b, c, max;
    scanf("%d %d %d", &a, &b, &c);
    max = a;
    if (b > max) max = b;
    if (c > max) max = c;
    printf("Largest = %d\\n", max);
    return 0;
}

5) Factorial of a Number

Factorial of a Number
#include <stdio.h>
int main() {
    int n, i;
    long long fact = 1;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) fact *= i;
    printf("Factorial = %lld\\n", fact);
    return 0;
}

6) Fibonacci Series (n terms)

Fibonacci Series
#include <stdio.h>
int main() {
    int n, i;
    long long a = 0, b = 1, next;
    scanf("%d", &n);
    for (i = 1; i <= n; i++) {
        printf("%lld ", a);
        next = a + b;
        a = b;
        b = next;
    }
    return 0;
}

7) Prime Number Check

Prime Number Check
#include <stdio.h>
int main() {
    int n, i, prime = 1;
    scanf("%d", &n);
    if (n <= 1) prime = 0;
    for (i = 2; i * i <= n && prime; i++) {
        if (n % i == 0) prime = 0;
    }
    if (prime) printf("Prime\\n");
    else printf("Not Prime\\n");
    return 0;
}

8) Reverse a Number

Reverse a Number
#include <stdio.h>
int main() {
    int n, rev = 0;
    scanf("%d", &n);
    while (n != 0) {
        rev = rev * 10 + n % 10;
        n /= 10;
    }
    printf("Reversed = %d\\n", rev);
    return 0;
}

9) Palindrome Number Check

Palindrome Number Check
#include <stdio.h>
int main() {
    int n, temp, rev = 0;
    scanf("%d", &n);
    temp = n;
    while (temp != 0) {
        rev = rev * 10 + temp % 10;
        temp /= 10;
    }
    if (rev == n) printf("Palindrome\\n");
    else printf("Not Palindrome\\n");
    return 0;
}

10) Armstrong Number Check

Armstrong Number Check
#include <stdio.h>
int main() {
    int n, temp, rem, sum = 0;
    scanf("%d", &n);
    temp = n;
    while (temp != 0) {
        rem = temp % 10;
        sum += rem * rem * rem;
        temp /= 10;
    }
    if (sum == n) printf("Armstrong\\n");
    else printf("Not Armstrong\\n");
    return 0;
}

11) Swap Two Numbers Without Third Variable

Swap Without Third Variable
#include <stdio.h>
int main() {
    int a, b;
    scanf("%d %d", &a, &b);
    a = a + b;
    b = a - b;
    a = a - b;
    printf("a=%d b=%d\\n", a, b);
    return 0;
}

12) GCD of Two Numbers (Euclidean)

GCD (Euclidean Algorithm)
#include <stdio.h>
int main() {
    int a, b, t;
    scanf("%d %d", &a, &b);
    while (b != 0) {
        t = b;
        b = a % b;
        a = t;
    }
    printf("GCD = %d\\n", a);
    return 0;
}

13) LCM of Two Numbers

LCM of Two Numbers
#include <stdio.h>
int main() {
    int a, b, x, y, t, gcd;
    scanf("%d %d", &a, &b);
    x = a; y = b;
    while (y != 0) {
        t = y;
        y = x % y;
        x = t;
    }
    gcd = x;
    printf("LCM = %d\\n", (a / gcd) * b);
    return 0;
}

14) Decimal to Binary

Decimal to Binary Conversion
#include <stdio.h>
int main() {
    int n, arr[32], i = 0, j;
    scanf("%d", &n);
    if (n == 0) { printf("0\\n"); return 0; }
    while (n > 0) {
        arr[i++] = n % 2;
        n /= 2;
    }
    for (j = i - 1; j >= 0; j--) printf("%d", arr[j]);
    printf("\\n");
    return 0;
}

15) Count Digits in a Number

Count Digits
#include <stdio.h>
int main() {
    int n, count = 0;
    scanf("%d", &n);
    if (n == 0) count = 1;
    while (n != 0) {
        count++;
        n /= 10;
    }
    printf("Digits = %d\\n", count);
    return 0;
}

16) Sum of Digits

Sum of Digits
#include <stdio.h>
int main() {
    int n, sum = 0;
    scanf("%d", &n);
    while (n != 0) {
        sum += n % 10;
        n /= 10;
    }
    printf("Sum of digits = %d\\n", sum);
    return 0;
}

17) Linear Search in Array

Linear Search
#include <stdio.h>
int main() {
    int n, i, key, pos = -1;
    scanf("%d", &n);
    int a[100];
    for (i = 0; i < n; i++) scanf("%d", &a[i]);
    scanf("%d", &key);
    for (i = 0; i < n; i++) {
        if (a[i] == key) { pos = i; break; }
    }
    if (pos != -1) printf("Found at index %d\\n", pos);
    else printf("Not found\\n");
    return 0;
}

18) Bubble Sort

Bubble Sort Algorithm
#include <stdio.h>
int main() {
    int n, i, j, t;
    scanf("%d", &n);
    int a[100];
    for (i = 0; i < n; i++) scanf("%d", &a[i]);
    for (i = 0; i < n - 1; i++) {
        for (j = 0; j < n - i - 1; j++) {
            if (a[j] > a[j + 1]) {
                t = a[j];
                a[j] = a[j + 1];
                a[j + 1] = t;
            }
        }
    }
    for (i = 0; i < n; i++) printf("%d ", a[i]);
    return 0;
}

19) Matrix Addition (2x2)

Matrix Addition
#include <stdio.h>
int main() {
    int a[2][2], b[2][2], c[2][2], i, j;
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &a[i][j]);
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            scanf("%d", &b[i][j]);
    for (i = 0; i < 2; i++)
        for (j = 0; j < 2; j++)
            c[i][j] = a[i][j] + b[i][j];
    for (i = 0; i < 2; i++) {
        for (j = 0; j < 2; j++) printf("%d ", c[i][j]);
        printf("\\n");
    }
    return 0;
}

20) String Length Without strlen()

String Length Without strlen()
#include <stdio.h>
int main() {
    char s[200];
    int i = 0;
    scanf("%199s", s);
    while (s[i] != '\\0') i++;
    printf("Length = %d\\n", i);
    return 0;
}
Key Takeaways
  • This page now includes 20 C programs with complete code examples.
  • Start with input/output, then loops, conditions, arrays, and strings.
  • Use functions to break larger logic into reusable blocks in real projects.
  • Always test edge cases such as zero, negative numbers, and empty input.
  • Use clear variable names and indentation to make C code easier to debug.
  • Compile with: gcc program.c -o program, then run: ./program

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.