Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

M.pranathi Cdst1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 27

FACULTY OF ENGINEERING & TECHNOLOGY

BACHELOR OF TECHNOLOGY

Computational Thinking for Structure Design -1


(303105104)
1st SEMESTER
COMPUTER SCIENCE & ENGINEERING DEPARTMENT

Laboratory Manual

COMPUTATIONAL THINKING FOR STRUCTURE DESIGN -1 PRACTICAL BOOK


COMPUTER SCIENCE AND ENGINEERING DEPARTMENT

Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

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.

The Fundamental of Programming theory and laboratory courses at PARUL UNIVERSITY,


WAGHODIA, VADODARA are designed in such a way that students develop the basic
understanding of the subject in the theory classes and then try their hands on the computer learnt
during the theoretical sessions.
This book is emphatically not focused on “the syntax of C”. Understanding the fundamental
ideals, principals, and techniques is the essence of a good programmer. Only well-designed code has
a chance of becoming part of a correct, reliable, and maintainable system. Also, “the fundamentals”
are what last: they will still be essential after today’s language and tools have evolved or been
replaced.
We acknowledge the authors and publishers of all the books which we have consulted while
developing this Practical book. Hopefully this Computational Thinking for Structure Design -1 will
serve the purpose for which it has been developed.

2|P ag e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Instructions to students

1. Every student should obtain a copy of laboratory Manual.


2. Dress Code: Students must come to the laboratory wearing.
i. Trousers,
ii. half-sleeve tops and
iii. Leather shoes. Half pants, loosely hanging garments and slippers are not allowed.
3. To avoid injury, the student must take the permission of the laboratory staff before handling any
machine.
4. Students must ensure that their work areas are clean and dry to avoid slipping.
5. Do not eat or drink in the laboratory.
6. Do not remove anything from the computer laboratory without permission.
7. Do not touch, connect or disconnect any plug or cable without your lecturer/laboratory
technician’s permission.
8. All students need to perform the practical/program.

3|P ag e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

CERTIFICATE

This is to certify that

MS…M.PRANATHI with enrolment no. ..23UG032890 ............ has successfully

completed his/her laboratory experiments in the Computational Thinking for

Structure Design -1 (303105104) from the department of COMPUTER

SCIENCE & ENGINEERING during the academic year 2023--- 2024

Date of Submission:......................... Staff In charge:...........................

Head of Department:...........................................

4|P ag e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

INDEX

Class: 1st Sem Subject: - Computational Thinking for Structured Design-1


A.Y. 20 -20 Subject Code: 303105104

Date
Sr. Date of
of Marks
No Experiment Title Page Perfor Sign
Assess out 0f 10
. No. mance
ment

Practical Set – 1 To From


Installation C IDE, Basic Structure of C
1 program.Format Specifiers, Escape Character.
Run time input/Output Programs.
1. Write a c program to calculate Area of
Rectangle,Perimeter of a Rectangle and
Diagonal of a Rectangle.
2. Write a c program to calculate Area of
2
square,Perimeter of a square and Diagonal of a
square.
3. Write a c program to calculate total area
ofCylinder and volume of a cylinder.
1. The total distance traveled by vehicle in t
seconds is given by distance s = ut+1/2at?
where uand aare the initial
velocity (m/sec.) and acceleration(m/sec?).
Write a C program to find the distance traveled
at regular intervals of time given
the values of uand aThe program should
provide the flexibility to the user toselect
3
his own time intervals and repeat the
calculations fordifferent values of uand
a
2. Write a C program, which takes two integer
operands and one operator from the user,
performs the operation and then
prints the result. (Consider the operators
and use Switch Statement)

4 1. Write a C program to find the sum of


individual digits of a positive integer.

5|P ag e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

2. A Fibonacci sequence is defined as follows:


