You are on page 1of 27

PART A

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.

In C, arrays are declared using the following syntax:


data_type array_name[array_size];
Example,
int marks[10];.

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

10. RULES FOR NAMING VARIABLES


A variable name can be any combination of alphabets digits and underscore.
Variable should not start with a digit
The first character must be an alphabet or an underscore(_).
No commas or blankspace are allowed within a variable name
No special symbol can be used in a variable

Valid variable name


Rollno, _Rollno, Rollno123
name, _name, name0

Invalid variable name


-Rollno, Roll No, 123RollNo
+name, name-, 7name

11. ALGORITHM TO FIND BIGGEST OF TWO NUMBERS


step1. Start
step2. Accept two numbers and store the variables in A and B.
step3. If (A>B) then Display A is greater than B.
step5. Else Display B is greater than A
step6. Stop

12. ALGORITHM TO CHECK WHETHER THE GIVEN NUMBER IS ODD OR EVEN


step1. Start
step2. Accept a numbers and store the variables in A.
step3. If (A%2==0) then Display A is ODD.
step5. Else Display A is EVEN
step6. Stop

13. DECLARE FLOAT ARRAY AND ASSIGN 5 VALUES TO IT


#include<stdio.h>
{
float a[10];
for(i<0;i<5;i++)
{
scanf(“%d”,&a[i]);
}
for(i=0;i<5;i++)
{
printf(“%.1f\n”,a[i]);
}

output:
2.3 4.5 6.7 7.8 8.9
2.3
4.5
6.7
7.8
8.9

14. DECLARE CHARACTER ARRAY AND ASSIGN 5 VALUES TO IT

#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

15. LIST OF STORAGE CLASS


1. auto
2. register
3. extern
4. static

16. ADVANTAGES OF ARRAY


We can put in place other data structures like stacks, queues, linked lists, trees, graphs etc., in
array
Array can sort multiple elements at a time.
We can access an element of array by using in index.
DISADVANTAGES OF ARRAY
We have to declare size of an array in advance. However, we may not know what size we
need at the time of array decleration.
The array is static structure. It means array size is always fixed, so we cannot increase or
decrease memory location.

17. HOW TO DECLARE A STRING IN C


char str_name[size];
Eg., char str[100];
There are two ways to declare string in c language.
1. By char array
ar str[] = {'H', 'E', 'L', 'L', 'O', '\0'};

2. By string literal
char str[ ] = "HELLO";
char str [10] = "HELLO";

18. HOW TO DECLARE 2D FLOAT ARRAY


syntax
data_type array_name[rows][columns];
Eg., float arr[2][2];
19. HOW TO DECLARE 2D CHARACTER ARRAY
data_type array_name[rows][columns];
char ch_arr[3][10] = {"spike", "tom","jerry"};

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.

6. TYPES OF PARAMETER PASSING METHOD


1. Call by value
2. Call by reference

7. COMPARE CALL BY VALUE AND CALL BY REFERENCE

Call by value Call by reference


A copy of the value is passed into An address of value is passed into
the function the function
Changes made inside the function is Changes made inside the function
limited to the function only. The validate outside of the function also.
values of the actual parameters do The values of the actual parameters
not change by changing the formal do change by changing the formal
parameters. parameters.
Actual and formal arguments are Actual and formal arguments are
created at the different memory created at the same memory
location location

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

12. ADVANTAGES OF STRUTURE


Heterogeous collection of data items
Reduced complexity
Increased productivity
Maintainability of code
Enhanced code readability
Suitable for mathematical operations

13. COMPARE STRUCTURE AND UNION

14. SYNTAX OF FUNCTION PROTOTYPE OR FUNCTION DECLERATION


return_type function name (datatype variable1, datatype variable2,..);
Positioning function declaration
1. If function definition is written after main, then we write prototype declaration in global
declaration section.
2. If function definition is written above the main function then ,it is not necessary to write
prototype declaration

PART B
UNIT 1
1. BUILDING BLOCKS

Unit 1 digital notes – slide no: 22 – 25

2. STRUCTURE OF C PROGRAM

A C program contains one or more functions, where a function is defined as a group of


statements that perform a well-defined task. The statements in a function are written in a
logical sequence to perform a specific task. The main() function is the most important
function and is a part of every C program.
Documentation Section
Header File Section
Definition Section
Global (Variable) Declaration Section
main()
{
Declaration(local variable) part
Executable part
}
sub program section
function 1()
{
Declaration(local variable) part
Executable part
}
.
.
.
function n
{
Declaration(local variable) part
Executable part
}

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.

Header File section:

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.

Eg: #include<stdio.h> , #include<conio.h>

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.

o Declaration part:Declaration part is used to declare variables. Every variable must be


declared before using it in C program. Every variable are specified with their data type
followed by a name.
o Execution part:Execution part contains program statements. There may or may not be
a declaration part but there must be an execution part. It includes input and output
statements.

Sub program section:


Sub program section consists of all user-defined functions. It is called from the main function.
It is placed before or after the main function. But generally placed after the main function.

Example program:

/* Enter the radius of a circle


C program to find the area of a circle, 30
given the radius Area of a circle = 2827.80
*/
#include <stdio.h>
#define PI 3.142
void main()
{
float radius, area;

printf("Enter the radius of a circle \n");


scanf("%f", &radius);
area = PI * radius * radius;
printf("Area of a circle = %5.2f\n", area);
}

3. OPERATOR AND OPERATOR PRECEDENCE


Operands:
An operand specifies an entity on which an operation is to be performed. An operand can be a
variable name, a constant, a function call or a macro.
Operators:
An operation specifies the operation to be applied to its operands.
int a=3,r;
r= a+2*3;
operands : a, 2, 3
operators : +, *
Criteria for the Classification of operators:
1. The number of operand on which an operator operates.

Operator Number of operands Operator names


Unary operator One Unary +, unary - , ++ , - -
Binary operator Two Arithmetic operators

( + , * ,/ )
Ternary operator Three Conditional operator

Exp1? Exp2 : exp 3


2. The role of an operator
The major group of operators are listed below:
• Arithmetic Operators
• Relational Operators
• Equality Operators
• Logical Operators
• Bitwise Operators
• Assignment Operators
• Unary Operators
• Conditional Operators
• Special Operators
1. Arithmetic Operators:

Consider three variables declared as, int a=9, b=3, result;

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 –

Operator Description Example


&& If both the operands are non-zero, then the condition (A && B) is false.
becomes true.
|| If any of the two operands is non-zero, then the condition (A || B) is true.
becomes true.
! It is used to reverse the logical state of its operand. If a !(A && B) is true.
condition is true, then Logical NOT operator will make it
false.
Truth Tables:

Truth table for logical


NOT
Truth table for logical AND Truth table for logical OR

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;

Operators Representation Explanation


y = ++x;

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

& and * Pointer operator & - it specifies the address of the


variable

* - it specifies the value of the variable


. and -> Member selection operators To access the elements from a structure
OPERATORS PRECEDENCE AND ASSOCIATIVITY IN C:
The order in which the operators will operate depends upon the precedence and associativity
of operators.
Operator precedence determines the grouping of terms in an expression and decides how an
expression is evaluated. Certain operators have higher precedence than others; for example, the
multiplication operator has a higher precedence than the addition operator.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher
precedence than +, so it first gets multiplied with 3*2 and then adds into 7.
Associativity of Operators:
When an expression contains two operators of equal priority the tie between them is settled
using the associativity of the operators. Associativity can be of two types—Left to Right or
Right to Left.
Consider the expression
a=3/2*5;
Here there is a tie between operators of same priority, that is between / and *. This tie is settled
using the associativity of / and * of L -> R.
a=3/2*5
= 1*5
=5
Here, operators with the highest precedence appear at the top of the table, those with the lowest
appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Precedence Category Operator Associativity


level
1 Postfix () [] -> . ++ - - Left to right
2 Unary + - ! ~ ++ - - (type)* & sizeof Right to left
3 Multiplicative */% Left to right
4 Additive +- Left to right
5 Shift << >> Left to right
6 Relational < <= > >= Left to right
7 Equality == != Left to right
8 Bitwise AND & Left to right
9 Bitwise XOR ^ Left to right
10 Bitwise OR | Left to right
11 Logical AND && Left to right
12 Logical OR || Left to right
13 Conditional ?: Right to left
14 Assignment = += -= *= /= %=>>= <<= &= ^= Right to left
|=
15 Comma , Left to right

4. STRING BUILT-IN FUNCTION


i)strrev
ii)strcpy
iii)strcmp-strncmp,stricmp,strncmpi
iv)strcat
v)strupr
vi)strlwr
REFER CW WRITE DEFINITION SYNTAX PROGRAM AND OUTPUT
5. CONDITIONAL STATEMENT
i)if statement
ii) if-else statement
iii)else-if ladder
iv)nested if statement
v)switch
WRITE DEFINITION, SYNTAX,FLOWCHART, PROGRAM,OUTPUT
6. ITERATION STATEMENT
i)while loop
ii)do-while loop
iii)for loop
WRITE DEFINITION, SYNTAX,FLOWCHART, PROGRAM,OUTPUT
7. STORAGE CLASS
REFER DIGITAL NOTES-SLIDE NO 40-43
8. SUM OF DIGITS

