Hello World Program in C Language (4 Ways)
Option 1: Hello World Program in C
#include <stdio.h>
main() {
printf("Hello, World!");
Output:
Hello, World!
Option 2: Print Hello World in C Using puts()
#include <stdio.h>
main() {
puts("Hello, World!");
}
Output:
Hello, World!
Option 3: Hello World Code in C Using Escape Characters
#include <stdio.h> // Including standard input-output header
main() {
printf("Hello,\nWorld!");
Output:
Hello,
World!
Option 4: Hello World in C by Printing Character by Character
#include <stdio.h>
main() {
printf("%c%c%c%c%c, %c%c%c%c%c!", 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l',
'd');
return 0;
}
Output:
Hello, World!
Program to Print Your Own Name Using printf
// C Program to Print Your Own Name using printf
#include <stdio.h>
main() {
// Printing your name "Rahul" on the output screen
printf("Rahul");
Output
Rahul
Take Your Name as Input and Then Print It
We can use scanf() function to take the name as input from the user and
store it in a character array. We can then use printf function to print the
name on the screen.
Syntax of scanf()
scanf("%s", charArr);
Program to Print Your Own Name by taking it as Input
// C Program to Print Your Own Name using scanf and printf
#include <stdio.h>
main() {
// defining string (character array) assuming 100
// characters at max
char name[100];
// Taking input from the user
printf("Enter Your Name: ");
scanf("%s", name);
// Printing your name to the screen
printf("Your Name: %s\n", name);
}
Output
Enter Your Name: Rahul
Your Name: Rahul
C Program for Area of Circle (2 Ways With Code)
Option 1: Basic C Program for Area of Circle Using #define
#include <stdio.h>
#define PI 3.14159
main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = PI * radius * radius;
printf("The area of the circle is: %.2f\n", area);
}
Output:
Enter the radius of the circle: 5
The area of the circle is: 78.54
Option 2: C Program for Area of Circle Using <math.h>
#include <stdio.h>
#include <math.h>
main() {
float radius, area;
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
area = M_PI * radius * radius;
printf("The area of the circle is: %.2f\n", area);
}
Output:
Enter the radius of the circle: 7
The area of the circle is: 153.94
C Program for Multiplication of Two Numbers
Option 1: C Program to Multiply Two Numbers
#include <stdio.h> // Including standard input-output header
main() {
int num1, num2, product; // Declaring variables to store two numbers
and their product
// Asking the user to input two numbers
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// Calculating the product of two numbers
product = num1 * num2;
// Printing the result
printf("Product: %d", product);
return 0; // Indicating that the program ended successfully
Output
Enter two integers: 4 5
Product: 20
Option 2: C Program to Multiply Two Floating Point Numbers
#include <stdio.h>
main() {
float num1, num2, product;
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2);
product = num1 * num2;
printf("Product: %.2f", product);
return 0;
}
Output:
Enter two numbers: 3.5 4.2
Product: 14.70
C Program to Swap Two Numbers
Option 1: C Program to Swap Two numbers Using Temporary
Variable
#include <stdio.h> // Including standard input-output library
main() {
int a, b, temp; // Declaring variables for two numbers and a temporary
variable for swapping
// Prompting user to enter two numbers
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
// Swapping the values using a temporary variable
temp = a;
a = b;
b = temp;
// Printing the swapped values
printf("After swapping: a = %d, b = %d", a, b);
return 0; // Indicating successful program completion
}
Output:
Enter two integers: 3 7
After swapping: a = 7, b = 3
Swapping Logic:
temp = a;: Store the value of a in temp.
a = b;: Assign the value of b to a.
b = temp;: Assign the stored value from temp to b.
Printing the Result: The program outputs the values of a and b after
swapping, confirming the exchange.
Option 2: Swapping of Two Numbers in C without Temporary
Variable (Using Arithmetic)
#include <stdio.h>
main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping: a = %d, b = %d", a, b);
return 0;
}
Output:
Enter two integers: 3 7
After swapping: a = 7, b = 3
C Program to Find Simple Interest
Option 1: Simple Interest in C Using Arithmetic Operations
#include <stdio.h>
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);
// Formula to calculate simple interest
simpleInterest = (principal * rate * time) / 100;
printf("The Simple Interest is: %.2f\n", simpleInterest);
}
Output
Enter principal amount: 1000
Enter rate of interest: 5
Enter time in years: 2
The Simple Interest is: 100.00
Option 1: C Program to Add Two Numbers
#include <stdio.h> //
main() {
int num1, num2, sum; // Declaring variables to store two numbers and
their sum
// Asking the user to input two numbers
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
// Calculating the sum of two numbers
sum = num1 + num2;
// Printing the result
printf("Sum: %d", sum);
return 0; // Indicating that the program ended successfully
}
Output:
Enter two integers: 10 15
Sum: 25
Option 2: C Code to Add Two Numbers using float
#include <stdio.h>
main() {
float num1, num2, sum; // Using float for decimal numbers
printf("Enter two numbers: ");
scanf("%f %f", &num1, &num2); // %f for float input
sum = num1 + num2;
printf("Sum: %.2f", sum); // %.2f to print two decimal places
}
Output:
Enter two numbers: 5.5 6.3
Sum: 11.80
Average of 3 Numbers in C Using Arithmetic Operations
#include <stdio.h>
main() {
float num1, num2, num3, average;
printf("Enter three numbers: ");
scanf("%f %f %f", &num1, &num2, &num3);
average = (num1 + num2 + num3) / 3;
printf("The average of the three numbers is: %.2f\n", average);
}
Output
Enter three numbers: 10 20 30
The average of the three numbers is: 20.00
Convert Celsius to Fahrenheit in C Using Arithmetic Operations
#include <stdio.h>
main() {
float celsius, fahrenheit;
printf("Enter temperature in Celsius: ");
scanf("%f", &celsius);
// Convert Celsius to Fahrenheit
fahrenheit = (celsius * 9 / 5) + 32;
printf("%.2f Celsius = %.2f Fahrenheit\n", celsius, fahrenheit);
}
Output
Enter temperature in Celsius: 25
25.00 Celsius = 77.00 Fahrenheit
C Program for Compound Interest
// C program to calculate Compound Interest
#include <stdio.h>
// For using pow function we must
// include math.h
#include<math.h>
// Driver code
main()
{
// Principal amount
double principal = 10000;
// Annual rate of interest
double rate = 5;
// Time
double time = 2;
// Calculating compound Interest
double Amount = principal *
((pow((1 + rate / 100),
time)));
double CI = Amount - principal;
printf("Compound Interest is : %lf",CI);
return 0;
}
Output:
Compound interest is 1025
Compound Interest formula:
Formula to calculate compound interest annually is given by:
Amount= P(1 + R/100)t
Compound Interest = Amount – P
Where,
P is principal amount
R is the rate and
T is the time span
EXPLANATION:
Input principal amount. Store it in some variable say principal.
Input time in some variable say time.
Input rate in some variable say rate.
Calculate Amount using formula,
Amount = principal * (1 + rate / 100) time).
Calculate Compound Interest using Formula.
Finally, print the resultant value of CI.
C Program to Find Area And Perimeter of Rectangle
The formula for the area of a rectangle is
Area = length*breadth => l*b
The formula for the perimeter of a rectangle is
Perimeter = 2*(length + breadth) => 2*(l+b)
#include <stdio.h>
main()
{
int l = 10, b = 10;
printf("Area of rectangle is : %d", l * b);
printf("\nPerimeter of rectangle is : %d", 2 * (l + b));
}
Output
Area of rectangle is : 100
Perimeter of rectangle is : 40
Method 2:
// C program to demonstrate the
// area and perimeter of rectangle
#include <stdio.h>
main()
{
int l = 10, b = 10;
int A, P;
A = l * b;
P = 2 * (l + b);
printf("Area of rectangle is : %d", A);
printf("\nPerimeter of rectangle is : %d", P);
return 0;
}
Output
Area of rectangle is: 100
Perimeter of rectangle is: 40
Operators in C
#include <stdio.h>
main() {
int a = 25, b = 5;
// using operators and printing results
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
//Returns the remainder after dividing the left operand with the right operand.
printf("a % b = %d\n", a % b);
printf("+a = %d\n", +a);
printf("-a = %d\n", -a);
printf("a++ = %d\n", a++);
printf("a-- = %d\n", a--);
}
Output
a + b = 30
a - b = 20
a * b = 125
a/b=5
a%b=0
+a = 25
-a = -25
a++ = 25
a-- = 26
Example of C Relational Operators
#include <stdio.h>
main() {
int a = 25, b = 5;
// using operators and printing results
printf("a < b : %d\n", a < b);
printf("a > b : %d\n", a > b);
printf("a <= b: %d\n", a <= b);
printf("a >= b: %d\n", a >= b);
printf("a == b: %d\n", a == b);
printf("a != b : %d\n", a != b);
return 0;
}
Output
a<b :0
a>b :1
a <= b: 0
a >= b: 1
a == b: 0
a != b : 1
Decision Making in C (if , if. Else, Nested if, if-else-if )
#include<stdio.h>
main() {
int i = 10;
// If statement
if (i < 18) {
printf("Eligible for vote");
}
Output
Eligible for vote
#include<stdio.h>
main() {
int i = 10;
if (i > 18) {
printf("Eligible for vote");
}
else {
printf("Not Eligible for vote");
}
Output
Not Eligible for vote
#include<stdio.h>
main() {
int i = 20;
// If else ladder with three conditions
if (i == 10)
printf("Not Eligible");
else if (i == 15)
printf("wait for three years");
else if (i == 20)
printf("You can vote");
else
printf("Not a valid age");
Output
You can vote
C for Loop
#include <stdio.h>
int main() {
// for loop to print "Hi" 5 times
for (int i = 5; i < 10; i++) {
printf("Hi\n");
}
return 0;
}
Output
Hi
Hi
Hi
Hi
Hi
Print First N Natural Numbers
#include <stdio.h>
int main() {
int n = 5;
// Initialization of loop variable
int i;
for (i = 1; i <= n; i++)
printf("%d ", i);
return 0;
}
Output
12345
Print Multiplication Table from 1 to 5
#include <stdio.h>
int main() {
// Outer for loop to print a multiplication
// table for all numbers upto 5
for (int i = 1; i <= 5; i++) {
// Inner loop to print each value in table
for (int j = 1; j <= 5; j++) {
printf("%d ", i * j);
}
printf("\n");
}
Output
12345
2 4 6 8 10
3 6 9 12 15
4 8 12 16 20
5 10 15 20 25
Explanation: This code uses nested for loops to generate a
multiplication table up to 5. The outer loop iterates through the rows
(from 1 to 5), and the inner loop (controlled by j) iterates through the
columns (also from 1 to 5). For each combination of i and j, the product i
* j is printed, creating the table entries.
Example of C Function
// C program to show function
// call and definition
#include <stdio.h>
// Function that takes two parameters
// a and b as inputs and returns
// their sum
int sum(int a, int b)
{
return a + b;
}
// Driver code
int main()
{
// Calling sum function and
// storing its value in add variable
int add = sum(10, 30);
printf("Sum is: %d", add);
return 0;
}
Output
Sum is: 40
// C program to implement
// the above approach
#include <math.h>
#include <stdio.h>
// Driver code
int main()
{
double Number;
Number = 49;
// Computing the square root with
// the help of predefined C
// library function
double squareRoot = sqrt(Number);
printf("The Square root of %.2lf = %.2lf",
Number, squareRoot);
return 0;
}
Output
The Square root of 49.00 = 7.00
// C program to show
// user-defined functions
#include <stdio.h>
int sum(int a, int b)
{
return a + b;
}
// Driver code
int main()
{
int a = 30, b = 40;
// function call
int res = sum(a, b);
printf("Sum is: %d", res);
return 0;
}
Output
Sum is: 70
1. C Program to Check Whether a Number is Positive, Negative, or
Zero
#include <stdio.h>
main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num > 0)
printf(“Positive number\n”);
else if (num < 0)
printf(“Negative number\n”);
else
printf(“Zero\n”);
return 0;
}
Output:
Enter a number: 7
Positive number
2. C Program to Check Whether Number is Even or Odd
#include <stdio.h>
main() {
int num;
printf(“Enter a number: “);
scanf(“%d”, &num);
if (num % 2 == 0)
printf(“Even number\n”);
else
printf(“Odd number\n”);
return 0;
}
Output:
Enter a number: 15
Odd number
3. C Program to Calculate Sum of Natural Numbers
#include <stdio.h>
main() {
int n, sum = 0;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
for (int i = 1; i <= n; ++i) {
sum += i;
}
printf(“Sum of natural numbers from 1 to %d: %d\n”, n, sum);
return 0;
}
Output:
Enter a positive integer: 5
Sum of natural numbers from 1 to 5: 15