0% found this document useful (0 votes)
17 views66 pages

CTSD Manual

The document is a laboratory manual for the 'Computational Thinking for Structure Design -1' course at Parul University, aimed at first-year B.Tech students in the Computer Science and Engineering department. It outlines the course's objectives, provides instructions for laboratory conduct, and includes a comprehensive list of practical programming exercises in C. The manual emphasizes the importance of understanding programming fundamentals over mere syntax, aiming to equip students with essential skills for programming and system design.

Uploaded by

hemu171807
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)
17 views66 pages

CTSD Manual

The document is a laboratory manual for the 'Computational Thinking for Structure Design -1' course at Parul University, aimed at first-year B.Tech students in the Computer Science and Engineering department. It outlines the course's objectives, provides instructions for laboratory conduct, and includes a comprehensive list of practical programming exercises in C. The manual emphasizes the importance of understanding programming fundamentals over mere syntax, aiming to equip students with essential skills for programming and system design.

Uploaded by

hemu171807
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
You are on page 1/ 66

‭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‬
‭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.‬

‭Enrollment No:23UG033506‬
‭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.‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭CERTIFICATE‬

‭This is to certify that‬

‭Mr./Ms………..................................................................................................... with‬

‭enrolment no. ................................................................ has successfully completed‬

‭his/her laboratory experiments in the‬‭Computational‬‭Thinking for Structure‬

‭Design -1 (303105104)‬ ‭from the department of‬

‭........................................................... during the academic year ................................‬

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

‭Head of Department:...........................................‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭INDEX‬

‭ lass: 1st‬ ‭Sem‬


C ‭Subject: - Computational Thinking‬‭for Structured Design-1‬
‭A.Y. 20__-20__‬ ‭Subject Code: 303105104‬

‭Date‬
‭ r.‬
S ‭ ate of‬
D ‭ arks‬
M
‭of‬
‭No‬ ‭Experiment Title‬ ‭Page‬ ‭Perfor‬ ‭out 0f‬ ‭Sign‬
‭ sses‬
A
‭.‬ ‭No.‬ ‭mance‬ ‭10‬
‭sment‬

‭Practical Set – 1‬ ‭To‬ ‭From‬

I‭ nstallation 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‬‭􀍚‭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‬
‭3‬
‭select his own time intervals and repeat the‬
‭calculations fordifferent values of‬‭􀍚‬‭u‬‭􀍛􀀃‬‭and‬
‭􀍚‭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)‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
1‭ . Write a C program to find the sum of‬
‭individual digits of a positive integer.‬
‭2. A Fibonacci sequence is defined as follows:‬
‭the first and second terms in the sequences are‬
‭0 and 1. Subsequent terms are‬
‭4‬ ‭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!‬
‭5‬ ‭+x^4/4!-x^6/6!+x^8/8!-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 doesn􀍛t 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.‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭ or example: if n is 3 and x is 5, then the‬
F
‭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 2􀍛s 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 k􀍛th‬
‭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.‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭Practical 1‬

‭1.‬ I‭ nstallation C IDE, Basic Structure of C program. Format Specifiers, Escape Character.‬
‭Run time input/Output Programs.‬

‭ he basic structure of a C program is divided into 6 parts which makes it easy to read, modify,‬
T
‭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.‬

‭ ections of the C Program‬


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