//WRITE ALGORITH FOLLOWED BY PROGRAM AND OUTPUT


#include <stdio.h>

int main()
{
int Number, Reminder, Sum=0;

printf("\n Please Enter any number\n");


scanf("%d", &Number);

while(Number > 0)
{
Reminder = Number % 10;
Sum = Sum+ Reminder;
Number = Number / 10;
}

printf("\n Sum of the digits of Given Number = %d", Sum);

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

10. TO CHECK A GIVEN NUMBER IS ARMSTRONG OR NOT


//WRITE ALGORITH FOLLOWED BY PROGRAM AND OUTPUT

#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

11. C program to find whether the number is positive, negative or zero


#include<stdio.h>
void main()
{
int num;
printf("Enter the number");
scanf("%d",&num);
if(num==0)
{
printf("The number is zero");
}
else if(num<0)
{
printf("The number is negative");
}
else
{
printf("The number is positive");
}
}
Output
Enter the number -9
The number is negative

12. A. LINEAR SEARCH


#include<stdio.h>
void main()
{
int num[10],i,n,search,flag=0;
printf("Enter the limit");
scanf("%d",&n);
printf("Enter the number");
for(i=0;i<n;i++)
{
scanf("%d",&num[i]);
}
printf("Enter the search element");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(num[i]==search)
{
flag=1;
}
}
if(flag==1)
{
printf("%d is found ",search);
}
else
{
printf("%d is not found",search);
}
}
Output
Enter the limit5
Enter the number1 2 3 4 5
Enter the search element6
6 is not found