the first and second terms in the sequences are
0 and 1. Subsequent terms are
found by adding the preceding two terms in the
sequence. Write a C program to generate the
first n terms of the sequence.
3. Write a C program to generate all the prime
numbers between 1 and n, where n is a value
supplied by the user.
1. Write a C program to calculate the following
Sum: Sum=1-x^2/2! +x^4/4!-x^6/6!+x^8/8!-
5 x^10/10!.
2. Write a C program to find the roots of a
quadratic equation.
Write C programs that use both recursive and
non-recursive functions.
6 1. To find the factorial of a given integer.
2. To find the GCD (greatest common divisor)
of two given integers.
1. Write a C program to find the largest integer
in a list of integers,
7 2. Write a C program that uses functions to
perform the following:
1. Addition of Two Matrices
2. Multiplication of Two Matrices
1. Write a C program that uses functions to
perform the following operation;
1. To insert a sub-string into a given main
8 string from a given position.
2. To delete n Characters from a given position
in a given string,
2. Write a C program to determine if the given
string is a palindrome or not.
1. Write a C program that displays the
position or index in the string S where the
9 string T begins, or -1 if S doesnt contain
T.
2. Write a C program to count the lines, words
and characters in a given text.
1. Write a C program to generate Pascal's
10 triangle.
2. Write a C program to construct a pyramid of
numbers.
Write a C program to read in two numbers, x
11 and n, and then compute the sum of this
geometric progression:
1+x+x^2+x^3+... x^n.

6|P ag e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

For example: if n is 3 and x is 5, then the


program computes 1+5+25+125.Print x, n, the
sum.
Perform error checking. For example, the
formula does not make sense for negative
exponents if n is less than 0. Have
your program print an error - message if n<0,
then go back and read in the next pair of
numbers without computing the sum.
Are any values of x also illegal? If so, test for
them too.
1. 2's complement of a number is obtained by
scanning it from right to left and
complementing all the bits after the first
appearance of a 1. Thus 2s complement of
12 11100 is 00100. Write a C program to find the
2's complement of a binary
number.
2. Write a C program to convert a Roman
numeral to its decimal Equivalent.
1. Write a c program on Given an unsorted
array arr[] of size N. Rotate the array to the left
(counter-clockwise direction) by D
steps, where D is a positive integer.
2. Write a c Program on given two sorted
arrays arr1 and arr2 of size N and M
respectively and an element K. The task is to
find the element that would be at the kth
13 position of the final sorted array.Explanation:
Input :
Array 1 - 1 4 2 3 5
Array 2 - 7 8 6
k=5
Output : 5
Because The final sorted array would be -1, 2,
3, 4, 5, 6, 7, 8, The 5th element of this array is
6.

1. Write a c program to take multiline string


input and print individual string length .
14 2. Write a c program to reverse the individual
word of a given string Explanation:input :
Welcome To Bytexl output: emocleW
oT lxetyB.

7|P ag e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Practical 1
1. AIM: Installation C IDE, Basic Structure of C program. Format Specifiers,
Escape Character. Run time input/Output Programs.
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.

Sections of the C Program


There are 6 basic sections responsible for the proper execution of a program. Sections are
mentioned below:

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:

description, name of the program, programmer name, date, time etc.


*/
Anything written as comments will be treated as documentation of the program and this will not
interfere with the given code. Basically, it gives an overview to the reader of the program.

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:

8|P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1
#include<stdio.h>
#include<math.h>

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:

#define long long ll

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:

int num = 18;

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.

9|P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Example:

int sum(int x, int y)


{
return x+y;
}

List of Format Specifiers in C


The below table contains the most commonly used format specifiers in C

Format Specifier Description

%c For b type.

%d For signed integer type.

%e or %E For scientific notation of floats.

%f For float type.

%g or %G For float type with the current precision.

%i Unsigned integer

%ld or %li Long

%lf Double

%Lf Long double

%lu Unsigned int or unsigned long

%lli or %lld Long long

%llu Unsigned long long

%o Octal representation

10 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Format Specifier Description

%p Pointer

%s String

%u Unsigned int

%x or %X Hexadecimal representation

%n Prints nothing

%% Prints % character

Escape Sequence List

The table below lists some common escape sequences in C language.

Escape
Sequence Name Description

\a Alarm or Beep It is used to generate a bell sound in the C program.

\b Backspace It is used to move the cursor one place backward.

It is used to move the cursor to the start of the next logical


\f Form Feed
page.

\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.

