Relational Operators:
Equal to (= =)
1) By using Constant values
#include <stdio.h>
int main()
{
int x=10;
int y=10;
if(x==y){
printf(“x is equal to y\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use equal to operator (==)
if (num1 == num2)
{
printf (" \n The value of num1 is equal to num2.");
}
else
{
printf (" \n The value of num1 is not equal to num2.");
}
return 0;
}
Not Equal to:
1) By using Constant values
#include <stdio.h>
int main()
{
int x=10;
int y=20;
if(x!=y){
printf(“x is not equal to y\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use not equal to operator (==)
if (num1 == num2)
{
printf (" \n The value of num1 is equal to num2.");
}
else
{
printf (" \n The value of num1 is not equal to num2.");
}
return 0;
}
Greater than:
1) By using Constant values
#include <stdio.h>
int main (){
int x=20;
int y=10;
if(x>y){
printf(“x is greater than y\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use greater than operator (>)
if (num1 > num2)
{
printf (" \n The value of num1 is greater than num2.");
}
else
{
printf (" \n The value of num1 is not greater than num2.");
}
return 0;
}
Less than:
1) By using Constant values
#include <stdio.h>
int main (){
int x=10;
int y=20;
if(x<y){
printf(“x is less than y\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use less than operator (<)
if (num1 < num2)
{
printf (" \n The value of num1 is less than num2.");
}
else
{
printf (" \n The value of num1 is not less than num2.");
}
return 0;
}
Greater than or Equal to:
1) By using Constant values
#include <stdio.h>
int main (){
int x=20;
int y=20;
if(x>=y){
printf(“x is greater than or equal to y\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main ()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use greater than or equal to operator (>=)
if (num1 >= num2)
{
printf (" \n The value of num1 is greater than or equal to num2.");
}
else
{
printf (" \n The value of num1 is not greater than or equal to num2.");
}
return 0;
}
Less than or equal to:
1) By using Constant values
#include <stdio.h>
int main (){
int x=10;
int y=20;
if(x<=y){
printf(“x is less than or equal to y\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main ()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use less than or equal to operator (<=)
if (num1 <= num2)
{
printf (" \n The value of num1 is less than or equal to num2.");
}
else
{
printf (" \n The value of num1 is not less than or equal to num2.");
}
return 0;
}
Logical Operators:
Logical AND (&&):
1) By using Constant values
#include <stdio.h>
int main(){
int x=10;
int y=20;
if(x>5&&y>15){
printf(“Both conditions are true\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use logical AND operator (&&)
if (num1>20&& num2>15)
{
printf (" Both the conditions are true\n");
}
else
{
printf (" the condition is false\n.");
}
return 0;
}
Logical OR(||):
1) By using Constant values
#include <stdio.h>
int main(){
int x=10;
int y=5;
if(x>15||y<10){
printf(“At least one condition is true\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
printf (" \n Enter the value of num2: ");
scanf (" %d", &num2);
// use logical OR operator (||)
if (num1>20 || num2<15)
{
printf (" At least one condition is true\n");
}
else
{
printf (" Both the conditions are false\n.");
}
return 0;
}
Logical NOT(!):
1) By using Constant values
#include <stdio.h>
int main(){
int x=10;
if(!(x>15)){
printf(“condition is false\n”);
}
return 0;
}
2) By accepting user’s input:
#include <stdio.h>
int main()
{
int num1, num2;
printf (" Enter the value of num1: ");
scanf (" %d", &num1);
// use logical NOT operator (!)
if (!(num1>20))
{
printf (" condition is false\n");
}
else
{
printf ("conditions is true\n.");
}
return 0;
}
Experiment No. 3
Title: Write a program to demonstrate the use of conditional expressions and
explain the order of evaluation.
Aim: A program to demonstrate the use of conditional expressions and explain
the order of evaluation.
Theory:
A conditional expression is a compound expression that contains a condition that is
implicitly converted to type bool in c/C++(operand1), an expression to be
evaluated if the condition evaluates to true (operand2), and an expression to be
evaluated if the condition has the value false (operand3).
While the order of operations defines the abstract syntax tree of the expression, the
evaluation order defines the order in which expressions are evaluated.
Flowchart:
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Program:
#include <stdio.h>
int main()
{
int m = 5, n = 4;
(m > n) ?
printf("m is greater than n that is %d > %d", m, n);
printf("n is greater than m that is %d > %d", n, m);
return 0;
}
Experiment No. 4
Title: Create a C program to perform bitwise operations on integer variables and
print the results.
Aim: A C program to perform bitwise operations on integer variables and print the
results.
Theory: In computer programming, a bitwise operation operates on a bit string, a
bit array or a binary numeral (considered as a bit string) at the level of its
individual bits. It is a fast and simple action, basic to the higher-level arithmetic
operations and directly supported by the processor.
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Program:
Experiment No. 5
Title: Develop a C program to Demonstrate the use of assignment operators and
evaluate expressions involving them.
Aim: A C Program to Demonstrate the use of assignment operators and
evaluate expressions involving them.
Theory: Assignment operators are used for assigning value to a variable. The left
side operand of the assignment operator is a variable and right-side operand of the
assignment operator is a value.
Operator Description Example
C = A + B will assign
Simple assignment operator. Assigns values from
= the value of A + B to
right side operands to left side operand
C
Add AND assignment operator. It adds the right
C += A is equivalent
+= operand to the left operand and assign the result to
to C = C + A
the left operand.
Subtract AND assignment operator. It subtracts the
C -= A is equivalent
-= right operand from the left operand and assigns the
to C = C - A
result to the left operand.
Multiply AND assignment operator. It multiplies
C *= A is equivalent
*= the right operand with the left operand and assigns
to C = C * A
the result to the left operand.
Divide AND assignment operator. It divides the
C /= A is equivalent
/= left operand with the right operand and assigns the
to C = C / A
result to the left operand.
Modulus AND assignment operator. It takes
C %= A is equivalent
%= modulus using two operands and assigns the result
to C = C % A
to the left operand.
C <<= 2 is same as C
<<= Left shift AND assignment operator.
= C << 2
C >>= 2 is same as C
>>= Right shift AND assignment operator.
= C >> 2
C &= 2 is same as C
&= Bitwise AND assignment operator.
=C&2
C ^= 2 is same as C =
^= Bitwise exclusive OR and assignment operator.
C^2
C |= 2 is same as C =
|= Bitwise inclusive OR and assignment operator.
C|2
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Program:
Experiment No. 6
Title: Write a C program to implement various control flow statements such as if-
else, switch-case, and loops, to solve a given problem.
Aim: A C program to implement various control flow statements such as if-else,
switch-case, and loops, to solve a given problem.
Theory: Control statements in C language are essential constructs that enable
programmers to control the flow of execution within a program. They dictate
which sections of code should be executed based on certain conditions. In other
words, the control statements help in making decisions, repeating tasks, and
creating logical structures within programs. They provide flexibility and control
over the execution of a C program.
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
1) Decision Making:
The conditional statements (also known as decision control structures) such
as if, if else, switch, etc. are used for decision-making purposes in C
programs.
They are also known as Decision-Making Statements and are used to
evaluate one or more conditions and make the decision whether to execute a
set of statements or not. These decision-making statements in programming
languages decide the direction of the flow of program execution.
These are as follows-
Simple if statement
If-else statements
Nested if-else statements
else-if ladder
Switch case statement
Simple if Statement
Simple if statements are carried out to perform some operation when the condition is
only true. If the condition of the if statement is true then the statements under the if
block is executed else the control is transferred to the statements outside the if block.
Syntax of the if statement is as given below:
if(condition)
{
statement1;
}
Flow Chart:
If-else Statement
In some situations, you may have to execute statements based on true or false under
certain conditions, therefore; you use if-else statements. If the condition is true, then if
block will be executed otherwise the else block is executed.
Syntax of the if-else statement is as given below:
if(condition)
statement1;
else
statement2;
Flowchart:
Nested if-else Statements
The nested if-else statements consist of another if or else. Therefore; if the condition of
“if” is true (i.e., an outer if) then outer if’s if block is executed which contains another if
(that is inner if) and if the condition of if block is true, statements under if block will be
executed else the statements of inner if’s “else” block will be executed.
If the outer “if” condition is not true then the outer if’s “else” block is executed which
consists of another if. The outer else’s inner if the condition is true then the statement
under outer else’s inner if is executed else the outer else’s else block is executed.
Flowchart:
Else-if Ladder Statements
The else-if ladder statements contain multiple else-if, when either of the condition
is true the statements under that particular “if” will be executed otherwise the
statements under the else block will be executed.
Suppose the “if” condition is true, statements under “if” will be executed else the
other “if” condition is tested, and if that condition is true statements under that
particular “if” will be executed. This process will repeat as long as the else-if’s are
present in the program.
Syntax of the else-if ladder statement is as given below:
Flowchart:
Program:
Simple If Statement:
#include <stdio.h>
int main() {
int num=10;
if(num>0) {
printf(“Number is positive\n”);
}
return 0;
}
If else statement:
#include <stdio.h>
int main() {
int number;
printf("Enter an integer: ");
scanf("%d", &number);
// True if the remainder is 0
if (number%2 == 0) {
printf("%d is an even integer.",number);
}
else {
printf("%d is an odd integer.",number);
}
return 0;
}
Nested If else Statement:
#include <stdio.h>
int main() {
int num=15;
if (num 0) {
if (num%2==0) {
printf(“number is positive and even\n”);
}
else {
printf(“number is positive and odd\n”);
}
}
else {
printf(“number is non positive \n”);
}
return 0;
}
Else-if ladder:
#include <stdio.h>
int main() {
int score;
printf(“Enter the score(0-100):”);
scanf(“%d”, &score);
if (score>=90) {
printf(“Grade:A\n”); //Executes if score is 90 or above
} else if (score>=80) {
printf(“Grade:B\n”); //Executes if score is between 80 and 89
} else if (score>=70) {
printf(“Grade:C\n”); //Executes if score is between 70 and 79
} else if (score>=60) {
printf(“Grade:D\n”); //Executes if score is between 60 and 69
} else {
printf(“Grade:F\n”); //Executes if score is below 60
}
return 0;
}
Switch Case Statement:
#include<stdio.h>
Int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
default;
printf("Invalid day\n");
break;
}
}
Looping Statement:
Looping statement defines a set of repetitive statements. These statements are
repeated, with same or different parameters for a number of times.
In programming, there exists situations when you need to repeat single or a group
of statements till some condition is met. Such as – read all files of a directory.
Looping statements are also known as iterative or repetitive statement.
There are mainly two types of loops in C Programming:
Entry Controlled loops: In Entry controlled loops the test condition is checked
before entering the main body of the loop. For Loop and While Loop is Entry-
controlled loops.
Exit Controlled loops: In Exit controlled loops the test condition is evaluated at
the end of the loop body. The loop body will execute at least once, irrespective of
whether the condition is true or false. do-while Loop is Exit Controlled loop.
For Loop
The for loop is also known as a pre-test loop. From the following syntax, expression1 is
an initialization, expression2 is the conditional expression and expression3 is an
updation. The variables can be initialized in for the statement itself.
The syntax of the do-while loop is as given below:
In the for loop, expression1 is used to initialize the variable, expression2 is evaluated
and if the condition is true, then the body of for loop will be executed and then the
statements under expression3 will be executed. This process is repeated as long as the
for loop condition is true, once the condition is false control will return to the statements
following the for loop and execute those statements.
Flow Chart:
While Loop
A while loop is also known as an entry loop because in a while loop the condition is
tested first then the statements underbody of the while loop will be executed.
If the while loop condition is false for the first time itself then the statements under the
while loop will not be executed even once.
The syntax of the while loop is as given below:
do-while Loop
The do-while is also known as an exit loop because in the do-while loop, the statements
will be executed first and then the condition is checked.
If the condition of the while loop is true then the body of the loop will be executed again
and again until the condition is false. Once the condition is false, the control will transfer
outside the do-while loop and execute statements followed soon after the do-while loop.
The syntax of the do-while loop is as given below:
Flow Chart:
Programs:
For Loop:
1) #include<stdio.h>
int main() {
//For loop example to print numbers 1 to 5
for(int i=1; i<=5; i++) {
printf(“%d”, i);
}
printf(“\n”);
return 0;
}
2) #include<stdio.h>
int main() {
for(int i=1; i<=3; i++) {
for(int j=1; j<=3; j++)
printf(“%d%d”, i,j);
}
printf(“\n”);
return 0;
}
While Loop
#include <stdio.h>
int main()
{
int count = 1;
while (count <= 5) {
printf("count:%d\n", count);
count++;
}
return 0;
}
Do-While Loop
#include <stdio.h>
int main()
{
int num;
do{
printf(“Enter a positive number:”);
scanf(“%d”, &num);
if(num<=0) {
printf(“Invalid input. Please enter a positive number\n”);
}
}while (num <= 0);
printf("You entered:%d\n”, num);
return 0;
}
Experiment No. 7
Title: Create a function in C to calculate the factorial of a given number and
display the result.
Aim: A Program to Create a function in C to calculate the factorial of a given
number and display the result.
Theory: A function in C is a block of organized reusable code that is performs a
single related action. Every C program has at least one function, which is main(),
and all the most trivial programs can define additional functions.
When the algorithm of a certain problem involves long and complex logic, it is
broken into smaller, independent and reusable blocks. These small blocks of code
are known by different names in different programming languages such as
a module, a subroutine, a function or a method.
A function declaration tells the compiler about a function's name, return type, and
parameters. A function definition provides the actual body of the function.
Program:
#include<stdio.h>
int fact(int);
void main() {
int no, factorial;
printf("Enter a number: ");
scanf("%d", &no);
factorial = fact(no);
printf("The factorial of %d is: %d\n", no, factorial);
}
int fact(int n) {
int i, f = 1;
for (i = 1; i <= n; i++) {
f = f * i;
}
return f;
}
Experiment No. 8
Title: Write a program to find the sum of digits of a number using recursion.
Aim: A program to find the sum of digits of a number using recursion.
Theory: Recursion is the process of a function calling itself repeatedly till the
given condition is satisfied. A function that calls itself directly or indirectly is
called a recursive function and such kind of function calls are called recursive
calls.
In C, recursion is used to solve complex problems by breaking them down into
simpler sub-problems. We can solve large numbers of problems using recursion in
C. For example, factorial of a number, generating Fibonacci series, generating
subsets, etc.
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Program:
#include <stdio.h>
int sumDigits(int no) {
if (no == 0) {
return 0;
}
return (no % 10) + sumDigits(no / 10);
}
int main() {
printf("%d", sumDigits(687));
return 0;
}
Experiment No. 9
Title: . Implement a program to find the largest and smallest elements in an array.
Aim: A Program to Implement a program to find the largest and smallest elements
in an array.
Theory: To find the largest and smallest elements in an array, you can sort the
array in descending or ascending order:
Descending order
The largest element will be at the end of the array, and the smallest element will be
at the beginning.
Ascending order
The largest element will be at the beginning of the array, and the smallest element
will be at the end.
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Program:
#include <stdio.h>
int main()
{
int arr[5] = {3, 1, 4, 1, 5};
int largest=arr[0];
int smallest=arr[0];
for (int i = 1; i < 5; i++)
{
if (arr[i] >largest)
{
largest=arr[i];
}
if(arr[i]<smallest){
smallest = arr[i];
}
}
printf("Largest element : %d\n", largest);
printf("Smallest element is : %d\n", smallest);
return 0;
}
Experiment No. 10
Title : Develop a C program to print the Fibonacci series using a loop.
Aim: A C program to print the Fibonacci series using a loop.
Theory: Fibonacci series is a sequence of numbers in which each number is the
sum of the two preceding ones, usually starting with 0 and 1. In mathematical
terms, the Fibonacci sequence is defined by the recurrence relation:
Procedure: Save program in a file, Compile program, debug errors, Execute or
Run program with necessary inputs. Verify the outputs obtained.
Program:
#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);//printing 0 and 1
for(i=2;i<number;++i)//loop starts from 2 because 0 and 1 are already printed
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
}