12.B. BINARY SEARCH


#include<stdio.h>
#include<math.h>
int main()
{
int a[20],n,mid,search,i;
printf("enter the limit");
scanf("%d",&n);
printf("Enter the numbers");
for(i=0;i<n;i++) // 2 4 5 3 6
{
scanf("%d",&a[i]);
}
printf("Enter the search element");
scanf("%d",&search);

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

14. MATRIX ADDITION


#include<stdio.h>
void main()
{
int a[10][10],b[10][10],e[10][10],i,j,r,c;
printf("Enter the rows and column\n");
scanf("%d%d",&r,&c);
printf("Enter the element of 1st matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the element of 2nd matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Sum of two matrix are:\n");
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
e[i][j] = a[i][j] + b[i][j];
printf("%d \t",e[i][j]);
}
printf("\n");
}
}
}
output
Enter the rows and column
22
Enter the element of 1st matrix
1234
Enter the element of 2nd matrix
5678
Sum of two matrix are:
6 8
10 12
15. MATRIX SUBTRACTION
#include<stdio.h>
void main()
{
int a[10][10],b[10][10],e[10][10],i,j,r,c;
printf("Enter the rows and column\n");
scanf("%d%d",&r,&c);
printf("Enter the element of 1st matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the element of 2nd matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("Dfference of two matrix are:\n");
{
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
e[i][j] = a[i][j] - b[i][j];
printf("%d \t",e[i][j]);
}
printf("\n");
}
}
}

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]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT
enter the number of row=2
enter the number of column=2
enter the first matrix element=
1234
enter the second matrix element=
1234
multiply of the matrix=
7 10
15 22
17. MATRIX TRANSPOSE
#include <stdio.h>
int main() {
int a[10][10], transpose[10][10], r, c;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
printf("\nEnter matrix elements:\n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("Enter element a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("\nEntered matrix: \n");
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
printf("%d ", a[i][j]);
if (j == c - 1)
printf("\n");
}
for (int i = 0; i < r; ++i)
for (int j = 0; j < c; ++j) {
transpose[j][i] = a[i][j];
}
printf("\nTranspose of the matrix:\n");
for (int i = 0; i < c; ++i)
for (int j = 0; j < r; ++j) {
printf("%d ", transpose[i][j]);
if (j == r - 1)
printf("\n");
}
return 0;
}
OUTPUT
Enter rows and columns: 2 2
Enter matrix elements:
Enter element a11: 1 2 3 4
Enter element a12: Enter element a21: Enter element a22:
Entered matrix:
1 2
3 4

