Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
📘 Lesson  ·  Lesson 53

68 C Practice Programs

Written and reviewed by · Senior IT Faculty · 15+ years’ experience

Easy Formula Programs with User Input

Start with these programs first. Every program takes values from the user, applies one clear formula and prints the result. Copy the code, change the input and practise.

1. Add two numbers entered by the user

Reads two numbers and displays their sum.

Formula: Sum = first number + second number
C
#include <stdio.h>
int main() {
    double a;
    printf("Enter first number: ");
    scanf("%lf", &a);
    double b;
    printf("Enter second number: ");
    scanf("%lf", &b);
    double result = a + b;
    printf("Sum = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter first number: 25
Enter second number: 17
Sum = 42

2. Area of a rectangle

Uses length and breadth to calculate the rectangular area.

Formula: Area = length x breadth
C
#include <stdio.h>
int main() {
    double length;
    printf("Enter length: ");
    scanf("%lf", &length);
    double breadth;
    printf("Enter breadth: ");
    scanf("%lf", &breadth);
    double result = length * breadth;
    printf("Area = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter length: 12
Enter breadth: 5
Area = 60

3. Area of a circle

Reads the radius and calculates the area of a circle.

Formula: Area = pi x radius x radius
C
#include <stdio.h>
int main() {
    double radius;
    printf("Enter radius: ");
    scanf("%lf", &radius);
    double result = 3.14159 * radius * radius;
    printf("Area = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter radius: 7
Area = 153.938

4. Metres to kilometres

Converts a distance from metres to kilometres.

Formula: Kilometres = metres / 1000
C
#include <stdio.h>
int main() {
    double metres;
    printf("Enter distance in metres: ");
    scanf("%lf", &metres);
    double result = metres / 1000;
    printf("Kilometres = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter distance in metres: 2500
Kilometres = 2.5

5. Kilometres to metres

Converts a distance from kilometres to metres.

Formula: Metres = kilometres x 1000
C
#include <stdio.h>
int main() {
    double kilometres;
    printf("Enter distance in kilometres: ");
    scanf("%lf", &kilometres);
    double result = kilometres * 1000;
    printf("Metres = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter distance in kilometres: 3.5
Metres = 3500

6. Rupees to paise

Converts an amount in rupees into paise.

Formula: Paise = rupees x 100
C
#include <stdio.h>
int main() {
    double rupees;
    printf("Enter amount in rupees: ");
    scanf("%lf", &rupees);
    double result = rupees * 100;
    printf("Paise = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter amount in rupees: 125.50
Paise = 12550

7. Paise to rupees

Converts an amount in paise into rupees.

Formula: Rupees = paise / 100
C
#include <stdio.h>
int main() {
    double paise;
    printf("Enter amount in paise: ");
    scanf("%lf", &paise);
    double result = paise / 100;
    printf("Rupees = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter amount in paise: 8750
Rupees = 87.5

8. Celsius to Fahrenheit

Converts a temperature from Celsius to Fahrenheit.

Formula: F = (C x 9 / 5) + 32
C
#include <stdio.h>
int main() {
    double celsius;
    printf("Enter temperature in Celsius: ");
    scanf("%lf", &celsius);
    double result = (celsius * 9 / 5) + 32;
    printf("Fahrenheit = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter temperature in Celsius: 30
Fahrenheit = 86

9. Fahrenheit to Celsius

Converts a temperature from Fahrenheit to Celsius.

Formula: C = (F - 32) x 5 / 9
C
#include <stdio.h>
int main() {
    double fahrenheit;
    printf("Enter temperature in Fahrenheit: ");
    scanf("%lf", &fahrenheit);
    double result = (fahrenheit - 32) * 5 / 9;
    printf("Celsius = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter temperature in Fahrenheit: 98.6
Celsius = 37

10. Simple interest

Calculates simple interest from principal, rate and time.

Formula: SI = principal x rate x time / 100
C
#include <stdio.h>
int main() {
    double principal;
    printf("Enter principal: ");
    scanf("%lf", &principal);
    double rate;
    printf("Enter annual rate: ");
    scanf("%lf", &rate);
    double time;
    printf("Enter time in years: ");
    scanf("%lf", &time);
    double result = principal * rate * time / 100;
    printf("Simple interest = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter principal: 5000
Enter annual rate: 6
Enter time in years: 2
Simple interest = 600

11. Compound interest

Calculates annually compounded interest.

Formula: CI = P(1 + R/100)^T - P
C
#include <stdio.h>
#include <math.h>
int main() {
    double principal;
    printf("Enter principal: ");
    scanf("%lf", &principal);
    double rate;
    printf("Enter annual rate: ");
    scanf("%lf", &rate);
    double time;
    printf("Enter time in years: ");
    scanf("%lf", &time);
    double result = principal * pow(1 + rate / 100, time) - principal;
    printf("Compound interest = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter principal: 10000
Enter annual rate: 10
Enter time in years: 2
Compound interest = 2100

12. Area of a triangle

Uses base and height to calculate the triangle area.

Formula: Area = base x height / 2
C
#include <stdio.h>
int main() {
    double base;
    printf("Enter base: ");
    scanf("%lf", &base);
    double height;
    printf("Enter height: ");
    scanf("%lf", &height);
    double result = base * height / 2;
    printf("Area = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter base: 10
Enter height: 8
Area = 40

13. Perimeter of a rectangle

Adds all four sides of a rectangle.

Formula: Perimeter = 2 x (length + breadth)
C
#include <stdio.h>
int main() {
    double length;
    printf("Enter length: ");
    scanf("%lf", &length);
    double breadth;
    printf("Enter breadth: ");
    scanf("%lf", &breadth);
    double result = 2 * (length + breadth);
    printf("Perimeter = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter length: 12
Enter breadth: 5
Perimeter = 34

14. Circumference of a circle

Calculates the distance around a circle from its radius.

Formula: Circumference = 2 x pi x radius
C
#include <stdio.h>
int main() {
    double radius;
    printf("Enter radius: ");
    scanf("%lf", &radius);
    double result = 2 * 3.14159 * radius;
    printf("Circumference = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter radius: 7
Circumference = 43.9823

15. Total and percentage of five subjects

Reads five marks, then calculates total and percentage.

Formula: Percentage = total marks / 5
C
#include <stdio.h>
int main() {
    double m1;
    printf("Enter marks in subject 1: ");
    scanf("%lf", &m1);
    double m2;
    printf("Enter marks in subject 2: ");
    scanf("%lf", &m2);
    double m3;
    printf("Enter marks in subject 3: ");
    scanf("%lf", &m3);
    double m4;
    printf("Enter marks in subject 4: ");
    scanf("%lf", &m4);
    double m5;
    printf("Enter marks in subject 5: ");
    scanf("%lf", &m5);
    double result = (m1 + m2 + m3 + m4 + m5) / 5;
    printf("Total = %g\n", m1+m2+m3+m4+m5);
    printf("Percentage = %g\n", result);
    return 0;
}
Sample Input and Output:
Marks: 80 75 90 85 70
Total = 400
Percentage = 80

16. GST amount

Calculates GST and the final bill amount.

Formula: GST = amount x rate / 100
C
#include <stdio.h>
int main() {
    double amount;
    printf("Enter amount: ");
    scanf("%lf", &amount);
    double gstRate;
    printf("Enter GST rate: ");
    scanf("%lf", &gstRate);
    double result = amount * gstRate / 100;
    printf("GST = %g\n", result);
    printf("Final amount = %g\n", amount+result);
    return 0;
}
Sample Input and Output:
Enter amount: 2000
Enter GST rate: 18
GST = 360
Final amount = 2360

17. Body mass index (BMI)

Calculates BMI from weight in kilograms and height in metres.

Formula: BMI = weight / (height x height)
C
#include <stdio.h>
int main() {
    double weight;
    printf("Enter weight in kg: ");
    scanf("%lf", &weight);
    double height;
    printf("Enter height in metres: ");
    scanf("%lf", &height);
    double result = weight / (height * height);
    printf("BMI = %g\n", result);
    return 0;
}
Sample Input and Output:
Enter weight in kg: 72
Enter height in metres: 1.8
BMI = 22.2222

18. Square and cube of a number

Reads one number and prints both its square and cube.

Formula: Square = n x n; Cube = n x n x n
C
#include <stdio.h>
int main() {
    double number;
    printf("Enter a number: ");
    scanf("%lf", &number);
    double result = number * number;
    printf("Square = %g\n", result);
    printf("Cube = %g\n", number*number*number);
    return 0;
}
Sample Input and Output:
Enter a number: 5
Square = 25
Cube = 125

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.