📘 Lesson  ·  Lesson 53

50 C Practice Programs

About

A collection of 50 important C practice programs every beginner should try — covering calculations, decisions, loops, patterns and arrays. Each program is short, complete and shows the expected output.

Basic Calculation Programs

1. Add two numbers
#include <stdio.h>
int main(){
    int a=5, b=3;
    printf("Sum = %d", a+b);  // 8
    return 0;
}
2. Subtract two numbers
#include <stdio.h>
int main(){
    int a=10, b=4;
    printf("Diff = %d", a-b);  // 6
    return 0;
}
3. Multiply two numbers
#include <stdio.h>
int main(){
    int a=6, b=7;
    printf("Product = %d", a*b);  // 42
    return 0;
}
4. Divide two numbers
#include <stdio.h>
int main(){
    float a=10, b=4;
    printf("Result = %.2f", a/b);  // 2.50
    return 0;
}
5. Find remainder (modulus)
#include <stdio.h>
int main(){
    int a=17, b=5;
    printf("Remainder = %d", a%b);  // 2
    return 0;
}
6. Area of rectangle
#include <stdio.h>
int main(){
    int l=8, w=3;
    printf("Area = %d", l*w);  // 24
    return 0;
}
7. Area of circle
#include <stdio.h>
int main(){
    float r=7;
    printf("Area = %.2f", 3.14*r*r);  // 153.86
    return 0;
}
8. Simple interest
#include <stdio.h>
int main(){
    float p=1000, r=5, t=2;
    printf("SI = %.2f", (p*r*t)/100);  // 100.00
    return 0;
}
9. Average of three numbers
#include <stdio.h>
int main(){
    int a=10, b=20, c=30;
    printf("Avg = %.2f", (a+b+c)/3.0);  // 20.00
    return 0;
}
10. Swap two numbers
#include <stdio.h>
int main(){
    int a=5, b=9, t;
    t=a; a=b; b=t;
    printf("a=%d b=%d", a, b);  // a=9 b=5
    return 0;
}

If-Else and If-Else-If Programs

1. Check even or odd
#include <stdio.h>
int main(){
    int n=7;
    if(n%2==0) printf("Even");
    else printf("Odd");  // Odd
    return 0;
}
2. Largest of two numbers
#include <stdio.h>
int main(){
    int a=12, b=20;
    if(a>b) printf("%d", a);
    else printf("%d", b);  // 20
    return 0;
}
3. Largest of three numbers
#include <stdio.h>
int main(){
    int a=5,b=9,c=3;
    if(a>=b && a>=c) printf("%d",a);
    else if(b>=c) printf("%d",b);
    else printf("%d",c);  // 9
    return 0;
}
4. Positive, negative or zero
#include <stdio.h>
int main(){
    int n=-4;
    if(n>0) printf("Positive");
    else if(n<0) printf("Negative");
    else printf("Zero");  // Negative
    return 0;
}
5. Check pass or fail
#include <stdio.h>
int main(){
    int marks=45;
    if(marks>=33) printf("Pass");
    else printf("Fail");  // Pass
    return 0;
}
6. Grade using if-else-if
#include <stdio.h>
int main(){
    int m=82;
    if(m>=90) printf("A+");
    else if(m>=75) printf("A");
    else if(m>=33) printf("Pass");
    else printf("Fail");  // A
    return 0;
}
7. Check leap year
#include <stdio.h>
int main(){
    int y=2024;
    if((y%4==0 && y%100!=0)||y%400==0) printf("Leap");
    else printf("Not Leap");  // Leap
    return 0;
}
8. Check vowel or consonant
#include <stdio.h>
int main(){
    char ch='e';
    if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') printf("Vowel");
    else printf("Consonant");  // Vowel
    return 0;
}
9. Check divisible by 5 and 11
#include <stdio.h>
int main(){
    int n=55;
    if(n%5==0 && n%11==0) printf("Yes");
    else printf("No");  // Yes
    return 0;
}
10. Largest using ternary
#include <stdio.h>
int main(){
    int a=7, b=4;
    printf("%d", (a>b)?a:b);  // 7
    return 0;
}

Loop Programs