a‭ .‬ ‭ ocumentation‬
D
‭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.‬
‭/*‬
‭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:‬

#‭ include<stdio.h>‬
‭#include<math.h>‬
‭3. Definition‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭ reprocessors are the programs that process our source code before the process of compilation.‬
P
‭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:‬

v‭ oid 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‭nt sum(int x, int y)‬


‭{‬
‭return x+y;‬
‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭ ist of Format Specifiers in C‬
L
‭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.‬

‭ or float type with the current‬


F
‭%g or %G‬
‭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‬

‭%p‬ ‭Pointer‬

‭%s‬ ‭String‬

‭%u‬ ‭Unsigned int‬

‭%x or %X‬ ‭Hexadecimal representation‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭Format Specifier‬ ‭Description‬

‭%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.‬

I‭ t 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.‬

I‭ t 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.‬

‭\’‬ ‭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.‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭Escape‬
‭Sequence‬ ‭Name‬ ‭Description‬

‭ exadecimal‬
H
‭\xhh‬ ‭It represents the hexadecimal number.‬
‭Number‬

‭\0‬ ‭NULL‬ ‭It represents the NULL character.‬

‭ xample:‬
E
‭#include <stdio.h>‬
‭int main() {‬
‭// printf() displays the string inside quotation‬
‭printf("Hello, World!");‬
‭return 0;‬
‭}‬

‭Enrollment No:23UG033506‬
‭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.‬

‭Input:‬
‭#include <stdio.h>‬

‭#include <math.h>‬

‭int main()‬

‭{‬

‭float length, breadth, perimeter, diagonal, area;‬

‭printf("Enter the length of the rectangle:\n");‬

‭scanf("%f", &length);‬

‭printf("Enter the breadth of the rectangle:\n");‬

‭scanf("%f", &breadth);‬

‭area = length * breadth;‬

‭perimeter = 2 * (length + breadth);‬

‭diagonal = sqrt(length * length + breadth * breadth);‬

‭printf("The area of the rectangle is %0.3f\n", area);‬

‭printf("The perimeter of the rectangle is %0.3f\n", perimeter);‬

‭printf("The diagonal of the rectangle is %0.3f\n", diagonal);‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬

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


‭square.‬

‭Input:‬
‭#include <stdio.h>‬

‭#include <math.h>‬

‭int main()‬

‭{‬

‭float side,perimeter,diagonal,area;‬

‭printf("Enter the side of the square:\n");‬

‭scanf("%f", &side);‬

‭area = side*side;‬

‭perimeter = 4*side;‬

‭diagonal = sqrt(2)*side;‬

‭printf("The area of the square is %0.4f\n", area);‬

‭printf("The perimeter of the square is %0.4f\n", perimeter);‬

‭printf("The diagonal of the square is %0.4f\n", diagonal);‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬

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

‭Input:‬
‭#include <stdio.h>‬

‭#include <math.h>‬

‭int main()‬

‭{‬

‭float height, radius, volume, area;‬

‭printf("Enter the radius of the cylinder:\n");‬

‭scanf("%f", &radius);‬

‭printf("Enter the breadth of the cylinder:\n");‬

‭scanf("%f", &height);‬

‭area = 2 * 3.14 * radius * (height + radius);‬

‭volume = 3.14 * radius * radius * height;‬

‭printf("The Total surface area of the cylinder is %0.3f\n", area);‬

‭printf("The volume of the cylinder is %0.3f\n", volume);‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬

‭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.‬

‭Input:‬
‭#include<stdio.h>‬

‭#include<math.h‬

‭> void main()‬

‭{‬

‭int n, sec;‬

‭float d, u, a;‬

‭printf("Enter the time in seconds \n");‬

‭scanf("%d", &sec);‬

‭printf("Enter the velocity \n");‬

‭scanf("%f", &u);‬

‭printf("Enter the acceleration \n");‬

‭scanf("%f", &a);‬

‭d = (u * sec + (a * (pow(sec, 2))) / 2);‬

‭printf("Total distance travelled is %.2f", d);‬

‭}‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬

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

‭Input:‬

‭#include <stdio.h>‬

‭int main()‬

‭{‬

‭int a, b, c;‬

‭char ch;‬

‭printf("Enter the values of a and b\n");‬

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

‭printf("Enter your operator(+, -, *, /, %)\n");‬

‭scanf(" %c", &ch);‬

‭switch (ch)‬

‭{‬

‭case '+':‬

‭c = a + b;‬

‭printf("addition of two numbers is %d", c);‬

‭break;‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭case '-':‬

‭c = a - b;‬

‭printf("substraction of two numbers is %d", c);‬

‭break;‬

‭case '*':‬

‭c = a * b;‬

‭printf("multiplication of two numbers is %d", c);‬

‭break;‬

‭case '%':‬

‭c = a % b;‬

‭printf("remainder of two numbers is %d", c);‬

‭break;‬

‭default:‬

‭printf("Invalid operator");‬

‭break;‬

‭}‬

‭return 0;‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭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.‬

‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭int main() {‬

‭int num, sum = 0, digit;‬

‭// Input a positive integer‬

‭printf("Enter a positive integer: ");‬

‭scanf("%d", &num);‬

‭// Check if the input is positive‬

‭if (num <= 0) {‬

‭printf("Please enter a positive integer.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭// Calculate the sum of individual digits‬

‭while (num > 0) {‬

‭digit = num % 10; // Get the last digit‬

‭sum += digit; // Add the last digit to the sum‬

‭num = num / 10; // Remove the last digit‬

‭}‬

‭// Display the result‬

‭printf("The sum of individual digits is: %d\n", sum);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭return 0; // Exit successfully‬

‭}‬

‭Out put:‬

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‬

‭Input:‬

‭#include <stdio.h>‬

‭int main() {‬

‭int n, i;‬

‭long long int first = 0, second = 1, next;‬

‭printf("Enter the number of terms in the Fibonacci sequence: ");‬

‭scanf("%d", &n);‬

‭if (n <= 0) {‬

‭printf("Please enter a positive integer.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭printf("Fibonacci sequence for the first %d terms:\n", n);‬

‭if (n >= 1) {‬

‭printf("%lld ", first);‬

‭}‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭if (n >= 2) {‬

‭printf("%lld ", second);‬

‭}‬

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

‭next = first + second;‬

‭printf("%lld ", next);‬

‭first = second;‬

‭second = next;‬

‭}‬

‭printf("\n");‬

‭return 0; // Exit successfully‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭ . Write a c programme to generate all the prime numbers between 1 and n ,‬


3
‭where n is a value supplied by the user.‬

‭Input:‬
‭#include <stdio.h>‬

‭#include <stdbool.h>‬

‭bool is_prime(int num) {‬

‭if (num <= 1) {‬

‭return false;‬

‭}‬

‭for (int i = 2; i * i <= num; i++) {‬

‭if (num % i == 0) {‬

‭return false;‬

‭}‬

‭}‬

‭return true;‬

‭}‬

‭int main() {‬

‭int n;‬

‭printf("Enter a positive integer (n): ");‬

‭scanf("%d", &n);‬

‭if (n < 2) {‬

‭printf("There are no prime numbers in the range [1, %d].\n", n);‬

‭return 1;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭printf("Prime numbers between 1 and %d are:\n", n);‬

‭for (int i = 2; i <= n; i++) {‬

‭if (is_prime(i)) {‬

‭printf("%d ", i);‬

‭}‬

‭}‬

‭printf("\n");‬

‭return 0;‬

‭}‬

‭Out put:‬

‭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!.‬
‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <math.h>‬

‭int factorial(int n) {‬

‭if (n == 0) {‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭return 1;‬

‭} else {‬

‭return n * factorial(n - 1);‬

‭}‬

‭}‬

‭int main() {‬

‭int n;‬

‭double x, sum = 0;‬

‭printf("Enter the value of x: ");‬

‭scanf("%lf", &x);‬

‭printf("Enter the number of terms (n, must be even): ");‬

‭scanf("%d", &n);‬

‭if (n % 2 != 0) {‬

‭printf("The number of terms (n) must be even.\n");‬

‭return 1;‬

‭}‬

‭for (int i = 0; i <= n; i += 2) {‬

‭double term = pow(-1, i / 2) * pow(x, i) / factorial(i);‬

‭sum += term;‬

‭}‬

‭printf("Sum = %lf\n", sum);‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭Out put:‬

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


‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <math.h>‬

‭int main() {‬

‭double a, b, c;‬

‭double discriminant, root1, root2;‬

‭printf("Enter the coefficients of the quadratic equation (a, b, c): ");‬

‭scanf("%lf %lf %lf", &a, &b, &c);‬

‭// Calculate the discriminant‬

‭discriminant = b * b - 4 * a * c;‬

‭// Check the value of the discriminant‬

‭if (discriminant > 0) {‬

‭// Two real and distinct roots‬

‭root1 = (-b + sqrt(discriminant)) / (2 * a);‬

‭root2 = (-b - sqrt(discriminant)) / (2 * a);‬

‭printf("Root 1 = %.2lf\n", root1);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭printf("Root 2 = %.2lf\n", root2);‬

‭} else if (discriminant == 0) {‬

‭// One real root (repeated)‬

‭root1 = -b / (2 * a);‬

‭printf("Root 1 = Root 2 = %.2lf\n", root1);‬

‭} else {‬

‭// Complex roots‬

‭double realPart = -b / (2 * a);‬

‭double imaginaryPart = sqrt(-discriminant) / (2 * a);‬

‭printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);‬

‭printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);‬

‭}‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭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.‬

‭Recursion‬

‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭unsigned long long int factorial_recursive(int n) {‬

‭if (n == 0) {‬

‭return 1;‬

‭} else {‬

‭return n * factorial_recursive(n - 1);‬

‭}‬

‭}‬

‭int main() {‬

‭int num;‬

‭printf("Enter an integer to find its factorial: ");‬

‭scanf("%d", &num);‬

‭if (num < 0) {‬

‭printf("Factorial is not defined for negative numbers.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭unsigned long long int result = factorial_recursive(num);‬

‭printf("The factorial of %d is %llu\n", num, result);‬

‭return 0;‬

‭}‬
‭Out put:‬

‭Non- recursion‬
‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭unsigned long long int factorial_non_recursive(int n) {‬

‭unsigned long long int result = 1;‬

‭for (int i = 1; i <= n; i++) {‬

‭result *= i;‬

‭}‬

‭return result;‬

‭}‬

‭int main() {‬

‭int num;‬

‭printf("Enter an integer to find its factorial: ");‬

‭scanf("%d", &num);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭if (num < 0) {‬

‭printf("Factorial is not defined for negative numbers.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭unsigned long long int result = factorial_non_recursive(num);‬

‭printf("The factorial of %d is %llu\n", num, result);‬

‭return 0;‬

‭}‬

‭Out put:‬

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

‭Recursion‬

‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭int gcd_recursive(int a, int b) {‬

‭if (b == 0) {‬

‭return a;‬

‭} else {‬

‭return gcd_recursive(b, a % b);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭}‬

‭}‬

‭int main() {‬

‭int num1, num2;‬

‭printf("Enter the first integer: ");‬

‭scanf("%d", &num1);‬

‭printf("Enter the second integer: ");‬

‭scanf("%d", &num2);‬

‭if (num1 < 0 || num2 < 0) {‬

‭printf("GCD is not defined for negative numbers.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭int result = gcd_recursive(num1, num2);‬

‭printf("The GCD of %d and %d is %d\n", num1, num2, result);‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Non- recursion‬
‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭int gcd_non_recursive(int a, int b) {‬

‭while (b != 0) {‬

‭int temp = b;‬

‭b = a % b;‬

‭a = temp;‬

‭}‬

‭return a;‬

‭}‬

‭int main() {‬

‭int num1, num2;‬

‭printf("Enter the first integer: ");‬

‭scanf("%d", &num1);‬

‭printf("Enter the second integer: ");‬

‭scanf("%d", &num2);‬

‭if (num1 < 0 || num2 < 0) {‬

‭printf("GCD is not defined for negative numbers.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭int result = gcd_non_recursive(num1, num2);‬

‭printf("The GCD of %d and %d is %d\n", num1, num2, result);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭return 0;‬

‭}‬

‭Out put:‬

‭Practical 7‬

‭1.‬ ‭Write a C program to find the largest integer in a list of integers.‬


‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭int main() {‬

‭int n, max, num;‬

‭// Ask the user for the number of integers‬

‭printf("Enter the number of integers: ");‬

‭scanf("%d", &n);‬

‭// Check if the number of integers is valid‬

‭if (n <= 0) {‬

‭printf("Invalid input. Please enter a positive number of integers.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭// Ask the user to enter the first integer‬

‭printf("Enter integer 1: ");‬

‭scanf("%d", &max);‬

‭// Iterate through the remaining integers‬

‭for (int i = 2; i <= n; i++) {‬

‭printf("Enter integer %d: ", i);‬

‭scanf("%d", &num);‬

‭// Check if the current number is greater than the current maximum‬

‭if (num > max) {‬

‭max = num;‬

‭}‬

‭}‬

‭// Print the largest integer‬

‭printf("The largest integer is: %d\n", max);‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭2.‬ ‭Write a C program that uses functions to perform the following:‬


‭1.‬ ‭Addition of Two Matrices‬
‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭void inputMatrix(int rows, int cols, int matrix[][cols]) {‬

‭for (int i = 0; i < rows; i++) {‬

‭for (int j = 0; j < cols; j++) {‬

‭printf("Enter element at row %d, column %d: ", i + 1, j + 1);‬

‭scanf("%d", &matrix[i][j]);‬

‭}‬

‭}‬

‭}‬

‭ oid addMatrices(int rows, int cols, int matrix1[][cols], int matrix2[][cols], int‬
v
‭result[][cols]) {‬

‭for (int i = 0; i < rows; i++) {‬

‭for (int j = 0; j < cols; j++) {‬

‭result[i][j] = matrix1[i][j] + matrix2[i][j];‬

‭}‬

‭}‬

‭}‬

‭void displayMatrix(int rows, int cols, int matrix[][cols]) {‬

‭for (int i = 0; i < rows; i++) {‬

‭for (int j = 0; j < cols; j++) {‬

‭printf("%d\t", matrix[i][j]);‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭}‬

‭printf("\n");‬

‭}‬

‭}‬

‭int main() {‬

‭int rows, cols;‬

‭printf("Enter the number of rows: ");‬

‭scanf("%d", &rows);‬

‭printf("Enter the number of columns: ");‬

‭scanf("%d", &cols);‬

‭int matrix1[rows][cols];‬

‭int matrix2[rows][cols];‬

‭int result[rows][cols];‬

‭printf("Enter elements of the first matrix:\n");‬

‭inputMatrix(rows, cols, matrix1);‬

‭printf("Enter elements of the second matrix:\n");‬

‭inputMatrix(rows, cols, matrix2);‬

‭addMatrices(rows, cols, matrix1, matrix2, result);‬

‭printf("The result of matrix addition is:\n");‬

‭displayMatrix(rows, cols, result);‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬
‭Matrix-1‬

‭Matrix-2‬

‭The program will display the result of the addition:‬

‭2.‬ ‭Multiplication of Two Matrices‬

‭Input:‬

‭#include <stdio.h>‬

‭void inputMatrix(int rows, int cols, int matrix[][cols]) {‬

‭for (int i = 0; i < rows; i++) {‬

‭for (int j = 0; j < cols; j++) {‬

‭printf("Enter element at row %d, column %d: ", i + 1, j + 1);‬

‭scanf("%d", &matrix[i][j]);‬

‭}‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭}‬

‭}‬

‭ oid multiplyMatrices(int rows1, int cols1, int matrix1[][cols1], int rows2, int cols2, int‬
v
‭matrix2[][cols2], int result[][cols2]) {‬

‭for (int i = 0; i < rows1; i++) {‬

‭for (int j = 0; j < cols2; j++) {‬

‭result[i][j] = 0;‬

‭for (int k = 0; k < cols1; k++) {‬

‭result[i][j] += matrix1[i][k] * matrix2[k][j];‬

‭}‬

‭}‬

‭}‬

‭}‬

‭void displayMatrix(int rows, int cols, int matrix[][cols]) {‬

‭for (int i = 0; i < rows; i++) {‬

‭for (int j = 0; j < cols; j++) {‬

‭printf("%d\t", matrix[i][j]);‬

‭}‬

‭printf("\n");‬

‭}‬

‭}‬

‭int main() {‬

‭int rows1, cols1, rows2, cols2;‬

‭printf("Enter the number of rows for matrix 1: ");‬

‭scanf("%d", &rows1);‬

‭printf("Enter the number of columns for matrix 1: ");‬


‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭scanf("%d", &cols1);‬

‭printf("Enter the number of rows for matrix 2: ");‬

‭scanf("%d", &rows2);‬

‭printf("Enter the number of columns for matrix 2: ");‬

‭scanf("%d", &cols2);‬

‭if (cols1 != rows2) {‬

‭ rintf("Matrix multiplication is not possible. The number of columns in matrix 1 must‬


p
‭be equal to the number of rows in matrix 2.\n");‬

‭return 1; // Exit with an error code‬

‭}‬

‭int matrix1[rows1][cols1];‬

‭int matrix2[rows2][cols2];‬

‭int result[rows1][cols2];‬

‭printf("Enter elements of matrix 1:\n");‬

‭inputMatrix(rows1, cols1, matrix1);‬

‭printf("Enter elements of matrix 2:\n");‬

‭inputMatrix(rows2, cols2, matrix2);‬

‭multiplyMatrices(rows1, cols1, matrix1, rows2, cols2, matrix2, result);‬

‭printf("The result of matrix multiplication is:\n");‬

‭displayMatrix(rows1, cols2, result);‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬
‭Matrix-1 (2x3):‬

‭Matrix-2 (3x2):‬

‭The program will display the result of the multiplication‬‭:‬

‭Practical 8‬
‭1. Write a C program that uses functions to perform the following operation;‬

‭1. To insert a sub-string into a given main string from a given position.‬

‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <string.h>‬

‭void insertSubstring(char *mainString, const char *subString, int position) {‬

‭int mainLen = strlen(mainString);‬

‭int subLen = strlen(subString);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭if (position < 0 || position > mainLen) {‬

‭printf("Invalid position for insertion.\n");‬

‭return;‬

‭}‬

‭if (mainLen + subLen >= 255) {‬

‭printf("Resulting string would be too long.\n");‬

‭return;‬

‭}‬

‭// Shift characters to make space for the sub-string‬

‭for (int i = mainLen; i >= position; i--) {‬

‭mainString[i + subLen] = mainString[i];‬

‭}‬

‭// Copy the sub-string into the main string‬

‭for (int i = 0; i < subLen; i++) {‬

‭mainString[position + i] = subString[i];‬

‭}‬

‭}‬

‭int main() {‬

‭char mainString[255];‬

‭char subString[255];‬

‭int position;‬

‭printf("Enter the main string: ");‬

‭fgets(mainString, sizeof(mainString), stdin);‬

‭mainString[strcspn(mainString, "\n")] = '\0'; // Remove newline character‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭printf("Enter the sub-string to insert: ");‬

‭fgets(subString, sizeof(subString), stdin);‬

‭subString[strcspn(subString, "\n")] = '\0'; // Remove newline character‬

‭printf("Enter the position to insert the sub-string: ");‬

‭scanf("%d", &position);‬

‭insertSubstring(mainString, subString, position);‬

‭printf("Resulting string: %s\n", mainString);‬

‭return 0;‬

‭}‬

‭Out put:‬

‭2.‬ ‭To delete n Characters from a given position in a given string,‬


‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <string.h>‬

‭void deleteCharacters(char *str, int position, int n) {‬

‭int strLen = strlen(str);‬

‭if (position < 0 || position >= strLen) {‬

‭printf("Invalid position for deletion.\n");‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭return;‬

‭}‬

‭if (n <= 0) {‬

‭printf("Invalid number of characters to delete.\n");‬

‭return;‬

‭}‬

‭if (position + n >= strLen) {‬

‭str[position] = '\0'; // If deletion extends beyond the string length, truncate the string.‬

‭} else {‬

‭// Shift characters to delete 'n' characters‬

‭for (int i = position; i <= strLen - n; i++) {‬

‭str[i] = str[i + n];‬

‭}‬

‭}‬

‭}‬

‭int main() {‬

‭char inputString[255];‬

‭int position, n;‬

‭printf("Enter a string: ");‬

‭fgets(inputString, sizeof(inputString), stdin);‬

‭inputString[strcspn(inputString, "\n")] = '\0'; // Remove newline character‬

‭printf("Enter the position to start deletion: ");‬

‭scanf("%d", &position);‬

‭printf("Enter the number of characters to delete: ");‬

‭scanf("%d", &n);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭deleteCharacters(inputString, position, n);‬

‭printf("Resulting string: %s\n", inputString);‬

‭return 0;‬

‭}‬

‭Out put:‬

‭2.‬ ‭Write a C program to determine if the given string is a palindrome or not.‬

‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <string.h>‬

‭#include <stdbool.h>‬

‭// Function to check if a string is a palindrome‬

‭bool isPalindrome(const char *str) {‬

‭int length = strlen(str);‬

‭for (int i = 0; i < length / 2; i++) {‬

‭if (str[i] != str[length - i - 1]) {‬

‭return false;‬

‭}‬

‭}‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭return true;‬

‭}‬

‭int main() {‬

‭char inputString[255];‬

‭printf("Enter a string: ");‬

‭fgets(inputString, sizeof(inputString), stdin);‬

‭inputString[strcspn(inputString, "\n")] = '\0'; // Remove newline character‬

‭if (isPalindrome(inputString)) {‬

‭printf("The string is a palindrome.\n");‬

‭} else {‬

‭printf("The string is not a palindrome.\n");‬

‭}‬

‭return 0;‬

‭}‬

‭Out put:‬

‭ owever, if you enter a non-palindromic string, such as "hello," the program will‬
H
‭output:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Practical 9‬

‭1.‬ ‭Write a C program that displays the position or index in the string S where the string T‬
‭begins, or -1 if S doesn’t contain T.‬

‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <string.h>‬

‭int findSubstring(const char *S, const char *T) {‬

‭int S_len = strlen(S);‬

‭int T_len = strlen(T);‬

‭for (int i = 0; i <= S_len - T_len; i++) {‬

‭int j;‬

‭for (j = 0; j < T_len; j++) {‬

‭if (S[i + j] != T[j]) {‬

‭break;‬

‭}‬

‭}‬

‭if (j == T_len) {‬

‭return i; // Substring found, return the starting index‬

‭}‬

‭}‬

‭return -1; // Substring not found‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭int main() {‬

‭char stringS[255];‬

‭char stringT[255];‬

‭printf("Enter string S: ");‬

‭fgets(stringS, sizeof(stringS), stdin);‬

‭stringS[strcspn(stringS, "\n")] = '\0'; // Remove newline character‬

‭printf("Enter string T: ");‬

‭fgets(stringT, sizeof(stringT), stdin);‬

‭stringT[strcspn(stringT, "\n")] = '\0'; // Remove newline character‬

‭int position = findSubstring(stringS, stringT);‬

‭if (position != -1) {‬

‭printf("String T begins at position %d in string S.\n", position);‬

‭} else {‬

‭printf("String T is not found in string S.\n");‬

‭}‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭2.‬‭Write a C program to count the lines, words and characters in a given text.‬
‭Input:‬
‭// Online C compiler to run C program online‬
‭#include <stdio.h>‬
‭#include <stdio.h>‬
‭#include <ctype.h>‬
‭int main() {‬
‭char text[1000];‬
‭int lines = 0, words = 0, characters = 0;‬
‭char prevChar = ' ';‬
‭printf("Enter the text (Ctrl+D to end input on Unix/Linux, Ctrl+Z on Windows):\n");‬
‭// Read the text until EOF (End of File) is encountered‬
‭while (fgets(text, sizeof(text), stdin) != NULL) {‬
‭lines++;‬
‭for (int i = 0; text[i] != '\0'; i++) {‬
‭characters++;‬
‭if (isspace(text[i])) {‬
‭if (!isspace(prevChar)) {‬
‭words++;‬
‭}‬
‭}‬
‭prevChar = text[i];‬
‭}‬
‭}‬
‭printf("\nLines: %d\nWords: %d\nCharacters: %d\n", lines, words, characters);‬

‭return 0;‬
‭}‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭Out put:‬

‭Practical 10‬

‭1.‬ ‭Write a C program to generate Pascal's triangle.‬


‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭int binomialCoefficient(int n, int k) {‬

‭if (k == 0 || k == n) {‬

‭return 1;‬

‭} else {‬

‭return binomialCoefficient(n - 1, k - 1) + binomialCoefficient(n - 1, k);‬

‭}‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭int main() {‬

‭int numRows;‬

‭printf("Enter the number of rows for Pascal's triangle: ");‬

‭scanf("%d", &numRows);‬

‭for (int n = 0; n < numRows; n++) {‬

‭for (int space = 0; space < numRows - n; space++) {‬

‭printf(" ");‬

‭}‬

‭for (int k = 0; k <= n; k++) {‬

‭printf("%4d", binomialCoefficient(n, k));‬

‭}‬

‭printf("\n");‬

‭}‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭2.‬ ‭Write a C program to construct a pyramid of numbers.‬


‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭int main() {‬

‭int rows, space, num, count = 0;‬

‭printf("Enter the number of rows for the pyramid: ");‬

‭scanf("%d", &rows);‬

‭for (int i = 1; i <= rows; i++) {‬

‭// Print spaces‬

‭for (space = 1; space <= rows - i; space++) {‬

‭printf(" ");‬

‭}‬

‭// Print numbers in ascending order‬

‭for (num = 1; num <= i; num++) {‬

‭printf("%2d ", ++count);‬

‭}‬

‭// Print numbers in descending order‬

‭for (num = i - 1; num >= 1; num--) {‬

‭printf("%2d ", ++count);‬

‭}‬

‭printf("\n");‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭return 0;‬

‭}‬
‭Out put:‬

‭Practical 11‬

1‭ .‬ ‭Write‬‭a‬‭C‬‭program‬‭to‬‭read‬‭in‬‭two‬‭numbers,‬‭x‬‭and‬‭n,‬‭and‬‭then‬‭compute‬‭the‬‭sum‬‭of‬‭this‬
‭geometric progression:‬

‭1+x+x^2+x^3+... x^n.‬

‭For example: if n is 3 and x is 5, then the program computes 1+5+25+125.Print x, n, the sum.‬

‭ erform‬ ‭error‬ ‭checking.‬ ‭For‬ ‭example,‬ ‭the‬ ‭formula‬ ‭does‬ ‭not‬ ‭make‬ ‭sense‬ ‭for‬ ‭negative‬
P
‭exponents – if n is less than 0. Have‬

y‭ our‬ ‭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.‬

‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <math.h>‬

‭int main() {‬

‭double x, sum = 1.0;‬

‭int n;‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭while (1) {‬

‭printf("Enter x and n (x n): ");‬

‭if (scanf("%lf %d", &x, &n) != 2) {‬

‭printf("Invalid input. Please enter two numbers.\n");‬

‭while (getchar() != '\n'); // Clear input buffer‬

‭continue; // Read the next pair of numbers‬

‭}‬

‭if (n < 0) {‬

‭printf("Error: n cannot be negative. Please enter a non-negative n.\n");‬

‭continue; // Read the next pair of numbers‬

‭}‬

‭if (x == 1.0) {‬

‭printf("Error: x cannot be 1. Please enter a different value for x.\n");‬

‭continue; // Read the next pair of numbers‬

‭}‬

‭if (x < -1.0 || x > 1.0) {‬

‭printf("Warning: The series may not converge for x outside the range [-1, 1].\n");‬

‭}‬

‭// Calculate the sum of the geometric progression‬

‭for (int i = 1; i <= n; i++) {‬

‭sum += pow(x, i);‬

‭}‬

‭printf("x: %.2lf, n: %d, Sum: %.2lf\n", x, n, sum);‬

‭break; // Exit the loop‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Practical 12‬
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‬‭2’s‬‭complement‬‭of‬‭11100‬‭is‬
‭00100. Write a C program to find the 2's complement of a binary number.‬

‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <string.h>‬

‭int main() {‬

‭char binaryNumber[100];‬

‭int length, i;‬

‭printf("Enter a binary number: ");‬

‭scanf("%s", binaryNumber);‬

‭length = strlen(binaryNumber);‬

‭// Find the first appearance of '1' from the right‬

‭for (i = length - 1; i >= 0; i--) {‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭if (binaryNumber[i] == '1') {‬

‭break;‬

‭}‬

‭}‬

‭// If no '1' is found, the number is already in 2's complement form‬

‭if (i == -1) {‬

‭printf("2's complement is: %s\n", binaryNumber);‬

‭return 0;‬

‭}‬

‭// Invert the bits after the first '1'‬

‭for (int j = 0; j < i; j++) {‬

‭binaryNumber[j] = (binaryNumber[j] == '0') ? '1' : '0';‬

‭}‬

‭printf("2's complement is: %s\n", binaryNumber);‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭2.‬‭Write a C program to convert a Roman numeral to its decimal Equivalent.‬


‭Input:‬
‭// Online C compiler to run C program online‬
‭#include <stdio.h>‬
‭int romanToDecimal(char romanNumeral[]) {‬
‭int decimalNum = 0;‬
‭int prevValue = 0;‬
‭for (int i = 0; romanNumeral[i] != '\0'; i++) {‬
‭int currentValue = 0;‬
‭switch (romanNumeral[i]) {‬
‭case 'I': currentValue = 1; break;‬
‭case 'V': currentValue = 5; break;‬
‭case 'X': currentValue = 10; break;‬
‭case 'L': currentValue = 50; break;‬
‭case 'C': currentValue = 100; break;‬
‭case 'D': currentValue = 500; break;‬
‭case 'M': currentValue = 1000; break;‬
‭default:‬
‭printf("Invalid Roman numeral: %c\n", romanNumeral[i]);‬
‭return -1; // Invalid input‬
‭}‬
‭if (currentValue > prevValue) {‬
‭decimalNum += (currentValue - 2 * prevValue);‬
‭} else {‬
‭decimalNum += currentValue;‬
‭}‬
‭prevValue = currentValue;‬
‭}‬
‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭return decimalNum;‬
‭}‬
‭int main() {‬
‭char romanNumeral[20];‬
‭printf("Enter a Roman numeral: ");‬
‭scanf("%s", romanNumeral);‬
‭int result = romanToDecimal(romanNumeral);‬
‭if (result != -1) {‬
‭printf("Decimal equivalent: %d\n", result);‬
‭}‬
‭return 0;‬
‭}‬
‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭Practical 13‬
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.‬

‭Input:‬
‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭void rotateLeftByD(int arr[], int N, int D) {‬

‭if (D <= 0) {‬

‭return;‬

‭}‬

‭D %= N; // Ensure D is within the array size‬

‭// Create a temporary array to store the rotated elements‬

‭int temp[D];‬

‭for (int i = 0; i < D; i++) {‬

‭temp[i] = arr[i];‬

‭}‬

‭// Shift the remaining elements to the left‬

‭for (int i = 0; i < N - D; i++) {‬

‭arr[i] = arr[i + D];‬

‭}‬

‭// Copy the rotated elements from the temporary array to the end of the original array‬

‭for (int i = N - D, j = 0; i < N; i++, j++) {‬

‭arr[i] = temp[j];‬

‭}‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭int main() {‬

‭int N, D;‬

‭printf("Enter the size of the array: ");‬

‭scanf("%d", &N);‬

‭if (N <= 0) {‬

‭printf("Invalid array size.\n");‬

‭return 1;‬

‭}‬

‭int arr[N];‬

‭printf("Enter the elements of the array:\n");‬

‭for (int i = 0; i < N; i++) {‬

‭scanf("%d", &arr[i]);‬

‭}‬

‭printf("Enter the number of steps to rotate left (D): ");‬

‭scanf("%d", &D);‬

‭rotateLeftByD(arr, N, D);‬

‭printf("Rotated array:\n");‬

‭for (int i = 0; i < N; i++) {‬

‭printf("%d ", arr[i]);‬

‭}‬

‭printf("\n");‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬

‭ .‬ ‭Write‬ ‭a‬ ‭c‬ ‭Program‬ ‭on‬ ‭given‬ ‭two‬ ‭sorted‬ ‭arrays‬ ‭arr1‬ ‭and‬ ‭arr2‬ ‭of‬ ‭size‬ ‭N‬ ‭and‬ ‭M‬
2
‭respectively‬‭and‬‭an‬‭element‬‭K.‬‭The‬‭task‬‭is‬‭to‬‭find‬‭the‬‭element‬‭that‬‭would‬‭be‬‭at‬‭the‬‭kth‬
‭position of the final sorted array.Explanation:‬

‭Input :‬

‭Array 1 - 1 4 2 3 5‬

‭Array 2 - 7 8 6‬

‭k = 5‬

‭Output : 5‬

‭ ecause‬ ‭The‬ ‭final‬ ‭sorted‬ ‭array‬‭would‬‭be‬‭-1,‬‭2,‬‭3,‬‭4,‬‭5,‬‭6,‬‭7,‬‭8,‬‭The‬‭5th‬‭element‬‭of‬‭this‬


B
‭array is 6.‬

‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭int findKthElement(int arr1[], int N, int arr2[], int M, int k) {‬

‭int i = 0, j = 0;‬

‭int mergedArr[N + M];‬

‭int mergedSize = 0;‬

‭while (i < N && j < M) {‬

‭if (arr1[i] < arr2[j]) {‬


‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭mergedArr[mergedSize++] = arr1[i++];‬

‭} else {‬

‭mergedArr[mergedSize++] = arr2[j++];‬

‭}‬

‭}‬

‭while (i < N) {‬

‭mergedArr[mergedSize++] = arr1[i++];‬

‭}‬

‭while (j < M) {‬

‭mergedArr[mergedSize++] = arr2[j++];‬

‭}‬

‭if (k > mergedSize) {‬

‭return -1; // Invalid k‬

‭}‬

‭return mergedArr[k - 1];‬

‭}‬

‭int main() {‬

‭int N, M, k;‬

‭printf("Enter the size of the first array (N): ");‬

‭scanf("%d", &N);‬

‭int arr1[N];‬

‭printf("Enter the elements of the first array in sorted order:\n");‬

‭for (int i = 0; i < N; i++) {‬

‭scanf("%d", &arr1[i]);‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭printf("Enter the size of the second array (M): ");‬

‭scanf("%d", &M);‬

‭int arr2[M];‬

‭printf("Enter the elements of the second array in sorted order:\n");‬

‭for (int i = 0; i < M; i++) {‬

‭scanf("%d", &arr2[i]);‬

‭}‬

‭printf("Enter the value of k: ");‬

‭scanf("%d", &k);‬

‭int result = findKthElement(arr1, N, arr2, M, k);‬

‭if (result != -1) {‬

‭printf("The %dth element of the final sorted array is: %d\n", k, result);‬

‭} else {‬

‭printf("Invalid value of k.\n");‬

‭}‬

‭return 0;‬

‭}‬

‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Practical 14‬

‭1.‬‭Write a c program to take multiline string input and print individual string length.‬
‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <string.h>‬

‭int main() {‬

‭char inputString[1000];‬

‭printf("Enter a multiline string (terminate with an empty line):\n");‬

‭int totalLength = 0; // To keep track of the total length of input‬

‭int lineCount = 0; // To count the number of lines‬

‭while (1) {‬

‭char line[1000];‬

‭fgets(line, sizeof(line), stdin);‬

‭if (line[0] == '\n') {‬

‭// Empty line indicates the end of input‬

‭break;‬

‭}‬

‭int lineLength = strlen(line) - 1; // Exclude the newline character‬

‭printf("Line %d: %d characters\n", ++lineCount, lineLength);‬

‭totalLength += lineLength;‬

‭}‬

‭printf("Total characters: %d\n", totalLength);‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭return 0;‬

‭}‬
‭Out put:‬

‭ .‬ ‭Write‬ ‭a‬ ‭c‬ ‭program‬ ‭to‬ ‭reverse‬ ‭the‬ ‭individual‬ ‭word‬ ‭of‬ ‭a‬ ‭given‬ ‭string‬
2
‭Explanation:input : Welcome To Bytexl output: emocleW oT lxetyB.‬
‭Input:‬

‭// Online C compiler to run C program online‬

‭#include <stdio.h>‬

‭#include <string.h>‬

‭void reverseWords(char *str) {‬

‭int length = strlen(str);‬

‭int start = 0;‬

‭for (int i = 0; i <= length; i++) {‬

‭if (str[i] == ' ' || str[i] == '\0') {‬

‭// Found a space or end of the string, reverse the word‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭for (int j = i - 1; j >= start; j--) {‬

‭printf("%c", str[j]);‬

‭}‬

‭if (str[i] == ' ') {‬

‭printf(" "); // Preserve space between words‬

‭}‬

‭start = i + 1;‬

‭}‬

‭}‬

‭}‬

‭int main() {‬

‭char inputString[1000];‬

‭printf("Enter a string: ");‬

‭fgets(inputString, sizeof(inputString), stdin);‬

‭inputString[strcspn(inputString, "\n")] = '\0'; // Remove newline character‬

‭printf("Reversed words: ");‬

‭reverseWords(inputString);‬

‭printf("\n");‬

‭return 0;‬

‭}‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬
‭Out put:‬

‭Enrollment No:23UG033506‬
‭Faculty of Engineering & Technology‬
‭303105104 - Computational Thinking for Structured Design-1‬

‭Enrollment No:23UG033506‬

You might also like