It inserts some whitespace to the left of the cursor and


\t Horizontal Tab
moves the cursor accordingly.

\v Vertical Tab It is used to insert vertical space.

\\ Backlash Use to insert backslash character.

11 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Escape
Sequence Name Description

\’ Single Quote It is used to display a single quotation mark.

\” Double Quote It is used to display double quotation marks.

\? Question Mark It is used to display a question mark.

\ooo Octal Number It is used to represent an octal number.

Hexadecimal
\xhh It represents the hexadecimal number.
Number

\0 NULL It represents the NULL character.

Example:

#include <stdio.h>
int main() {
inside q// printf() displays the string uotation
printf("Hello, World!");
return 0;
}

12 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Practical 2
1. Write a c program to calculate Area of Rectangle , Perimeter of a
Rectangle and Diagonal of a Rectangle.

#include <stdio.h>
int main() {
int length, breadth, area, perimeter, diagonal;

printf("Enter the length of the rectangle: ");


scanf("%d", &length);

printf("Enter the breadth of the rectangle: ");


scanf("%d", &breadth);

area = length * breadth;


perimeter = 2 * (length + breadth);
diagonal = sqrt(length * length + breadth * breadth);

printf("The area of the rectangle is %d\n", area);


printf("The perimeter of the rectangle is %d\n", perimeter);
printf("The diagonal of the rectangle is %d\n", diagonal);

return 0;
}

13 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

2. Write a c program to calculate Area of square ,Perimeter of a square and


Diagonal of a square.

#include<stdio.h>

#include<math.h>

int main()

float a,area,perimeter,diagonal;

printf("enter the length of side:");

scanf("%f",&a);

area=a*a;

perimeter=4*a;

diagonal=sqrt(2*a);

printf("area of square= %0.2f\n",area);

printf(" perimeter of square=%0.2f\n",perimeter);

printf(" diagonals of square =%0.2f\n",diagonal);

return 0;

14 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

3. Write a c program to calculate total area of Cylinder and volume of a


cylinder.
#include <stdio.h>

