0% found this document useful (0 votes)
12 views

C Practical 1

Uploaded by

funrun766
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

C Practical 1

Uploaded by

funrun766
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

Experiment No.

01

Aim :-

Demonstrate a program using input/output statement.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

The 'printf' statement is part of the C programming language and is used for
formatted output. It allows you to print information to the console with specified
formatting. The format string contains placeholders, such as '%d' for integers or '%s'
for strings, which are then replaced by the corresponding values provided as additional
arguments.

the 'printf' statement is part of the standard input/output library ('stdio.h'). It is used
to display formatted output on the console. Here's a simple theory for printing "Hello,
World!" using the ‘printf’ statement.
Source Code :-

include<stdio.h>

int main()

printf("Hello World!");

return 0;

Output :-
Experiment No. 02

Aim :-

Demonstrate a program using arithmetic operators.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

Arithmetic operators are used to perform mathematical operations on variables and


values.

● Addition (" + ") : Adds two operands to produce the sum.


● Subtraction (" - ") : Subtracts the right operand from the left operand.
● Multiplication (" * ") : Multiplies two operands to produce the product.
● Division (" / ") : Divides the left operand by the right operand to produce the
quotient.
● Modulus (" % ") : Returns the remainder of the division of the left operand by
the right operands.
Source Code :-

#include<stdio.h>

int main()

int x,y ;

int a, b, c, d, e;

printf("Enter the two numbers to perform arithmetic operations: ");

scanf("%d %d",&x, &y);

a = x + y;

printf("The addition of %d and %d is: %d \n",x,y,a);

b = x - y;

printf("The subtraction of %d and %d is: %d \n",x,y,b);

c = x * y;

printf("The multiplication of %d and %d is: %d \n",x,y,c);

d = var1 / var2;

printf("The division of %d and %d is: %d \n",x,y,d);

e = var1 % var2;

printf("The modulus of %d and %d is: %d \n",x,y,e);

return 0;

}
Output :-
Experiment No. 03

Aim :-

Demonstrate a program using relational operators.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

Relational operators are used to compare two values and determine the relationship
between them. These operators return a Boolean result, either true (1) or false (0).
Here are some common relational operator :

1. Less than ("<"):


Checks if the left operand is less than the right operand.
2. Greater than (">"):
Checks if the left operand is greater than the right operand.
3. Less than or equal to ("<="):
Checks if the left operand is less than or equal to the right operand.
4. Greater than or equal to (">="):
Checks if the left operand is greater than or equal to the right operand.
5. Equal to ("=="):
Checks if two values are equal.
6. Not equal to ("!="):
Checks if two values are not equal.

Source Code :-

#include<stdio.h>

int main()

int x, y;

printf("Enter the two numbers to check relationship between them:");

scanf("%d%d", &x,&y);

printf("%d is Greater than or equal to %d: the result is %d\n",x,y,x>=y);

printf("%d equals to %d: the result is %d\n", x, y,x==y);

printf("%d is Greater-than %d: the result is %d\n", x, y,x>y);

printf("%d is Less-than %d: the result is %d\n",x, y,x<y);

printf("%d is not equal to %d: the result is %d\n", x, y,y!=x);

return 0;

}
Output :-
Experiment No. 04

Aim :-

Demonstrate a program using logical operators.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

Logical operators are used to perform logical operations on boolean values (true or
false). This are often used in conditional statements and loops to control the flow of a
program based on certain conditions.

1. Logical AND ("&&"): The "&&" operator returns true if both of its operands
are true; otherwise, it returns false.

2. Logical OR ("||"): The "||" operator returns true if at least one of its operands is
true; it returns false only if both operands are false.

3. Logical NOT ("!"): The "!" operator is a unary operator that negates the truth
value of its operand. If the operand is true, ! makes it false; if the operand is false, !
makes it true.
Source Code :-

#include<stdio.h>

int main()

int x, y, a, b, c, d;

x = 20<30 && 40<50;

y = 20<30 && 40>50;

printf("\nThe logical AND operator works as :-");

