Basic C Programs With Code and Output (2026 Edition)
Every C tutorial eventually points you at the same wall: write programs, lots of them, until the syntax stops feeling like a foreign language. This page is that wall, organized so you're not just copy-pasting code, you're climbing through it in order.
30 programs, grouped into 7 levels, each one compiled and actually run before going anywhere near this page (GCC 13, if you're curious). Every program gets a one-line problem statement, the code, the real output, and a note on what it's actually teaching you, not just what it does.
Type these out by hand at least the first time. Copy-pasting a for-loop teaches your fingers nothing.
How to Practice These C Programs
Quick setup before the programs start. You need two things: a compiler, and the discipline to not skip straight to Level 4 because patterns look fun.
• GCC is the standard choice. On Linux or macOS it's usually already there; on Windows, install it through MinGW, or check our guide to setting up a C compiler on Windows if that sentence made you nervous.
• An online compiler (Scaler's, or any similar one) works fine too for quick practice, no install needed.
• Compile with gcc filename.c -o filename, then run it with ./filename (or filename.exe on Windows).
New to programming logic entirely, not just C syntax? Scaler's free logic-building course for beginners is worth doing before this page, honestly. Syntax is teachable in an afternoon. Thinking in loops and conditions takes a bit longer.
And in case anyone's still asking whether C is worth the trouble in 2026: it's consistently sat in the top 2 to 3 languages on the TIOBE index for years, it's the standard first language across Indian engineering curricula, and it still runs the Linux kernel and most embedded systems on the planet. Old doesn't mean irrelevant here, it means load-bearing.
For the full C fundamentals before or alongside this practice set, our C programming tutorial hub covers the underlying concepts each level below assumes you've at least skimmed.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Level 1: Input, Output & Variables
The absolute starting line. printf, scanf, variables, basic arithmetic. Nobody skips this level, even people pretending they don't need it.
1. Hello World
The first program everyone writes, and for good reason, it confirms your compiler actually works.
Output:
Concept practiced: printf() and basic program structure (main, return 0).
###2. Sum of Two Numbers
Add two numbers and print the result.
Output:
Concept practiced: variable declaration, int type, and arithmetic operators.
3. Simple Interest
Calculate simple interest from principal, rate, and time.
Output:
Concept practiced: float precision and formatted output with %.2f.
4. Celsius to Fahrenheit
Convert a temperature from Celsius to Fahrenheit.
Output:
Concept practiced: operator precedence, why 9/5 without floats would silently give you 1, not 1.8.
Level 2: Operators & Expressions
Same building blocks, now doing actual decision-adjacent work. This is where the third variable in swap-two-numbers earns its keep.
Swap two variables using a temporary third variable.
Output:
Concept practiced: temp variables. Once you're comfortable, our swapping page also covers doing this without a third variable, using arithmetic or XOR.
6. Check Even or Odd
Check whether a number is even or odd using the modulus operator.
Output:
Concept practiced: the modulus operator (%) and your first if-else.
7. Largest of Three Numbers
Find the largest among three numbers.
Output:
Concept practiced: chained comparisons without nested if-else spaghetti.
8. ASCII Value of a Character
Print the ASCII value of a character.
Output:
Concept practiced: char is really just a small integer underneath, that's why %d works on it.
Level 3: Conditionals
Where if-else stops being a toy example and starts branching into real logic, switch statements included.
Check whether a given year is a leap year.
Output:
Concept practiced: compound conditions with && and ||, and the classic '%100 exception' every beginner forgets once.
10. Vowel or Consonant
Check if a character is a vowel or a consonant.
Output:
Concept practiced: long OR chains, and why a switch statement (next up) sometimes reads cleaner.
11. Simple Calculator (switch)
Build a basic calculator using a switch statement.
Output:
Concept practiced: switch-case syntax, and why every case needs a break unless you want fall-through on purpose.
12. Grade Calculator
Assign a letter grade based on marks using an if-else ladder.
Output:
Concept practiced: if-else ladders, and ordering conditions from highest to lowest so nothing gets caught early.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Level 4: Loops & Pattern Printing
The level where lab exams live and die. Loops, patterns, and the handful of programs interviewers ask about because they're small enough to whiteboard.
13. Multiplication Table
Print the multiplication table of a number using a for loop.
Output:
Concept practiced: the for loop's three parts (init, condition, increment) doing real work.
Calculate the factorial of a number using a loop.
Output:
Concept practiced: accumulator variables, and why factorial results outgrow int fast enough to need long.
15. Fibonacci Series
Print the first n terms of the Fibonacci series.
Output:
Concept practiced: tracking two running values instead of storing the whole sequence in an array. Our Fibonacci-via-recursion page shows the other common way to write this, worth comparing.
Check whether a number is prime.
Output:
Concept practiced: the i*i <= num trick, checking up to the square root instead of all the way to num, is the actual optimisation, not a stylistic choice.
Try this next: reverse the digits of a number using the same %10 and /10 trick you'll see again in Level 6. Full walkthrough on our reverse a number in C page.
17. Right Triangle Pattern
Print a right-angled triangle of stars.
Output:
Concept practiced: nested loops, the outer one controls rows, the inner one controls what gets printed per row.
18. Pyramid Pattern
Print a centered pyramid of stars.
Output:
Concept practiced: two inner loops doing two separate jobs, spacing then stars, in the same row. Patterns are the single most-asked lab-exam category, and this page only scratches the surface. Our dedicated pattern programs page covers 20-plus more, including Floyd's triangle and diamond patterns.
Level 5: Arrays & Strings
Single variables run out of road fast. Arrays and strings are where C programs start looking like programs instead of calculator scripts.
19. Largest Element in an Array
Find the largest element in an array.
Output:
Concept practiced: array indexing, and looping from index 1 since arr[0] is already your starting guess.
20. Bubble Sort
Sort an array in ascending order using bubble sort.
#include <stdio.h>
Output:
Concept practiced: nested loops with adjacent-swap logic. It's O(n²) and nobody uses it in production, but it's the clearest way to actually see sorting happen.
21. Binary Search
Search for a value in a sorted array using binary search.
Output:
Concept practiced: why binary search needs a sorted array first, and how it eliminates half the search space every step.
Check whether a string reads the same forwards and backwards.
Output:
Concept practiced: comparing characters from both ends inward, and only needing to check half the string.
23. Reverse a String
Print a string in reverse.
Concept practiced: strlen() and walking a string backward by index instead of using a library shortcut.
Turn Learning into Career Growth
Level 6: Functions & Recursion
Breaking logic into reusable functions, then the specific flavor of function that calls itself. If recursion has never quite clicked, this is usually where it does, mostly because you've already written the iterative version of factorial two levels ago and can compare directly.
24. Factorial (Recursive)
Calculate factorial using a recursive function instead of a loop.
Output:
Concept practiced: base cases and the call stack. Same answer as Level 4's loop version, different mechanism entirely.
Quick side-by-side, since this is the comparison that actually explains why recursion isn't just "the fancy way to write a loop":
| Iterative (Level 4) | Recursive (above) | |
|---|---|---|
| How it works | One variable gets updated in a loop | Function calls a smaller version of itself |
| Memory use | Constant, one stack frame | Grows with n, one stack frame per call |
| Readability | Familiar, a bit mechanical | Mirrors the mathematical definition directly |
25. Armstrong Number
Check whether a number is an Armstrong number (sum of its own digits, each raised to the power of the digit count, equals the number).
Output:
Concept practiced: combining digit extraction with pow(), and why compiling this needs -lm linked for math.h.
26. GCD Using Euclid's Algorithm
Find the greatest common divisor of two numbers recursively.
Output:
Concept practiced: Euclid's algorithm in about four lines, arguably the most elegant recursive function most beginners ever write.
27. Sum of Digits (Recursive)
Sum the digits of a number using recursion.
Output:
Concept practiced: peeling off one digit per recursive call until n hits zero, the base case doing the actual stopping.
Level 7: Pointers, Structures & 2D Arrays
The bridge to "real" C, the stuff that actually separates C from most beginner-friendly languages. Pointers trip people up mostly because the syntax looks scarier than the idea underneath it.
28. Swap Using Pointers
Swap two variables using pointers instead of returning values.
Output:
Before swap: a \= 7, b \= 19
After swap: a \= 19, b \= 7
```abc
***Concept practiced:** the actual reason pointers exist: swap() can't modify a's and b's real values without their addresses. Compare this to Level 2's version, same problem, genuinely different mechanism.*
**29\. Student Record Using a Structure**
Store and print a student's details using a struct.
```abc
\#include \<stdio.h\>
struct Student {
char name\[30\];
int roll;
float marks;
};
int main(void) {
struct Student s1 \= {"Rahul", 21, 89.5};
printf("Name: %s\\n", s1.name);
printf("Roll No: %d\\n", s1.roll);
printf("Marks: %.1f\\n", s1.marks);
return 0;
}
Output:
Concept practiced: bundling related data of different types into one struct, instead of juggling three separate arrays.
Multiply two 2x2 matrices.
Output:
Concept practiced: three nested loops, 2D array indexing, and why matrix multiplication is O(n³) in its naive form.
What to Practice Next (Roadmap)
Finishing these 30 doesn't make you done, it makes you ready for the part that actually gets asked about in interviews: data structures.
• Arrays you've already touched, next go deeper: dynamic arrays, multi-dimensional arrays beyond a 2x2 toy matrix.
• Linked lists, the first real pointer-heavy data structure, and usually the one that separates "knows C syntax" from "can build things in C."
• Stacks and queues, built on arrays or linked lists, foundational for almost everything after.
• Trees and graphs, once the linear structures feel comfortable.
For exact function signatures and standard library behaviour while you're building any of this, cppreference is the reference worth bookmarking over random Stack Overflow answers.
Ready to go from C basics to data structures and real interviews? Explore Scaler's Software Development Program.
FAQs
What are the basic programs in C? Classic starters: Hello World, sum of two numbers, swap, even or odd, leap year, factorial, Fibonacci series, prime check, palindrome, pattern printing, and array or string operations, roughly the order this page follows.
How do I start practicing C programs as a beginner? Set up GCC or an online compiler, then work through the levels in order: I/O, operators, conditionals, loops, arrays and strings, functions and recursion, pointers. Type every program yourself, don't copy-paste, your fingers need the repetition more than your eyes do.
How long does it take to learn basic C programming? With daily practice, most beginners get through basic programs like these in 2 to 4 weeks. Comfortable, independent problem-solving usually takes another 2 to 3 months on top of that.
Is C still worth learning in 2026? Yes. It consistently ranks in the top languages on the TIOBE index, underpins operating systems and embedded systems, and remains the standard first language in most Indian engineering curricula.
Which compiler should I use to run C programs? GCC, via MinGW on Windows or built-in on Linux and macOS, or any online compiler for quick practice without installing anything.
What comes after basic C programs? Data structures next, arrays into linked lists into stacks and queues into trees, then algorithm practice on top of that. That combination is the actual foundation for coding interviews.