Transpose of the matrix:


1 3
2 4

20. C PROGRAM TO FIND SUM OF DIAGNOLS


#include<stdio.h>
void main()
{
int mat[12][12];
int i,j,row,col,sum=0;
printf("Enter the number of rows and columns for 1st matrix\n");
scanf("%d%d",&row,&col);
printf("Enter the elements of the matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
scanf("%d",&mat[i][j]);
}
}
printf("The matrix\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mat[i][j]);
}
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(i==j)
{
sum=sum+mat[i][j];
}
}
}
printf("The sum of diagonal elements of a square matrix = %d\n",sum);
}
OUTPUT
Enter the number of rows and columns for 1st matrix
22
Enter the elements of the matrix
1234
The matrix
1 2
3 4
The sum of diagonal elements of a square matrix = 5

21. FIND SUM AND AVERAGE OF N NUMBERS


#include <stdio.h>
int main()
{
int num, sum = 0, n;
float avg;
printf("Please Enter term of n number:-");
scanf("%d", &n);
for(int i = 1; i <= n; i++)
{
printf("Number %d = ", i);
scanf("%d", &num);
sum = sum + num;
}
avg = sum / n;
printf("\nThe Sum of n Numbers = %d", sum);
printf("\nThe Average of n Numbers = %.2f\n", avg);
}
OUTPUT
Please Enter term of n number:-4
Number 1 = 1
Number 2 = 2
Number 3 = 3
Number 4 = 4

The Sum of n Numbers = 10


The Average of n Numbers = 2.00
22. FIND THE SMALLEST AND LARGEST VALUE FROM SET OF NUMBERS
#include<stdio.h>
int main()
{
int a[50],i,num,large,small;
printf("Enter the number of elements :");
scanf("%d",&num);
printf("Input the array elements :");
for(i=0;i<num;++i)
scanf("%d",&a[i]);
large=small=a[0];
for(i=1;i<num;++i)
{
if(a[i]>large)
large=a[i];
if(a[i]<small)
small=a[i];
}
printf("small= %d\n",small);
printf("large= %d",large);
return 0;
}
OUTPUT
Enter the number of elements :5
Input the array elements :2 3 4 6 7 8 
small= 2
large= 7

23. C PROGRAM TO CONVERT NUMBER TO WORD

#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

You might also like