int main() {

double radius, height, surface_area, volume;

printf("Enter the radius and height of the cylinder: ");

scanf("%lf%lf", &radius, &height);

surface_area = 2 * 3.14 * radius * (radius + height);

volume = 3.14 * radius * radius * height;

printf("Surface area = %.2lf\n", surface_area);

printf("Volume = %.2lf\n", volume);

return 0;

15 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Practical-3
1. The total distance traveled by vehicle in t seconds is given by distance s =
ut+1/2at? where u and a are the initial velocity (m/sec.) and acceleration
(m/sec?). Write a C program to find the distance traveled at regular intervals of
time given the values of u and a The program should provide the flexibility to
the user to select his own time intervals and repeat the calculations for different
values of u and a ?

#include<stdio.h>

Int main()

int a,u,t,t1,t2,i;

float s;

clrscr();

printf("ENTER THE VALUES OF a,u,t,t1,t2:");

scanf("%d%d%d%d%d",&a,&u,&t,&t1,&t2);

for(i=t1;i<=t2;i=i+t)

s=(u*i)+(0.5*a*i*i);

printf("\n\nthe distance travelled in %d seconds is %f ",i,s);

return 0;

Output:

16 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

17 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

2. Write a C program, which takes two integer operands and one operator from
the user, performs the operation and then prints the result. (Consider the
operators and use Switch Statement)?

//C program which takes two integer operands and one operator from the user, performs the
operation and then prints the result
#include<stdio.h>
#include<math.h>
int main()
{
int a, b;
char op;
printf("Enter two integer values: ");
scanf("%d%d",&a,&b);
printf("Enter a operand + or - or * or / or %: ");
scanf("%c",&op);
switch(op)
{
case '+':
printf("Result is: %d",(a+b));
break;
case '-':
printf("Result is: %d",(a-b));
break;
case '*':
printf("Result is: %d",(a*b));
break;

18 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1
case '/':
printf("Result is: %d",(a/b));
break;
default:
printf("Invalid operand!");
}
Return 0;
}
Output:

19 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Practical-4
1. Write a C program to find the sum of individual digits of a positive integer.

#include<stdio.h>

#include<math.h>

int main()

int n,sum=0;

printf("enter a +ve integer:");

scanf("%d",&n);

while(n>0)

sum=sum+n%10;

n=n/10;

printf("sum of individual digits of a positive integer is %d",sum);

20 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

2. A Fibonacci sequence is defined as follows: the first and second terms in the
sequences are 0 and 1. Subsequent terms are found by adding the preceding two
terms in the sequence. Write a C program to generate the first n terms of the
sequence.

#include <stdio.h>

int main() {

int i, n;

int t1 = 0, t2 = 1;

int nextTerm = t1 + t2;

printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: %d, %d, ", t1, t2);

for (i = 3; i <= n; ++i) {

printf("%d, ", nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

return 0;

21 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

3. Write a C program to generate all the prime numbers between 1 and n,


where n is a value supplied by the user?

#include <stdio.h>

int main() {

int i,num,n,count;

printf(“enter the range”);

scanf(“%d”,&n);

printf(“the prime num in between the range 1to %d;”,n);

for(num=1;num<=n;num++)

count=0;

for(i=2;i<=num/2;i++)

{ if(num%i= =0)

{ count++;

break;

if(count= = 0&&num!=1)

printf(“%d“,num);

22 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Practical-5
1. Write a C program to calculate the following Sum: Sum=1-x^2/2! +x^4/4!-
x^6/6!+x^8/8!-x^10/10!.
#include <stdio.h>

#include <math.h>

int main()

int counter,f_coun;

float sum=0,x,power,fact;

printf("\tEQUATION SERIES : 1- X^2/2! + X^4/4! - X^6/6! + X^8/8! - X^10/10!");

printf("\n\tENTER VALUE OF X : ");

scanf("%f",&x);

for(counter=0, power=0; power<=10; counter++,power=power+2)

fact=1;

for(f_coun=power; f_coun>=1; f_coun--)

fact *= f_coun;

sum=sum+(pow(-1,counter)*(pow(x,power)/fact));

printf("SUM : %f",sum);

23 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

2. Write a C program to find the roots of a quadratic equation.


# include<stdio.h>

# include<math.h>

int main () {

float a,b,c,r1,r2,d;

printf ("Enter the values of a b c: ");

scanf (" %f %f %f", &a, &b, &c);

d= b*b - 4*a*c;

if (d>0) {

r1 = -b+sqrt (d) / (2*a);

r2 = -b-sqrt (d) / (2*a);

printf ("The real roots = %f %f", r1, r2);

else if (d==0) {

r1 = -b/(2*a);

r2 = -b/(2*a);

printf ("Roots are equal =%f %f", r1, r2);

else

printf("Roots are imaginary");

return 0;

24 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

Practical 6
Write C programs that use both recursive and non-recursive functions.
1. To find the factorial of a given integer.

#include<stdio.h>

int fact(int n)

if(n==0)

return 1;

else

return n*fact(n-1);

void main()

int n,f;

printf("\n enter a num: ");

scanf("%d",&n);

f=fact(n);

printf("\n factorial of %d is %d",n,f);

return 0;

25 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

non-recursive functions:
#include<stdio.h>

int fact(int n)

int f=1,i;

if((n==0)||(n==1))

return(1);

else

for(i=1;i<=n;i++)

f=f*i;

return(f);

void main()

int n;

printf("enter the number :");

scanf("%d",&n);

printf("factoria of number%d",fact(n));

return 0;

26 | P a g e
Enrollment no:23UG032890
Faculty of Engineering & Technology
303105104 – Computational Thinking for Structured
Design-1

2. To find the GCD (greatest common divisor) of two given integers.


#include<stdio.h>

int hcf(int n1,int n2);

int main ()

int n1,n2;

printf("enter two positive integer :");

scanf("%d %d", &n1,&n2);

printf("G.C.D of %d and %d is %d ",n1,n2,hcf(n1,n2));

return 0;

int hcf(int n1, int n2)

if(n2 !=0)

return hcf(n2,n1%n2);

else

return n1;

27 | P a g e
Enrollment no:23UG032890

You might also like