printf("\nResult of the first expression is %d\n",x);

printf("Result of the second expression is %d\n",y);

a = 20<30 || 40>50;

b = 62<96 || 70>54;

printf("\nThe logical OR operator works as :-");

printf("\nResult of the first expression is %d\n",a);

printf("nResult of the second expression is %d\n",b);

c = !(20>30);

d = !(20<30);

printf("\nThe logical NOT operator works as :-");

printf("\nResult of !(20>30) is %d",c);

printf("\nResult of !(20<30) is %d\n",d);


return 0;

Output :-
Experiment No. 05

Aim :-

Demonstrate a program using bitwise operators.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

The bitwise operators deal with the bit. It means that given operands are converted into
the binary form and then manipulated bits according to the operations.

1. Bitwise AND “&” : Takes two binary numbers as operands and performs a
bitwise AND operation on each pair of corresponding bits.

2. Bitwise OR “|” : Performs a bitwise OR operation on each pair of


corresponding bits.

3. Bitwise XOR “^” : Performs a bitwise exclusive OR operation on each pair of


corresponding bits. The result is 1 if the bits are different.
4. Bitwise NOT “~” : Takes one binary number and flips all its bits, changing 0s
to 1s and vice versa.

5. Left Shift “<<”: Shifts the bits of a binary number to the left by a specified
number of positions. New bits are filled with zeros.

6. Right Shift “>>”: Shifts the bits of a binary number to the right by a specified
number of positions. The vacant positions are filled based on the type of shift.

Source Code :-

#include<stdio.h>

int main()

int a, b,c;

printf("Enter two numbers to perform bitwise operations::\n ");

scanf("%d%d",&a,&b);

c = a | b;

printf("The result of the bitwise OR operation is %d \n",c);

c = a & b;

printf("The result of the bitwise AND operation is %d \n",c);

c = a ^ b;

printf("The result of the bitwise X-OR operation is %d \n",c);

c = a << b;

printf("The result of the bitwise left shift operation is %d \n",c);


c = a >> b;

printf("The result of the bitwise right shift operation is %d \n",c);

c = ~a;

printf("The result of the bitwise complement operation is %d \n",c);

return 0;

Output :-
Experiment No. 06

Aim :-

Demonstrate a program using unary operators.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

1. Unary Plus Operator (+) :


The unary plus operator is used to indicate the positive value of an expression.
It is the often used for clarity and does not change value of the operand.
2. Unary Minus Operator (-) :

The unary minus operator is used to negate the value of an expression. When
applied to a positive value, it changes the sign to negative, and vice versa.
3. Address of Operator (&) :

The address-of operator returns the memory address of its operand. It is often
used with pointers to obtain the memory address of a variable.
4. Size of Operator (size of) :
The sizeof operator is used to determine the size, in bytes, of a data type or a
variable. It provides a compile-time constant value that represents the size of the
expression or type.

Source Code :-

#include <stdio.h>

int main()

int a,b;

printf("Enter a number to perform unary operations ");

scanf("%d",&a);

b = -a;

printf("After the unary minus operation the number becomes %d \n",b);

b = +a;

printf("After the unary plus operation the number becomes %d \n",b);

printf("The address of %d is %d \n",a,&a);

b = sizeof(a);

printf("The number of bytes required to store %d is %d\n",a,b);

return 0;

}
Output :-

Unary increment and decrement operators are used to increase or decrease the value of
a variable by 1. There are two forms of these operators: pre-increment, post-increment,
pre-decrement, and post-decrement.

Pre-increment (“++var”) :

The value of the variable is incremented before its value is used in an expression.

Post-increment (“var++”) :

The current value of the variable is used in an expression before it is incremented.

Pre-decrement (“--var”) :

The value of the variable is decremented before its value is used in an expression.

Post-decrement (“var--”) :

The current value of the variable is used in an expression before it is decremented.


Source Code :-

#include <stdio.h>

int main()

int a,b;

printf("Enter a number to perform pre-increment operation ");

scanf("%d",&a);

printf("After the pre-increment, The number is: %d\n", ++a);