1. Print 1 to 10
#include <stdio.h>
int main(){
    for(int i=1;i<=10;i++)
      printf("%d ", i);  // 1 2 3 4 5 6 7 8 9 10
    return 0;
}
2. Sum of first N numbers
#include <stdio.h>
int main(){
    int n=5, sum=0;
    for(int i=1;i<=n;i++) sum+=i;
    printf("Sum = %d", sum);  // 15
    return 0;
}
3. Factorial of a number
#include <stdio.h>
int main(){
    int n=5, f=1;
    for(int i=1;i<=n;i++) f*=i;
    printf("Factorial = %d", f);  // 120
    return 0;
}
4. Multiplication table
#include <stdio.h>
int main(){
    int n=5;
    for(int i=1;i<=10;i++)
      printf("%d x %d = %d\n", n, i, n*i);
    return 0;
}
5. Print even numbers 1-20
#include <stdio.h>
int main(){
    for(int i=2;i<=20;i+=2)
      printf("%d ", i);  // 2 4 6 ... 20
    return 0;
}
6. Reverse a number
#include <stdio.h>
int main(){
    int n=1234, rev=0;
    while(n>0){ rev=rev*10+n%10; n/=10; }
    printf("%d", rev);  // 4321
    return 0;
}
7. Sum of digits
#include <stdio.h>
int main(){
    int n=123, s=0;
    while(n>0){ s+=n%10; n/=10; }
    printf("Sum = %d", s);  // 6
    return 0;
}
8. Count digits
#include <stdio.h>
int main(){
    int n=98765, c=0;
    while(n>0){ c++; n/=10; }
    printf("Digits = %d", c);  // 5
    return 0;
}
9. Check prime number
#include <stdio.h>
int main(){
    int n=13, p=1;
    for(int i=2;i<n;i++) if(n%i==0) p=0;
    printf(p?"Prime":"Not Prime");  // Prime
    return 0;
}
10. Fibonacci series
#include <stdio.h>
int main(){
    int a=0,b=1,c;
    for(int i=0;i<7;i++){ printf("%d ",a); c=a+b; a=b; b=c; }
    // 0 1 1 2 3 5 8
    return 0;
}

Pattern Programs

1. Square of stars
#include <stdio.h>
int main(){
    for(int i=1;i<=4;i++){
      for(int j=1;j<=4;j++) printf("* ");
      printf("\n");
    }
    return 0;
}
2. Right triangle of stars
#include <stdio.h>
int main(){
    for(int i=1;i<=5;i++){
      for(int j=1;j<=i;j++) printf("* ");
      printf("\n");
    }
    return 0;
}
3. Inverted triangle
#include <stdio.h>
int main(){
    for(int i=5;i>=1;i--){
      for(int j=1;j<=i;j++) printf("* ");
      printf("\n");
    }
    return 0;
}
4. Number triangle
#include <stdio.h>
int main(){
    for(int i=1;i<=5;i++){
      for(int j=1;j<=i;j++) printf("%d ", j);
      printf("\n");
    }
    return 0;
}
5. Repeated number triangle
#include <stdio.h>
int main(){
    for(int i=1;i<=5;i++){
      for(int j=1;j<=i;j++) printf("%d ", i);
      printf("\n");
    }
    return 0;
}
6. Right-aligned triangle
#include <stdio.h>
int main(){
    for(int i=1;i<=4;i++){
      for(int j=i;j<4;j++) printf("  ");
      for(int k=1;k<=i;k++) printf("* ");
      printf("\n");
    }
    return 0;
}
7. Pyramid of stars
#include <stdio.h>
int main(){
    int n=4;
    for(int i=1;i<=n;i++){
      for(int j=i;j<n;j++) printf(" ");
      for(int k=1;k<=2*i-1;k++) printf("*");
      printf("\n");
    }
    return 0;
}
8. Alphabet triangle
#include <stdio.h>
int main(){
    for(int i=1;i<=5;i++){
      for(char c='A';c<'A'+i;c++) printf("%c ", c);
      printf("\n");
    }
    return 0;
}
9. Diamond pattern
#include <stdio.h>
int main(){
    int n=3;
    for(int i=1;i<=n;i++){ for(int j=i;j<n;j++)printf(" "); for(int k=1;k<=2*i-1;k++)printf("*"); printf("\n"); }
    for(int i=n-1;i>=1;i--){ for(int j=n;j>i;j--)printf(" "); for(int k=1;k<=2*i-1;k++)printf("*"); printf("\n"); }
    return 0;
}
10. Hollow square
#include <stdio.h>
int main(){
    int n=4;
    for(int i=1;i<=n;i++){
      for(int j=1;j<=n;j++){
        if(i==1||i==n||j==1||j==n) printf("* ");
        else printf("  ");
      }
      printf("\n");
    }
    return 0;
}

