Siva CTSD Lab Manual
Siva CTSD Lab Manual
Siva CTSD Lab Manual
BACHELOR OF TECHNOLOGY
Laboratory Manual
PREFACE
It gives us immense pleasure to present the first edition of Computational Thinking for
Structure Design -1 for the B.Tech. 1st year students for PARUL UNIVERSITY.
Instructions to students
CERTIFICATE
Date of Submission:
HoD’s Signature
INDEX
CTSD - 1
Practical 1
Introduction to C Language
The basic structure of a C program is divided into 6 parts which makes it easy to read, modify,
document, and understand in a particular format. C program must follow the below-mentioned
outline in order to successfully compile and execute. Debugging is easier in a well-structured C
program.
a. Documentation
b. Preprocessor Section
c. Definition
d. Global Declaration
e. Main() Function
f. Sub Programs
1. Documentation
This section consists of the description of the program, the name of the program, and the
creation date and time of the program. It is specified at the start of the program in the form of
comments. Documentation can be represented as:
2. Preprocessor Section
All the header files of the program will be declared in the preprocessor section of the program.
Header files help us to access other’s improved code into our code. A copy of these multiple
files is inserted into our program before the process of compilation.
Example:
3. Definition
Preprocessors are the programs that process our source code before the process of compilation.
There are multiple steps which are involved in the writing and execution of the program.
Preprocessor directives start with the ‘#’ symbol. The #define preprocessor is used to create a
constant throughout the program. Whenever this name is encountered by the compiler, it is
replaced by the actual piece of defined code.
Example:
4. Global Declaration
The global declaration section contains global variables, function declaration, and static
variables. Variables and functions which are declared in this scope can be used anywhere in the
program.
Example:
5. Main() Function
Every C program must have a main function. The main() function of the program is written in
this section. Operations like declaration and execution are performed inside the curly braces of
the main program. The return type of the main() function can be int as well as void too. void()
main tells the compiler that the program will not return any value. The int main() tells the
compiler that the program will return an integer value.
Example:
void main()
or
int main()
6. Sub Programs
User-defined functions are called in this section of the program. The control of the program is
shifted to the called function whenever they are called from the main or outside the main()
function. These are specified as per the requirements of the programmer.
Example:
%i Unsigned integer
%lf Double
%o Octal representation
%p Pointer
%s String
%u Unsigned int
%x or %X Hexadecimal representation
%n Prints nothing
%% Prints % character
Escape
Sequence Name Description
\n New Line It moves the cursor to the start of the next line.
\r Carriage Return It moves the cursor to the start of the current line.
Hexadecimal
\xhh It represents the hexadecimal number.
Number
Example:
#include <stdio.h>
int main() {
// printf() displays the string inside quotation
printf("Hello, World!");
return 0;
}
OUTPUT:
Hello world
Output:-
PROGRAM 1.2:-
Print full name in 2 lines
Input:-
Output:-
PROGRAM 1.3:-
Print your name in the center of the screen
Input:-
Output:-
PROGRAM 1.4:-
Print some patterns using ‘\n &\t’
(Triangle)
Input:-
Output:-
PROGRAM 1.5:-
Print some pattern using (‘\n&\t’)
Square
Input:-
Output:-
C language has standard libraries that allow input and output in a program. The stdio.h or
standard input output library in C that has methods for input and output.
scanf()
The scanf() method, in C, reads the value from the console as per the type specified and store
it in the given address.
Syntax:
scanf("%X", &variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a
variable and & is the address operator in C, which tells the compiler to change the real value of
variableOfXType, stored at this address in the memory.
Input and output operations in C are essential for interacting with users and files. To master I/O
operations and integrate them into larger programs and data structures, the C Programming
Course Online with Data Structures covers these fundamentals with practical applications.
printf()
The printf() method, in C, prints the value passed as the parameter to it, on the console screen.
Syntax:
printf("%X", variableOfXType);
where %X is the format specifier in C. It is a way to tell the compiler what type of data is in a
variable and variableOfXType is the variable to be printed.
Integer:
Input: scanf("%d", &intVariable);
Output: printf("%d", intVariable);
Float:
Input: scanf("%f", &floatVariable);
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
Output: printf("%f", floatVariable);
Character:
Input: scanf("%c", &charVariable);
Output: printf("%c", charVariable);
Please refer Format specifiers in C for more examples.
#include <stdio.h>
int main()
{
return 0;
}
Output
Enter the integer: 10
Entered integer is: 10
#include <stdio.h>
int main()
{
return 0;
}
Output
Enter the Word: GeeksForGeeks
Entered Word is: GeeksForGeeks
Input:-
Output:-
PROGRAM 2.2:-
Finding average of 3 numbers
Input:-
Output:-
Output:-
Input:-
Output:-
Output:-
PROGRAM 2.5:-
Swapping two numbers without using third variable
Input:-
Output:-
Branching Statements:
Discussion
Branching statements allow the flow of execution to jump to a different part of the program.
The common branching statements used within other control structures include: break,
continue, return, and goto. The goto is rarely used in modular structured
programming. Additionally, we will add to our list of branching items a pre-defined function
commonly used in programming languages of: exit.
Examples break
The break is used in one of two ways; with a switch to make it act like a case structure or
as part of a looping process to break out of the loop. The following gives the appearance
that the loop will execute 8 times, but the break statement causes it to stop during the fifth
iteration.
counter = 0;
Output counter
If counter == 4
break counter += 1
continue
The following gives the appearance that the loop will print to the monitor 8 times, but the
continue statement causes it not to print number 4.
The return statement exits a function and returns to the statement where the function
was called.
Function DoSometing
statements
Return <optional return value>
goto
The goto structure is typically not accepted in good structured programming. However,
some programming languages allow you to create a label with an identifier name
followed by a colon. You use the command word goto followed by the label.
branching statements
Allow the flow of execution to jump to a different part of the program.
break
A branching statement that terminates the existing structure.
continue
A branching statement that causes a loop to stop its current iteration and begin the next
one.
exit
A predefined function used to prematurely stop a program and return to the operating
system.
goto
An unstructured branching statement that causes the logic to jump to a different place in
the program.
return
A branching statement that causes a function to jump back to the function that called it.
#include<stdio.h>
int main() {
int num1, num2, num3;
printf("enter three integers:");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2){ if (num1 > num3)
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
printf("maximum:%d\n", num1);
else
printf("maximum:%d\n", num3);
} else{ if (num2 > num3) printf("maximum:%d\n", num2);
else
printf("maximum:%d\n", num3);
}
return 0;
}
Output:
return 0; }
#include<stdio.h>
int main() {
char operator;
double num1, num2;
printf("enter an operator (+, -, *, /):");
scanf("%c", &operator);
printf("enter two numbers:");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
printf("result:%.2lf\n", num1 + num2);
break;
case '-':
printf("result:%.2lf\n", num1 - num2);
break;
case '*':
printf("result:%.2lf\n", num1 * num2);
break;
case '/':
if (num1 != 0)
printf("result:%.2lf\n", num1 / num2);
return 0;
}
}
Output:
Output:
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
3.7 Find area of rectangle, circle and square using switch case.
#include <stdio.h>
#include <math.h>
int main() {
int choice;
float area;
printf("Select the shape to calculate the area:\n");
printf("1. Rectangle\n");
printf("2. Circle\n");
printf("3. Square\n");
printf("Enter your choice (1-3): ");
scanf("%d", &choice);
switch (choice) {
case 1: { float length, width;
printf("Enter the length of the rectangle: ");
scanf("%f", &length);
printf("Enter the width of the rectangle: ");
scanf("%f", &width);
area = length * width;
printf("The area of the rectangle is: %.2f\n", area);
break; }
case 2: {
float radius;
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);
break; }
case 3: {
float side;
printf("Enter the side length of the square: ");
scanf("%f", &side);
area = side * side;
printf("The area of the square is: %.2f\n", area);
break; }
default:
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
printf("Invalid choice! Please select a valid option.\n");
break;
}
return 0;
}
Output:
Conclusion:
Practical 4
While and do-while loops are commonly used looping statements in C, C++ and Java
programming languages. Both loops allow repetitive execution of a block of code as
long as the given condition remains true. The key difference between while and do
while loo[ lies in the time when the condition is checked. o In a while loop, the
condition is checked before the loop body executes. So the body may not execute
even once if the condition is false from the beginning. o On the other hand, in a do
while loop the body is guaranteed to execute at least once because the condition is
checked after executing the body. o This ensures the body runs at least once before
checking if the loop needs to be repeated.
o A while loop is an entry controlled loop as it evaluates the condition at the start of
each iteration, before executing the loop body. If the condition is false initially, the
body will not execute even once.
o On the other hand, a do-while loop is an exit controlled loop that first executes the
body and then evaluates the condition at the end of each iteration. This ensures the
body is guaranteed to execute at least once no matter whether the condition is true
or false originally.
o In terms of syntax, a while loop condition does not use a semicolon while do-while
condition is followed by a semicolon.
o Position-wise as well, the condition comes at the beginning for while loop and at the
end for do-while loop, after the body.
o Due to guaranteed execution of the body, do-while is preferred when it is important
for the code block to run at least once irrespective of the condition.
In contrast, while loop is preferred when there is a possibility the code block needs not
run even once based on condition result.
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
A do while loop is similar to a while loop with one difference. It checks the
condition at the end of the loop body. This ensures the body executes at
least once regardless of the condition.
In both, the condition determines if the loop repeats. But while checks
first, do-while checks last, ensuring the body runs once no matter what in
do-while.
{
statements…
}
while (condition);
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
Output:
4.2 Print the sum of odd and even numbers between 51 and 550.
#include <stdio.h>
int main() { int i, sum_even = 0, sum_odd = 0;
for (i = 51; i <= 550; i++) { if (i % 2 == 0) { sum_even += i;
} else { sum_odd += i;
}
}
printf("Sum of even numbers from 51 to 550: %d\n", sum_even);
printf("Sum of odd numbers from 51 to 550: %d\n", sum_odd); return 0;
}
Output:
Output:
return 0;
}
Output:
Conclusion:
Practical 5
For loop programs
In C programming, loops are responsible for performing repetitive tasks using a short code block that executes until the
condition
holds true. In this article, we will learn about for loop in C.
for Loop in C
The for loop in C Language provides a functionality/feature to repeat a set of statements a defined number of times. The for
loop is in
itself a form of an entry-controlled loop.
Unlike the while loop and do…while loop, the for loop contains the initialization, condition, and updating statements as part of
its
syntax. It is mainly used to traverse arrays, vectors, and other data structures.
Initialization: This step initializes a loop control variable with an initial value that helps to progress the loop or helps in checking
the
condition. It acts as the index value when iterating an array or string.
Check/Test Condition: This step of the for loop defines the condition that determines whether the loop should continue
executing or
not. The condition is checked before each iteration and if it is true then the iteration of the loop continues otherwise the loop is
terminated.
Body: It is the set of statements i.e. variables, functions, etc that is executed repeatedly till the condition is true. It is enclosed
within
curly braces { }.
Updation: This specifies how the loop control variable should be updated after each iteration of the loop. Generally, it is the
incrementation (variable++) or decrementation (variable–) of the loop control variable.
How for Loop Works?
The working of for loop is mentioned below:
Step 1: Initialization is the basic step of for loop this step occurs only once during the start of the loop. During Initialization, variables are
declared, or already existing variables are assigned some value.
Step 2: During the Second Step condition statements are checked and only if the condition is the satisfied loop we can further process
otherwise loop is broken.
Step 3: All the statements inside the loop are executed.
Step 4: Updating the values of variables has been done as defined in the loop.
Continue to Step 2 till the loop breaks.
Flowchart of for Loop
c for loop flowchart
C for Loop Flow Diagram
// conditional statement
for (gfg = 1; gfg <= 5; gfg++)
{
// statement will be printed
printf("GeeksforGeeks\n");
}
Syntax
for( .. ; .. ; .. ){
for( .. ; .. ; .. ){
....
}
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
}
To know more about nested for loop refer to Nested for loop in C.
Special Conditions
1. for loop without curly braces
You already know about for loop and its syntax, when we declare a for loop without curly braces, the loop executes only one statement
which is written just after it, and the statement can not be declarative.
Example
#include <stdio.h>
int main()
{
int i;
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
This statement executes after for loop end!!!!
2. Infinite for Loop/NULL Parameter Loop
This is also a kind of for loop where the input parameters are not available or do not exist by virtue of which the loop iterates/runs
endlessly.
Example
int main()
{
int gfg = 0;
for (;;) // condition 1,2 and 3 are not entered
{
printf("GeeksforGeeks to Infinite");
}
// Return statement to tell that everything executed
// safely
return 0;
}
Output:
It is an Entry-Controlled Loop
It can iterate from an adequate number to an infinite number according to the situation.
It requires 3 conditions parameters i.e. check expression, conditional statement, and urinary operators for updation.
Its workflow is an initialization, check/test, and then updation.
FAQs on for loops in C
1. What is a loop?
Answer:
Output:-
Enrollment No: 24UG032315 Page No:
Faculty of Engineering & Technology
303105104 - Computational Thinking for Structured Design-1
PROGRAM 5.2:-
Generate fibbonacci series upto N numbers
Input:-
PROGRAM 5.3:-
Generate a multiplication table for any given number
Input:-
Output:-
PROGRAM 5.4:-
Input:-
Output:-