Introduction to C Programming: 7 Basic C Programs with Output for Beginners
C programming is one of the most popular and powerful programming languages for beginners. It was developed by Dennis Ritchie at Bell Laboratories. C is often called a foundation programming language because many modern programming languages such as C++, Java, and others have been influenced by C.
C programming is widely used in system programming, operating systems, embedded systems, application development, and software development. Learning basic C programs helps students understand important programming concepts such as variables, data types, input and output, operators, expressions, and conditional statements.
📑 C Programs Covered in This Tutorial
- Hello World Program in C
- Sum of Two Numbers in C
- Average of Three Numbers in C
- Arithmetic Operations on Two Numbers in C
- Simple Interest Program in C
- Temperature Conversion Program in C
- Maximum of Three Numbers Using Conditional Operator
1. Program to Display Hello Message in C
The following is a simple Hello World program in C. It uses the printf() function to display a message on the screen.
📌 C Program
#include <stdio.h>
int main()
{
printf("Hello, Welcome to C Programming!");
return 0;
}
💻 Output
Hello, Welcome to C Programming!
2. Program to Find Sum of Two Numbers in C
This C program takes two numbers as input from the user using the scanf() function and calculates their sum using the addition operator +.
📌 C Program
#include <stdio.h>
int main()
{
int num1, num2, sum;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum = %d", sum);
return 0;
}
💻 Sample Output
Enter first number: 10 Enter second number: 20 Sum = 30
3. Program to Find Average of Three Numbers in C
This program accepts three numbers from the user and calculates their average. The float data type is used to store decimal values.
📌 Formula
📌 C Program
#include <stdio.h>
int main()
{
float num1, num2, num3, average;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
average = (num1 + num2 + num3) / 3;
printf("Average = %.2f", average);
return 0;
}
💻 Sample Output
Enter three numbers: 10 20 30 Average = 20.00
4. Program to Perform All Four Arithmetic Operations in C
This program performs the four basic arithmetic operations on two numbers: addition, subtraction, multiplication, and division.
📌 Arithmetic Operators Used in C
- + for Addition
- - for Subtraction
- * for Multiplication
- / for Division
📌 C Program
#include <stdio.h>
int main()
{
float num1, num2;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
printf("\nAddition = %.2f", num1 + num2);
printf("\nSubtraction = %.2f", num1 - num2);
printf("\nMultiplication = %.2f", num1 * num2);
if(num2 != 0)
printf("\nDivision = %.2f", num1 / num2);
else
printf("\nDivision is not possible because divisor is zero.");
return 0;
}
💻 Sample Output
Enter two numbers: 20 5 Addition = 25.00 Subtraction = 15.00 Multiplication = 100.00 Division = 4.00
5. Program to Find Simple Interest in C
This C program calculates simple interest by taking the principal amount, rate of interest, and time from the user.
📌 Simple Interest Formula
📌 C Program
#include <stdio.h>
int main()
{
float principal, rate, time, simpleInterest;
printf("Enter Principal Amount: ");
scanf("%f", &principal);
printf("Enter Rate of Interest: ");
scanf("%f", &rate);
printf("Enter Time in Years: ");
scanf("%f", &time);
simpleInterest = (principal * rate * time) / 100;
printf("Simple Interest = %.2f", simpleInterest);
return 0;
}
💻 Sample Output
Enter Principal Amount: 10000 Enter Rate of Interest: 5 Enter Time in Years: 2 Simple Interest = 1000.00
6. Program for Temperature Conversion in C
This program converts temperature from Celsius to Fahrenheit.
📌 Temperature Conversion Formula
📌 C Program
#include <stdio.h>
int main()
{
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
fahrenheit = (celsius * 9 / 5) + 32;
printf("Temperature in Fahrenheit = %.2f", fahrenheit);
return 0;
}
💻 Sample Output
Enter temperature in Celsius: 25 Temperature in Fahrenheit = 77.00
7. Program to Find Maximum of Three Numbers Using Conditional Operator in C
This program finds the largest number among three numbers using the conditional operator. The conditional operator ? : is also known as the ternary operator in C programming.
📌 Syntax of Conditional Operator
condition ? expression1 : expression2;
📌 C Program
#include <stdio.h>
int main()
{
int a, b, c, max;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
max = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("Maximum number = %d", max);
return 0;
}
💻 Sample Output
Enter three numbers: 10 25 18 Maximum number = 25
📚 Summary of Basic C Programs
These basic C programs are useful for beginners who are learning C programming and preparing for programming practicals, assignments, viva questions, and coding practice.
| Program No. | C Program Name |
|---|---|
| 1 | Hello World Program in C |
| 2 | Sum of Two Numbers in C |
| 3 | Average of Three Numbers in C |
| 4 | Four Arithmetic Operations in C |
| 5 | Simple Interest Program in C |
| 6 | Temperature Conversion Program in C |
| 7 | Maximum of Three Numbers Using Conditional Operator in C |
Program to Calculate the Area of a Circle
Aim
To write a C program to calculate the area of a circle.
Formula
Area of Circle = π × r × r
Where r is the radius of the circle and π = 3.14159.
C Program
#include <stdio.h>
int main()
{
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = 3.14159 * radius * radius;
printf("Area of the circle = %.2f", area);
return 0;
}
Sample Output
Enter the radius of the circle: 5 Area of the circle = 78.54
Online Area of Circle Calculator
Enter the radius below to calculate the area of the circle:
Example
If the radius of the circle is 5 units:
Area = π × r × r
= 3.14159 × 5 × 5
= 78.54 square units
C Program to Find Square Root Without Using sqrt() Function
In this C program, we will learn how to find the square root of a number without using the built-in sqrt() function. This is useful for beginners to understand how a square root can be calculated using a simple loop and approximation.
What is a Square Root?
The square root of a number is a value which, when multiplied by itself, gives the original number.
Example:
Therefore, √25 = 5
C Program
The following program finds the square root approximately without using sqrt().
#include <stdio.h>
int main()
{
float num, i;
printf("Enter a number: ");
scanf("%f", &num);
i = 0;
while (i * i <= num)
{
i = i + 0.01;
}
printf("Square root of %.2f is approximately %.2f", num, i);
return 0;
}
How Does the Program Work?
Suppose the user enters:
Initially:
i = 0
The while loop checks whether:
i * i <= num
The value of i is gradually increased:
0.00
0.01
0.02
0.03
...
4.98
4.99
5.00
When i = 5:
5 × 5 = 25
Therefore, the square root is approximately 5.
What Does i = i + 0.01 Mean?
The statement:
i = i + 0.01;
means that 0.01 is added to the current value of i each time the loop runs.
For example:
i = 0
i = 0.01
i = 0.02
i = 0.03
...
i = 4.99
i = 5.00
What If We Use i = i + 0.1?
We can also write:
i = i + 0.1;
In this case, the value increases by 0.1 each time:
0
0.1
0.2
0.3
0.4
...
4.8
4.9
5.0
This makes the program faster because fewer iterations are required, but the answer is less precise.
0.1 vs 0.01
| Increment | Accuracy | Speed |
|---|---|---|
| i = i + 1 | Low | Fast |
| i = i + 0.1 | About 1 decimal place | Faster |
| i = i + 0.01 | About 2 decimal places | Slower |
| i = i + 0.001 | About 3 decimal places | More iterations |
Example Output
Enter a number: 25
Square root of 25.00 is approximately 5.01
The result may show 5.01 because the loop stops after the value becomes slightly greater than the actual square root.
Key Points
- No
sqrt()function is used. - The program uses a
whileloop. i * iis used to check the square of the current value.i = i + 0.01gradually increases the value ofi.- A smaller increment provides better approximation but requires more iterations.
Conclusion
This simple C program demonstrates how a square root can be approximated without using the built-in
sqrt() function. It is a good beginner example for understanding
loops, floating-point numbers, conditions, and approximation.
C Program to Calculate Electricity Bill
In this C program, we will learn how to calculate an electricity bill based on the number of units consumed. The program uses if-else conditions to apply different rates for different ranges of electricity consumption.
What is an Electricity Bill?
An electricity bill is generally calculated according to the number of electricity units consumed. Different unit ranges may have different rates.
For this example, we will use the following rates:
| Units Consumed | Rate per Unit |
|---|---|
| 0 – 100 units | ₹5 |
| 101 – 200 units | ₹7 |
| 201 – 300 units | ₹10 |
| Above 300 units | ₹12 |
These rates are used only for learning the C programming concept. Actual electricity tariffs vary by electricity provider and consumer category.
C Program
#include <stdio.h>
int main()
{
int units;
float bill;
printf("Enter electricity units consumed: ");
scanf("%d", &units);
if (units <= 100)
{
bill = units * 5;
}
else if (units <= 200)
{
bill = (100 * 5) + ((units - 100) * 7);
}
else if (units <= 300)
{
bill = (100 * 5) + (100 * 7) + ((units - 200) * 10);
}
else
{
bill = (100 * 5) + (100 * 7) + (100 * 10)
+ ((units - 300) * 12);
}
printf("Electricity Bill = Rs. %.2f", bill);
return 0;
}
How Does the Program Work?
First, the program asks the user to enter the number of electricity units consumed.
Enter electricity units consumed: 250
The program then checks the number of units using if-else if-else statements.
Example: 250 Units
Suppose the consumer has used 250 units.
The calculation will be:
First 100 units:
100 × ₹5 = ₹500
Next 100 units:
100 × ₹7 = ₹700
Remaining 50 units:
50 × ₹10 = ₹500
Total Bill:
₹500 + ₹700 + ₹500 = ₹1700
Example Output
Enter electricity units consumed: 250
Electricity Bill = Rs. 1700.00
Understanding the Condition
The first condition checks whether the units are less than or equal to 100:
if (units <= 100)
{
bill = units * 5;
}
If the units are between 101 and 200, the first 100 units are charged at ₹5 and the remaining units are charged at ₹7.
bill = (100 * 5) + ((units - 100) * 7);
Similarly, for units between 201 and 300, the program calculates the charges for the first 100 units, the next 100 units, and then the remaining units separately.
Why Do We Use (units - 100)?
Suppose the user enters 150 units.
The first 100 units are already charged at ₹5. Therefore, only the remaining 50 units need to be charged at ₹7.
150 - 100 = 50
Therefore:
100 × ₹5 = ₹500
50 × ₹7 = ₹350
Total = ₹850
Key Points
- The program takes electricity consumption in units as input.
if-else if-elseis used to select the appropriate slab.- Each electricity slab has a different rate.
- The bill is calculated progressively for higher unit consumption.
%.2fdisplays the bill with two digits after the decimal point.
Conclusion
This C program is a useful beginner example for understanding if-else conditions, arithmetic operators, user input, and slab-based calculations. Similar logic can be used in programs for calculating water bills, telephone bills, income tax, and other charges.
C Program to Print Numbers from 1 to N Using While Loop
In this C program, we will take a number N from the user and print all the numbers from 1 to N using a while loop.
C Program
#include <stdio.h>
int main()
{
int n, i = 1;
printf("Enter the value of n: ");
scanf("%d", &n);
while (i <= n)
{
printf("%d ", i);
i++;
}
return 0;
}
How the Program Works
- The user enters the value of n.
- The variable i is initialized to 1.
- The while loop checks whether i <= n.
- If the condition is true, the value of i is printed.
- The statement i++ increases the value of i by 1.
- The loop continues until i becomes greater than n.
Sample Output
Enter the value of n: 10 1 2 3 4 5 6 7 8 9 10
Dry Run
| i | Condition (i <= n) | Output | i++ |
|---|---|---|---|
| 1 | 1 <= 10 → True | 1 | 2 |
| 2 | 2 <= 10 → True | 2 | 3 |
| 3 | 3 <= 10 → True | 3 | 4 |
| ... | ... | ... | ... |
| 10 | 10 <= 10 → True | 10 | 11 |
| 11 | 11 <= 10 → False | Loop stops | - |
Important Points
- while is an entry-controlled loop.
- The loop condition is checked before executing the loop body.
- i++ is necessary; otherwise, the loop may become an infinite loop.
- The program prints numbers starting from 1 up to the value entered by the user.
Conclusion
This is a simple example of using a while loop in C. It is useful for understanding initialization, condition checking, and incrementing a loop variable.
🎯 Practice is the Key to Learning C Programming!
These basic C programs help beginners understand important concepts such as input and output, variables, arithmetic operators, conditional operators, and mathematical calculations in C programming. Try changing the input values and experiment with these programs to improve your programming skills.