printf("The updated number is: %d\n\n",a);

printf("Enter a number to perform post-increment operation ");

scanf("%d",&a);

printf("After the post-increment, The number is: %d\n", a++);

printf("The updated number is: %d\n\n",a);

printf("Enter a number to perform pre-decrement operation ");

scanf("%d",&a);

printf("After the pre-decrement , The number is: %d\n", --a);

printf("The updated number is: %d\n\n",a);

printf("Enter a number to perform post-decrement operation ");

scanf("%d",&a);

printf("After the post-decrement , The number is: %d\n",a--);

printf("The updated number is: %d\n",a);

return 0;
}

Output :-
Experiment No. 07

Aim :-

Demonstrate a program using ternary conditional statement.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

The ternary conditional operator in C, often denoted as “? :”, provides a concise way to
express conditional statements.

syntax:

condition ? expression if true : expression if false;

1. Condition Evaluation :

The condition is evaluated first. It is typically a relational or logical expression


that results in either true or false.

2. Branching :
If the condition evaluates to true, the expression immediately following the ‘?’ is
executed.

If the condition evaluates to false, the expression immediately following the ‘:’
is executed.

Source Code :-

#include <stdio.h>

int main()

int a, b, c;

a= 14<19?14:19;

b= 45>96?45:96;

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

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

return 0;

}
Output :-
Experiment No. 08

Aim :-

Demonstrate a program of implicite and explicite conversion.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-

Implicit and explicit conversions in C involve the conversion of data from one type to
another. These conversions can occur automatically by the compiler or can be
explicitly specified by the programmer.

Implicit Conversion:

Definition: Implicit conversion, also known as automatic or type coercion, is a


conversion performed by the compiler without any explicit instruction from the
programmer.

Automatic Nature: The compiler automatically converts one data type to another
when it determines that the conversion is safe and will not result in loss of data.
Widening Conversions: Implicit conversions typically involve widening conversions,
where there is no loss of precision or data. For example, converting an integer to a
floating-point number.

Explicit Conversion:

Definition: Explicit conversion, also known as type casting, is a conversion explicitly


specified by the programmer to change the data type of a variable.

Manual Instruction: The programmer uses casting operators like (type) to instruct the
compiler to perform the conversion.

Narrowing Conversions: Explicit conversions often involve narrowing conversions,


where there may be a risk of losing data or precision. For instance, converting a
floating- point number to an integer.

Source Code :-

#include<stdio.h>

int main()

printf("\nThis is how an Implicit conversion works::\n");

int x = 19;

char y = 'A';

x = x + y;

float z = x + 1.0;

printf("x = %d, z = %f", x, z);


printf("\nThis is how an Explicite conversion works::\n");

double a = 1.7;

int add = (int)a + 3;

printf("addition = %d", add);

return 0;

Output :-
Experiment No. 09

Aim :-

Demonstrate a program using Function.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.
Theory :-

Function in C :
A function in C is a block of code that performs a specific task, designed to be reusable
and modular. Functions help organize programs and avoid redundancy.

Definition of a Function:
A function is a set of statements grouped together to perform a specific operation.
Functions take input, perform computations, and return an output.

Types of Functions:
1. Library Functions: Built-in functions (e.g., printf(), scanf(), sqrt()).
2. User-defined Functions: Custom functions created by the programmer.

Defining a Function:

The syntax for defining a function:

return_type function_name(parameters)
{
// Function body
return value; // Optional
}
Example:

int add(int a, int b) {


return a + b;
}

Function Declaration:

The function declaration (prototype) specifies the function's name, return type, and
parameters before it is used in the program.

Syntax:

return_type function_name(parameter_list);

Example:

int add(int, int);

Function Calling:

To use a function, call it in the main program.

1. Call by Value:

● Copies the actual value of arguments to the function.


● Changes inside the function don't affect the original variables.

Example:

void modify(int x) {

x = 10; // Changes only the copy

2. Call by Reference:

● Passes the address of arguments to the function.


● Changes inside the function affect the original variables.

Example:

void modify(int *x) {

*x = 10; // Changes the original variable

}
Source Code :-

#include <stdio.h>

// Function Declaration
int add(int a, int b); // User-defined function (Call by Value)
void modify(int *x); // User-defined function (Call by Reference)

int main() {
// Call by Value
int num1 = 5, num2 = 10;
int result = add(num1, num2); // Function calling (Call by Value)
printf("Sum of %d and %d (Call by Value): %d\n", num1, num2, result);

// Call by Reference
int num3 = 20;
printf("Before Modify (Call by Reference): %d\n", num3);
modify(&num3); // Function calling (Call by Reference)
printf("After Modify (Call by Reference): %d\n", num3);

return 0;
}

// Function Definition
int add(int a, int b) {
return a + b;
}

// Function Definition for Call by Reference


void modify(int *x) {
*x = 50;
}

Output :-
Experiment No. 10

Aim :-

Demonstrate a program using Recursive Function.

Software :-

Dev-C++

Dev-C++ is an open source integrated development environment (IDE) that is widely


used for C and C++ programming. It provide a user friendly interface and a range
feature to assit developers in writing, compiling, and a code editor with syntax
highlighting, auto completion, and code folding, making it easier to write and navigate
through your code.

Theory :-
Definition of Recursion:

Recursion is a programming technique where a function calls itself directly or indirectly


to solve a problem by breaking it into smaller subproblems.

Types of Recursion:

1. Direct Recursion:
A function directly calls itself.

a. Tail Recursion:
The recursive call is the last operation in the function.

b. Head Recursion:
The recursive call is made before any computation in the function.

c. Linear Recursion:
The function makes only one recursive call per execution.

d. Tree Recursion:
The function makes multiple recursive calls, creating a branching
structure.

e. Nested Recursion:
A function's parameter includes a recursive function call (e.g.,
f(f(x))).

2. Indirect Recursion:
A function calls another function, which eventually calls the original function
back.

Source Code :-

#include <stdio.h>

// Definition of recursion: A function that calls itself


void directRecursion(int n) {
if (n > 0) {
printf("%d ", n);
directRecursion(n - 1); // Direct recursion
}
}

// Tail recursion: Recursive call is the last operation


void tailRecursion(int n) {
if (n > 0) {
printf("%d ", n);
tailRecursion(n - 1); // Recursive call at the tail
}
}

// Head recursion: Recursive call is made before any processing


void headRecursion(int n) {
if (n > 0) {
headRecursion(n - 1); // Recursive call at the head
printf("%d ", n);
}
}

// Linear recursion: One recursive call per step


void linearRecursion(int n) {
if (n > 0) {
printf("%d ", n);
linearRecursion(n - 1);
}
}

// Tree recursion: Multiple recursive calls per step


void treeRecursion(int n) {
if (n > 0) {
printf("%d ", n);
treeRecursion(n - 1); // First recursive call
treeRecursion(n - 2); // Second recursive call
}
}

// Nested recursion: Recursive call inside another recursive call


int nestedRecursion(int n) {
if (n > 10)
return n - 1;
return nestedRecursion(nestedRecursion(n + 2));
}

// Indirect recursion: Two functions call each other recursively


void funcA(int n);
void funcB(int n);

void funcA(int n) {
if (n > 0) {
printf("%d ", n);
funcB(n - 1); // Indirect recursion via funcB
}
}

void funcB(int n) {
if (n > 0) {
printf("%d ", n);
funcA(n - 1); // Indirect recursion via funcA
}
}

int main() {
printf("Direct Recursion: ");
directRecursion(5);

printf("\nTail Recursion: ");


tailRecursion(5);

printf("\nHead Recursion: ");


headRecursion(5);

printf("\nLinear Recursion: ");


linearRecursion(5);

printf("\nTree Recursion: ");


treeRecursion(3);

printf("\nNested Recursion: ");


printf("%d ", nestedRecursion(5));

printf("\nIndirect Recursion: ");


funcA(5);

return 0;
}

Output :-

You might also like