💻 Online C++ Compiler
Write • Compile • Run • Get Output
- Write your C++ program in the editor.
- Enter input in the I/O section if required.
- Press the green RUN ▶ button below.
- Check the output in Console.
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.
The following is a simple Hello World program in C. It uses the printf() function to display a message on the screen.
#include <stdio.h>
int main()
{
printf("Hello, Welcome to C Programming!");
return 0;
}
Hello, Welcome to C Programming!
This C program takes two numbers as input from the user using the scanf() function and calculates their sum using the addition operator +.
#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;
}
Enter first number: 10 Enter second number: 20 Sum = 30
This program accepts three numbers from the user and calculates their average. The float data type is used to store decimal values.
#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;
}
Enter three numbers: 10 20 30 Average = 20.00
This program performs the four basic arithmetic operations on two numbers: addition, subtraction, multiplication, and division.
#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;
}
Enter two numbers: 20 5 Addition = 25.00 Subtraction = 15.00 Multiplication = 100.00 Division = 4.00
This C program calculates simple interest by taking the principal amount, rate of interest, and time from the user.
#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;
}
Enter Principal Amount: 10000 Enter Rate of Interest: 5 Enter Time in Years: 2 Simple Interest = 1000.00
This program converts temperature from Celsius to Fahrenheit.
#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;
}
Enter temperature in Celsius: 25 Temperature in Fahrenheit = 77.00
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.
condition ? expression1 : expression2;
#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;
}
Enter three numbers: 10 25 18 Maximum number = 25
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 |
To write a C program to calculate the area of a circle.
Area of Circle = Ï€ × r × r
Where r is the radius of the circle and π = 3.14159.
#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;
}
Enter the radius of the circle: 5 Area of the circle = 78.54
Enter the radius below to calculate the area of the circle:
If the radius of the circle is 5 units:
Area = Ï€ × r × r
= 3.14159 × 5 × 5
= 78.54 square units
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.
The square root of a number is a value which, when multiplied by itself, gives the original number.
Example:
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;
}
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.
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
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.
| 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 |
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.
sqrt() function is used.while loop.i * i is used to check the square of the current value.i = i + 0.01 gradually increases the value of i.
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.
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.
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.
#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;
}
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.
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
Enter electricity units consumed: 250
Electricity Bill = Rs. 1700.00
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.
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
if-else if-else is used to select the appropriate slab.%.2f displays the bill with two digits after the decimal point.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.
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.
#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;
}
Enter the value of n: 10 1 2 3 4 5 6 7 8 9 10
| 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 | - |
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.
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.
The if-else statement is used in C programming to make decisions. It checks a condition and executes a particular block of code depending on whether the condition is true or false.
This program takes the age of a person and checks whether the person is greater than 18 years old.
#include <stdio.h>
int main()
{
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age > 18)
{
printf("You are greater than 18 years old.");
}
else
{
printf("You are 18 years old or younger.");
}
return 0;
}
Enter your age: 21 You are greater than 18 years old.
A nested if-else means using one if-else statement inside another if-else statement.
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num >= 0)
{
if (num == 0)
{
printf("The number is zero.");
}
else
{
printf("The number is positive.");
}
}
else
{
printf("The number is negative.");
}
return 0;
}
Enter a number: 15 The number is positive.
Enter a number: -8 The number is negative.
Enter a number: 0 The number is zero.
The else-if ladder is useful when we have multiple conditions to check. The following program displays a grade according to the marks obtained.
#include <stdio.h>
int main()
{
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);
if (marks >= 90)
{
printf("Grade A");
}
else if (marks >= 80)
{
printf("Grade B");
}
else if (marks >= 70)
{
printf("Grade C");
}
else if (marks >= 60)
{
printf("Grade D");
}
else if (marks >= 40)
{
printf("Grade E");
}
else
{
printf("Fail");
}
return 0;
}
Enter your marks: 85 Grade B
The && operator is called the logical AND operator. Both conditions must be true for the complete condition to be true.
Example: A student is eligible for admission if the marks are 50 or more and the age is 18 or more.
#include <stdio.h>
int main()
{
int marks, age;
printf("Enter your marks: ");
scanf("%d", &marks);
printf("Enter your age: ");
scanf("%d", &age);
if (marks >= 50 && age >= 18)
{
printf("You are eligible.");
}
else
{
printf("You are not eligible.");
}
return 0;
}
Enter your marks: 65 Enter your age: 20 You are eligible.
The || operator is called the logical OR operator. If at least one of the conditions is true, the complete condition becomes true.
Example: A student can participate if they have a valid ID card or a valid registration number.
#include <stdio.h>
int main()
{
int id, registration;
printf("Enter ID card status (1 for Yes, 0 for No): ");
scanf("%d", &id);
printf("Enter registration status (1 for Yes, 0 for No): ");
scanf("%d", ®istration);
if (id == 1 || registration == 1)
{
printf("You can participate.");
}
else
{
printf("You cannot participate.");
}
return 0;
}
Enter ID card status (1 for Yes, 0 for No): 0 Enter registration status (1 for Yes, 0 for No): 1 You can participate.
| Concept | Purpose |
|---|---|
| if-else | Checks one condition and chooses between two alternatives. |
| Nested if-else | Places an if-else statement inside another if-else statement. |
| else-if | Checks multiple conditions one after another. |
| && | AND – both conditions must be true. |
| || | OR – at least one condition must be true. |
These examples are useful for beginners learning decision-making statements in C programming.
💻 Online C++ Compiler Write • Compile • Run • Get Output How to use: ...