Array Programs

1. Sum of array elements
#include <stdio.h>
int main(){
    int a[]={10,20,30,40}, sum=0;
    for(int i=0;i<4;i++) sum+=a[i];
    printf("Sum = %d", sum);  // 100
    return 0;
}
2. Largest in array
#include <stdio.h>
int main(){
    int a[]={3,7,2,9,4}, max=a[0];
    for(int i=1;i<5;i++) if(a[i]>max) max=a[i];
    printf("Max = %d", max);  // 9
    return 0;
}
3. Smallest in array
#include <stdio.h>
int main(){
    int a[]={3,7,2,9,4}, min=a[0];
    for(int i=1;i<5;i++) if(a[i]<min) min=a[i];
    printf("Min = %d", min);  // 2
    return 0;
}
4. Average of array
#include <stdio.h>
int main(){
    int a[]={10,20,30}, s=0;
    for(int i=0;i<3;i++) s+=a[i];
    printf("Avg = %.2f", s/3.0);  // 20.00
    return 0;
}
5. Reverse an array
#include <stdio.h>
int main(){
    int a[]={1,2,3,4,5};
    for(int i=4;i>=0;i--) printf("%d ", a[i]);  // 5 4 3 2 1
    return 0;
}
6. Count even numbers
#include <stdio.h>
int main(){
    int a[]={1,2,3,4,5,6}, c=0;
    for(int i=0;i<6;i++) if(a[i]%2==0) c++;
    printf("Even count = %d", c);  // 3
    return 0;
}
7. Search element (linear)
#include <stdio.h>
int main(){
    int a[]={5,8,12,3}, key=12, pos=-1;
    for(int i=0;i<4;i++) if(a[i]==key) pos=i;
    printf("Found at %d", pos);  // 2
    return 0;
}
8. Copy array to another
#include <stdio.h>
int main(){
    int a[]={1,2,3}, b[3];
    for(int i=0;i<3;i++) b[i]=a[i];
    printf("%d %d %d", b[0],b[1],b[2]);  // 1 2 3
    return 0;
}
9. Sort array (ascending)
#include <stdio.h>
int main(){
    int a[]={5,2,8,1}, n=4;
    for(int i=0;i<n;i++)for(int j=0;j<n-i-1;j++)
      if(a[j]>a[j+1]){int t=a[j];a[j]=a[j+1];a[j+1]=t;}
    for(int i=0;i<n;i++) printf("%d ",a[i]);  // 1 2 5 8
    return 0;
}
10. Count positive and negative
#include <stdio.h>
int main(){
    int a[]={-1,2,-3,4,5}, p=0,n=0;
    for(int i=0;i<5;i++){ if(a[i]>=0)p++; else n++; }
    printf("Pos=%d Neg=%d", p, n);  // Pos=3 Neg=2
    return 0;
}

Frequently Asked Questions

Why should I practise C programs?
Practising programs turns theory into skill: you only truly understand loops, arrays, functions and pointers once you write working code with them. Regular practice also builds the speed and confidence needed for exams, interviews and real projects.
What are good C programs for beginners to practise?
Good starters include adding two numbers, checking even or odd, finding a factorial, reversing a number, checking for a prime or palindrome, printing patterns, and finding the largest element in an array. Each reinforces a core concept while staying small enough to finish.
How should I practise coding to improve fastest?
Try to write each program yourself before looking at a solution, run it, and fix your own errors. Tracing the logic by hand with a small input and then modifying the program slightly deepens understanding far more than copying code.
Are C practice programs useful for interviews?
Yes. Many interview and viva questions are variations of classic programs like prime check, string reversal, sorting and recursion. Being able to write and explain them from memory is exactly what interviewers want to see.
How many C programs should a beginner solve?
There is no magic number, but carefully solving several dozen varied programs across calculations, decisions, loops, patterns and arrays gives most beginners solid command of the basics. Understanding each program fully matters more than rushing through many.
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.