Beginning the journey of programming may be both exciting and rewarding. Starting with the fundamentals is crucial to creating a strong foundation. Among programming languages, C is a strong and adaptable choice that is well-liked for its effectiveness and wide use. Learning the fundamentals of C through short programs is a great place to start if you’re a beginner who is excited to start coding.
We’ll walk you through some basic C programming ideas in this blog post. Every program is designed with care to demonstrate a certain idea, so you will understand the underlying logic as well as the syntax. Let’s get started on our path of learning by doing with practical examples that allow anyone to learn C programming.
Table of Contents
Simple C Programs with Output
1. Hello World Program
#include <stdio.h>
// This is a simple "Hello, World!" program in C
int main() {
// Display the message
printf("Hello, World!\n");
// Return 0 to indicate successful execution
return 0;
}
Output:
Hello, World!
2. Addition of Two Numbers with User Input
#include <stdio.h>
int main() {
// Declare variables to store user input
double num1, num2;
// Prompt user for input
printf("Enter the first number: ");
scanf("%lf", &num1);
printf("Enter the second number: ");
scanf("%lf", &num2);
// Perform addition
double sum = num1 + num2;
// Display the result
printf("The sum of %.2lf and %.2lf is: %.2lf\n", num1, num2, sum);
return 0;
}
Output:
Enter the first number: 5.4
Enter the second number: 3.2
The sum of 5.40 and 3.20 is: 8.60
3. Find the Maximum of Two Numbers
Program 1: Using Conditional Operator
#include <stdio.h>
int main() {
// Declare variables
int num1, num2, max;
// Input two numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Find the maximum using the conditional operator
max = (num1 > num2) ? num1 : num2;
// Display the result
printf("The maximum of %d and %d is: %d\n", num1, num2, max);
return 0;
}
Output:
Enter the first number: 25
Enter the second number: 33
The maximum of 25 and 33 is: 33
Program 2: Using if-else Statement
#include <stdio.h>
int main() {
// Declare variables
int num1, num2, max;
// Input two numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Find the maximum using if-else statement
if (num1 > num2) {
max = num1;
} else {
max = num2;
}
// Display the result
printf("The maximum of %d and %d is: %d\n", num1, num2, max);
return 0;
}
Output:
Enter the first number: 45
Enter the second number: 39
The maximum of 45 and 39 is: 45
4. Simple Interest Calculation Program
#include <stdio.h>
int main() {
// Declare variables
float principal, rate, time, simpleInterest;
// Get input from the user
printf("Enter the principal amount: ");
scanf("%f", &principal);
printf("Enter the rate of interest: ");
scanf("%f", &rate);
printf("Enter the time (in years): ");
scanf("%f", &time);
// Calculate simple interest
simpleInterest = (principal * rate * time) / 100;
// Display the result
printf("Simple Interest = %.2f\n", simpleInterest);
return 0;
}
Output:
Enter the principal amount: 1000
Enter the rate of interest: 5
Enter the time (in years): 2
Simple Interest = 100.00
5. Check whether the given integer is positive or negative
#include <stdio.h>
int main() {
// Declare variable to store user input
int number;
// Prompt user for input
printf("Enter an integer: ");
// Read user input
scanf("%d", &number);
// Check if the number is positive, negative, or zero
if (number > 0) {
printf("%d is a positive number.\n", number);
} else if (number < 0) {
printf("%d is a negative number.\n", number);
} else {
printf("The entered number is zero.\n");
}
return 0;
}
Output 1:
Enter an integer: 5
5 is a positive number.
Output 2:
Enter an integer: -8
-8 is a negative number.
Output 3:
Enter an integer: 0
The entered number is zero.
6. Check Even or Odd
Program 1: Using Modulo Operator
#include <stdio.h>
int main() {
int num;
// Input from user
printf("Enter an integer: ");
scanf("%d", &num);
// Using modulo operator to check even or odd
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
Output :
Enter an integer: 5
5 is odd.
Program 2: Using Bitwise AND Operator
#include <stdio.h>
int main() {
int num;
// Input from user
printf("Enter an integer: ");
scanf("%d", &num);
// Using bitwise AND operator to check even or odd
if (num & 1) {
printf("%d is odd.\n", num);
} else {
printf("%d is even.\n", num);
}
return 0;
}
Output :
Enter an integer: 16
16 is even.
7. Swap two numbers
Program 1: Using a Temporary Variable
#include <stdio.h>
int main() {
int num1, num2, temp;
// Input
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Swapping using a temporary variable
temp = num1;
num1 = num2;
num2 = temp;
// Output
printf("After swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}
Output :
Enter the first number: 5
Enter the second number: 10
After swapping:
First number: 10
Second number: 5
Program 2 : Without Using a Temporary Variable (Using Arithmetic Operations)
#include <stdio.h>
int main() {
int num1, num2;
// Input
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Swapping without a temporary variable
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
// Output
printf("After swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}
Output :
Enter the first number: 7
Enter the second number: 3
After swapping:
First number: 3
Second number: 7
Program 3 : Without Using a Temporary Variable (Using XOR)
#include <stdio.h>
int main() {
int num1, num2;
// Input
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Swapping without a temporary variable using XOR
num1 = num1 ^ num2;
num2 = num1 ^ num2;
num1 = num1 ^ num2;
// Output
printf("After swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}
Output:
Enter the first number: 8
Enter the second number: 12
After swapping:
First number: 12
Second number: 8
8. C program to calculate the area of a circle
#include <stdio.h>
int main() {
// Declare variables
float radius, area;
// Get user input for the radius
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate the area of the circle
area = 3.14159 * radius * radius;
// Display the result
printf("The area of the circle with radius %.2f is %.2f\n", radius, area);
return 0;
}
Output:
Enter the radius of the circle: 5
The area of the circle with radius 5.00 is 78.54
9. C program to convert Fahrenheit to Celsius
#include <stdio.h>
int main() {
// Declare variables
float fahrenheit, celsius;
// Prompt user for input
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
// Convert Fahrenheit to Celsius
celsius = (fahrenheit - 32) * 5 / 9;
// Display the result
printf("Temperature in Celsius: %.2f\n", celsius);
return 0;
}
Output:
Enter temperature in Fahrenheit: 98.6
Temperature in Celsius: 37.00
10. C Program to Find the Size of int, float, double and char
#include <stdio.h>
int main() {
// Find and print the size of int
printf("Size of int: %lu bytes\n", sizeof(int));
// Find and print the size of float
printf("Size of float: %lu bytes\n", sizeof(float));
// Find and print the size of double
printf("Size of double: %lu bytes\n", sizeof(double));
// Find and print the size of char
printf("Size of char: %lu byte\n", sizeof(char));
return 0;
}
Output:
Size of int: 4 bytes
Size of float: 4 bytes
Size of double: 8 bytes
Size of char: 1 byte