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