Professional Documents
Culture Documents
UNIT 1
1. COMPUTATIONAL THINKING
Computational thinking is an interrelated set of skills and practices for solving complex problems, a
way to learn topics in many disciplines and a necessity for fully participating in a computational
world.
Four cornerstone of computational thinking
1.Decomposition
2.Pattern recognition
3.abstraction
4.algorithm
2. DECOMPOSITION
Decomposition is an approach that seeks to break a complex problem down into simpler
parts that are easier to deal with.
It is a divide and conquer strategy.
3. ALGORITHM
An algorithm is a step by step procedure for solving any problem.
It consist of finite set of unambiguous rules(instruction) which specify a finite sequence
of operations that provides the solution to a problem.
It also a precise rule specifying how to solve a problem
An algorithm is a sequence of finite instructions, often used for calculation and data
processing.
Example:
step 1: start
step 2: Read a,b
step 3: compute c
step 4: print c
step 5;: stop
4. CHARACTERISTICS OF ALGORITHM
The algorithm should be written in sequence.
It looks like normal English
The instruction in an algorithm should be repeatedly infinitely.
Each step of an alogorithm must be exact
An algorithm must be precisely and unambiguously described, so that there remains no
uncertainity.
An algorithm must terminate
An algorithm must contain a finite number of steps in its execution.
An algorithm must be effective
An algorithm must be general
An algorithm must solve every instance of the problem
5. KEYWORD
The keyword are reserved words by the compiler.
The keywords cannot be used as variable name
All keywords must be written in lower case.
Keywords are predefined and have standard meaning.
Eg auto, double, int, struct
6. STRING
String in C language is an array of characters that is terminated by \0 (null character).
In C language the group of characters, digits and symbols enclosed within quotation marks
(“ “) are called as strings.
Example:
char str[] = "HELLO";
The length of the string is 5 and it requires 5 + 1 byte (1 extra for the null character) = 6 bytes
of memory location to store the string.
Declaration & Initialization of Strings
The general form of declaring a string is
char str_name[size];
7. VARIABLES
A variable is a container to hold data.
Variable names are just the symbolic representation of a memory location
A variables is a data name used for storing a data values
A variables may take different values at different times during the execution.
Variable name may declare based on the meaning of the operation.
Eg., int a= 10; where a is the variable name
8. ARRAY
An array is a collection of similar data elements. These data elements have the same data
type. The elements of the array are stored in consecutive memory locations and are referenced
by an index (also known as the subscript).
An array must be declared before being used. Declaring an array means specifying the
following:
• Data type—the kind of values it can store, for example, int, char, float, double.
• Name—to identify the array.
• Size—the maximum number of values that the array can hold.
9. DATARYPES
Data Type is used to define the type of value to be used in a Program. Based on the type of
value specified in the program specified amount of required Bytes will be allocated to the
variables used in the program. Data types are broadly classified into three main types. They
are as follows.
• Primary data type ( Fundamental Data Types) – integer, floating point, character, void
• Derived data type – arrays, pointer, function
• User defined data type – structure, union, enumerated data types
output:
2.3 4.5 6.7 7.8 8.9
2.3
4.5
6.7
7.8
8.9
#include<stdio.h>
#include<string.h>
void main()
{
char str[10];
printf("Enter a string");
scanf("%s", &str);
printf("%s", str);
}
output:
Enter a string apple
apple
2. By string literal
char str[ ] = "HELLO";
char str [10] = "HELLO";
UNIT 2
1. POINTERS:
A pointer is a variable that contains the memory location of another variable. Therefore, a pointer is a
variable that represents the location of a data item, such as a variable or an array element.
These applications include:
• Pointers are used to pass information back and forth between functions.
• Pointers provide an alternate way to access the individual elements of an array.
• Pointers are used to pass arrays and strings as function arguments.
• Pointers are used to create complex data structures, such as trees, linked lists, linked stacks, linked
queues, and graphs.
• Pointers are used for the dynamic memory allocation of a variable
2. POINTER DECLERATION
The general syntax of declaring pointer variables can be given as below.
data_type *ptr_name;
Here, data_type is the data type of the value that the pointer will point to.
Example:
int *pnum;
char *pch;
3. POINTER OPERATOR
Increment
Decrement
Addition
Subtraction
Comparison
4. FUNCTION
Functions are the building blocks of a C program. Functions are the Self-
contained program segment that is placed separately from the main program
to perform some specific well defined task.
5. TWO TYPES OF PARAMETER
1. Formal parameter
2. Actual parameter
The actual parameters are the parameters that are speficified in calling function. The formal
parameters are the parameters that are declared at called function.
8. USE OF FUNCTIONS IN C
Avoids redundancy in the programs.
The program will be easier to understand, maintain and debug.
Reusable codes that can be used in other programs
A large program can be divided into smaller modules. Hence, a large
project can be divided among many programmers.
9. RECURSION
A recursive function is defined as a function that calls itself to solve a
smaller version of its task until a final call is made which does not require
a call to itself. And the phenomenon is known as recursion.
Classification of recursion:
Every recursive solution has two major cases. They are
• Base case, in which the problem is simple enough to be solved directly
without making any further calls to the same function.
• Recursive case, in which first the problem at hand is divided into simpler
sub-parts.
10. USE OF STRUCTURE
Structure in c are user defined data types which are used to store group of items of non-
similar data types.
11. DIFFERENCE BETWEEN STRUCTURE AND ARRAY
ARRAY STRUCTURE
Array is collection of homogeneous data Structure is the collection of heterogeneous
data
Array data are access using index Structure element are acess using (.) dot
operator
Eg., a[6]; Ex., s.name
Array allocates static memory Tructure allocates dynamic memory
PART B
UNIT 1
1. BUILDING BLOCKS
2. STRUCTURE OF C PROGRAM
Documentation Section
Documentation section consists of comments. There are two types of comments. They
are Single line Comments and Multi line comments. Comments are used to give a meaningful
name to a C Program.
Single line comments are specified with two backslash //. Multi line comments are
specified by /* and ends with */ in different lines.
In this section we can declare the header files of the program. Header file consists of predefined
function definitions for frequently used functions. Preprocessor directives are lines included in
a program that begin with the character # and terminated with new line character, which make
them different from a typical source code text. Preprocessor directives are executed before the
compiler compiles the source code.
Definition Section:
It is used to define symbolic constants.
Global declaration section:Used to declare functions, variables or symbolic constants
globally which could be accessed anywhere in the program.
main( ) section:
The execution of C program starts from the main function. Every C program must have a main
function. It contains two parts. They are declaration part and executable part.
Example program:
( + , * ,/ )
Ternary operator Three Conditional operator
Arithmetic Operators
2.Relational Operators:
A relational operator, also known as a comparison operator, is an operator that compares two
values or expressions. Relational operators return 1(true) or 0 (false) values, depending on
whether the conditional relationship between the two operands holds or not.
Relational Operators
3. Equality operators:
C language also supports two equality operators to compare operands for strict equality or
inequality. They are equal to (= =) and not equal to (!=) operators.
Equality Operators
4. Logical Operators:
C language supports three logical operators. They are logical AND (&&), logical OR (||), and
logical NOT (!).Assume variable A holds 1 and variable B holds 0, then –
5. Assignment Operators:
The equal sign (=) is the fundamental assignment operator, C also supports other
assignment operators that provide shorthand ways to represent common variable
assignments.
Operator Description Example
= Simple assignment operator. Assigns values C = A + B will assign the
from right side operands to left side operand value of A + B to C
+= Add AND assignment operator. It adds the C += A is equivalent to C = C
right operand to the left operand and assign +A
the result to the left operand.
-= Subtract AND assignment operator. It C -= A is equivalent to C = C -
subtracts the right operand from the left A
operand and assigns the result to the left
operand.
*= Multiply AND assignment operator. It C *= A is equivalent to C = C
multiplies the right operand with the left *A
operand and assigns the result to the left
operand.
/= Divide AND assignment operator. It divides C /= A is equivalent to C = C /
the left operand with the right operand and A
assigns the result to the left operand.
%= Modulus AND assignment operator. It takes C %= A is equivalent to C = C
modulus using two operands and assigns the %A
result to the left operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C <<
2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >>
2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= Bitwise exclusive OR and assignment C ^= 2 is same as C = C ^ 2
operator.
|= Bitwise inclusive OR and assignment C |= 2 is same as C = C | 2
operator.
6. Bitwise Operators:
Bitwise operator works on bits and performs bit-by-bit operation. The truth tables for &, |, and
^ is as follows −
p q p&q p|q p^q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
The following table lists the bitwise operators supported by C. Assume variable 'A' holds 10
and variable 'B' holds 8
Operator Description Example
& Binary AND Operator copies a bit to the result (A & B) = 8, i.e., 1000
if it exists in both operands.
| Binary OR Operator copies a bit if it exists in (A | B) = 10, i.e., 1010
either operand.
^ Binary XOR Operator copies the bit if it is set (A ^ B) = 2, i.e., 0010
in one operand but not both.
~ Binary Ones Complement Operator is unary (~A ) = -60, i.e,. 1100 0100
and has the effect of 'flipping' bits. in 2's complement form.
<< Binary Left Shift Operator. The left operands A << 1 = 20 i.e., 10100
value is moved left by the number of bits
specified by the right operand.
>> Binary Right Shift Operator. The left operands A >> 1 = 5 i.e., 101
value is moved right by the number of bits
specified by the right operand.
7. Unary Operators:
Unary arithmetic
It requires only one operand (indicating negative and positive number)
Example: +x, -y
Increment Operator (++) and Decrement Operator (– –):
• The increment operator is a unary operator that increases the value of its operand by 1.
Similarly, the decrement operator decreases the value of its operand by 1.
• The increment/decrement operators have two variants: prefix and postfix.
• Pre-increment / pre –decrement: First the value of the operand is incremented /decremented
and then used for evaluation of the expression.
• Post-increment / post –decrement: First the value of the operand is used for evaluation of
the expression and then its value is incremented /decremented. Consider int x=10,y;
is equivalent to writing
Pre-increment operator ++x
x = x + 1;
y = x;
y = x++;
is equivalent to writing
Post-increment operator x++
y = x;
x = x + 1;
y = - -x;
is equivalent to writing
Pre -decrement operator - -x
x = x - 1;
y = x;
y = x- -;
is equivalent to writing
Post -decrement operator y--
y = x;
x = x - 1;
8. Conditional Operator:
• A ternary operator pair “?:” is available in C.
• Syntax: exp1 ? exp2 : exp3 ;
• Work: exp1 is evaluated first. If it is true, then exp2 is evaluated and becomes the result of
the expression, otherwise exp3 is evaluated and becomes the result of the expression.
• For example: (a > b) ? printf(“a is big”) : printf(“b id big”);
9. Special operators
Operator Description Example
, Comma operator To seperate the statement elements such
as variables, constant or expressions.
sizeof() Size of operator Returns the length in bytes of the
specified variable
int main()
{
int Number, Reminder, Sum=0;
while(Number > 0)
{
Reminder = Number % 10;
Sum = Sum+ Reminder;
Number = Number / 10;
}
return 0;
}
output
Please Enter any number
345
Sum of the digits of Given Number= 12
9. REVERSE OF A GIVEN NUMBER
//WRITE ALGORITH FOLLOWED BY PROGRAM AND OUTPUT
#include <stdio.h>
int main()
{
int n, reverse = 0, remainder;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
printf("Reversed number = %d", reverse);
return 0;
OUTPUT
Enter an integer: 2345
Reversed number = 5432
#include<stdio.h>
#include<math.h>
void main
{
int check=0,num,l=0,temp;
temp=num;
x=num;
printf(“Enter the number:”);
scanf(“%d”,&num);
while(num!=0)
{
num=num/10;
l++;
}
while(num!=0)
{
int x=num%10;
check=check+pow(x,l);
x=x/10;
}
if(check==temp)
printf(“It is palindrome”);
else
printf(“It is not palindrome”);
}
Output
Enter the number: 153
It is palindrome
int first=0;
int last =n-1;
mid=(first+last)/2;
while(first <= last)
{
if(a[mid]==search)
{
printf("%d found at %d",search,mid);
break;
}
else if(a[mid]<search)
{
first=mid+1;
}
else
{
last=mid-1;
}
mid=(first+last)/2;
}
Output
enter the limit 5
5
Enter the numbers 1 2 3 4 5
12345
Enter the search element5
5 found at 4
13. Write a program to print the numbers that are divisible by both 3 and 7 from 1 to
100
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=100;i++)
{
if((i%3==0)&&(i%7==0))
{
printf("%d is divisible by both 3 and 7\n",i);
}
}
}
Output
21 is divisible by both 3 and 7
42 is divisible by both 3 and 7
84 s divisible by both 3 and 7
84 is divisible by both 3 and 7
OUTPUT
Enter the rows and column
22
Enter the element of 1st matrix
4567
Enter the element of 2nd matrix
1234
Difference of two matrix are:
3 3
3 3
16.MATRIX MULTIPLICATION
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
system("cls");
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
#include<stdio.h>
void main()
{
int num=0,n,x;
printf("Enter the number");
scanf("%d",&n);
while(n!=0)
{
x=n%10;
num=num*10+x;
n=n/10;
}
while(num!=0)
{
switch(num%10)
{
case 0: printf("ZERO ");
break;
case 1: printf("ONE ");
break;
case 2: printf("TWO ");
break;
case 3: printf("THREE ");
break;
case 4: printf("FOUR ");
break;
case 5: printf("FIVE ");
break;
case 6: printf("SIX ");
break;
case 7: printf("SEVEN ");
break;
case 8: printf("EIGHT ");
break;
case 9: printf("NINE ");
break;
default:
printf("The given num is not valid digit");
break;
}
num=num/10;
}
}
Output
Enter the number2345
TWO THREE FOUR FIVE