0% found this document useful (0 votes)
3 views5 pages

C Programming Basics: Print, Add, Swap

The document contains multiple C programs demonstrating basic programming concepts such as printing text, reading and printing integers, performing arithmetic operations, and calculating area and perimeter of a rectangle. It includes examples for printing 'Hello World', adding two numbers, swapping values, calculating simple and compound interest, and functions for area and perimeter. Each program is self-contained with comments explaining the code functionality.

Uploaded by

dasdipra816
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

C Programming Basics: Print, Add, Swap

The document contains multiple C programs demonstrating basic programming concepts such as printing text, reading and printing integers, performing arithmetic operations, and calculating area and perimeter of a rectangle. It includes examples for printing 'Hello World', adding two numbers, swapping values, calculating simple and compound interest, and functions for area and perimeter. Each program is self-contained with comments explaining the code functionality.

Uploaded by

dasdipra816
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C Program to Print “Hello World”

// Header file for input output functions


#include <stdio.h>

// Main function: entry point for execution


int main() {

// Writing print statement to print hello world


printf("Hello World");

return 0;
}

Program to Print Your Own Name Using printf

// C Program to Print Your Own Name using printf


#include <stdio.h>

int main() {

// Printing your name "Rahul" on the output screen


printf("Rahul");

return 0;
}

How to Read and Print an Integer Value in C


// C Program to Print
// Integer value
#include <stdio.h>

// Driver code
int main()
{
// Declaring integer
int x = 5;

// Printing values
printf("Printing Integer value %d", x);
return 0;
}
Below is the C program to read integer values in C:
// C program to take an integer
// as input and print it
#include <stdio.h>

// Driver code
int main()
{
// Declare the variables
int num;

// Input the integer


printf("Enter the integer: ");
scanf("%d", &num);

// Display the integer


printf("Entered integer is: %d", num);

return 0;
}

Program to Add Two Numbers using + Operator

// C program to add two numbers


#include <stdio.h>

int main() {
int a, b, sum = 0;

// Read two numbers from the user


printf("Enter two integers: ");
scanf("%d %d", &a, &b);

// Calculate the addition of a and b


// using '+' operator
sum = a + b;

printf("Sum: %d", sum);

return 0;
}

Swap Two Numbers Using Temporary Variable


// C Program to Swap Two Numbers using a
// Temporary Variable

#include <stdio.h>

int main() {
int a = 5, b = 10, temp;
// Swapping values of a and b
temp = a;
a = b;
b = temp;

printf("a = %d, b = %d\n", a, b);


return 0;
}

Simple Interest Program in C


// C program to find the simple interest
#include <stdio.h>

int main() {

// Input values
float P = 1, R = 1, T = 1;

// Calculate simple interest


float SI = (P * T * R) / 100;

// Print Simple Interest


printf("Simple Interest = %f\n", SI);

return 0;
}

C Program For Compound Interest


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

// C program to calculate Compound Interest


#include <stdio.h>

// For using pow function we must


// include math.h
#include<math.h>

// Driver code
int 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;
}

C Program To Find Area And Perimeter of


Rectangle

// C program to demonstrate the


// area and perimeter of rectangle
#include <stdio.h>

int 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;
}

2.// C program to demonstrate the


// area and perimeter of rectangle
#include <stdio.h>

int main()
{

int l = 10, b = 10;


printf("Area of rectangle is : %d", l * b);
printf("\nPerimeter of rectangle is : %d", 2 * (l + b));
return 0;
}

// C program to demonstrate the


// area and perimeter of rectangle
// using function
#include <stdio.h>

int area(int a, int b)


{
int A;
A = a * b;
return A;
}
int perimeter(int a, int b)
{
int P;
P = 2 * (a + b);
return P;
}

int main()
{

int l = 10, b = 10;


printf("Area of rectangle is : %d", area(l, b));
printf("\nPerimeter of rectangle is : %d",
perimeter(l, b));
return 0;
}

You might also like