Programming Concepts and Logic

 

Chapter 5: Programming Concepts and Logic

Introduction:

Computers do not function autonomously; they require commands to operate. These commands, written in programming languages, instruct the computer to perform specific actions. Programs, which are sets of instructions, are written in these languages and are integrated to create software. Software is essentially a collection of interrelated programs performing specific functions.

Programs written in programming languages are not directly understood by computers. Therefore, language translators or processors like assemblers, compilers, and interpreters are used to convert these programs into machine-level language.

Qualities of Good Programs:

  1. Understandability: Programs should be easily understood.
  2. Correctness: Programs should be error-free.
  3. Reliability: Programs should be dependable.
  4. User Interface: Programs should have an easily understandable user interface.
  5. Portability and Flexibility: Programs should be portable and flexible.

Programming Language:

To facilitate communication between users and computers, programming languages are used. These languages consist of keywords, variables, operators, loops, and other entities, utilizing different character sets.

Types of Programming Languages:

Low-Level Language:

a. Machine Level Language:

It is machine dependent language which uses sequence of 0s and 1s to write a program. Program written in machine level language

Should not be converted so, program execution time is higher as compared to other languages.

  • Advantage:
    • Directly understood by the processor, leading to higher execution speed.
    • No need for a language translator or processor.
  • Disadvantage:
    • Difficult and time-consuming development and debugging.
    • Machine-dependent language.

b. Assembly Language:

  • Advantage:
    • Easier to write and understand compared to machine level language.
    • Faster execution than high-level languages.
  • Disadvantage:
    • Machine-dependent.
    • More complex due to the use of mnemonics.

High-Level Language:

It is closer to English language. High level language code is written in English like structure using mathematical notations. It is machine

Independent language i.e. the program written for one computer can run on another computer. Language translator named interpreter

or compiler are required to convert the program from source code to object code. FORTAN (formula translator) is the first high-level

programming language developed in 1956 AD. C, C++, Python, JavaScript etc are some examples of high-level programming language.

  1. Procedural Oriented Language (POPs):

This is the type of high-level language which primarily focuses on procedure rather than data. It is complex and time consuming to

Write large program. It follows from top to bottom. It is the primitive way of programming. it provides less security compared to

Object oriented programming languages. C, FORTAN, QBASIC etc are the examples of Procedural Oriented Language.

    • Advantage:
      • Easier development and debugging compared to low-level languages.
      • Used for general-purpose programming.
    • Disadvantage:
      • Requires a language translator.
      • Slower program execution.

 

  1. Problem/Object Oriented Language (OOPs):

This is the type of high-level language which primarily focuses on data rather than procedure. It follows from bottom to top approach.

It is the modern way of programming. It has some powerful features that provides high security Compared to

procedural oriented programming languages. C#, C++, Java etc are the examples of object-Oriented Language.

 

    • Advantage:
      • Development of web-based applications and software.
      • More advanced and user-friendly software.
    • Disadvantage:
      • Requires a language translator.
      • Slower program execution.
  1. Natural Language:

Natural Language uses simple statement of common communicating language which we could write statements

that would look like normal sentences. It is still in developing stage; Programming language like PROLOG

(Programming with logic) is currently in use.

    • Advantage:
      • Easier program development and debugging.
      • Machine-independent.
    • Disadvantage:
      • Requires a language translator.
      • Slower program execution.

 

 

 

Language Translators/Processors:

Language translator is a system software that convert program written in any other language to machine code.

Types of language translator

  1. Assembler: It is a language translator that convert program written in assembly level language to machine code.
    • Converts assembly language to machine-level language.
  2. Compiler: It is a language translator that convert program written in high level language to machine-level language

                  whole program at a time.

    • Converts high-level language to machine-level language.
    • Translates the entire program at once.
    • Examples: C, C++, Java.
  1. Interpreter: It is a language translator that convert program written in high level language to machine-level language

                  One statement at a time.

    • Converts high-level language to machine-level language.
    • Translates one statement at a time.
    • Examples: BASIC, C#, PHP.

Difference between Compiler and Interpreter:

Compiler

Interpreter

Translates the entire program at once.

Translates one line or statement at a time.

Finds errors after compiling the complete program.

Finds errors after translating a line of the program.

Faster translating process.

Slower translating process.

Difficult to code and debug.

Easy to code and debug.

Larger program, requires more memory space.

Smaller program, requires less memory space.

Not commonly used for new programming languages.

Commonly used for new programming languages.

Examples: C, C++, Java, FORTAN.

Examples: BASIC, C#, PHP.

 Common Terminology in Programming

Syntax and Semantics:

Programming language syntax refers to the structure of codes, while semantics refers to the meaning attached to

individual words or symbols.

Example in Q Basic:

qbasic

Print "This is programming";

Here, "Print" is semantics, and the whole line is syntax.

Errors (Bugs):

Errors in a program, known as bugs, can be syntax, logical, or runtime errors. Debugging is the process of removing errors.

  1. Syntax Error:
    • Arises due to not following proper rules or format.
    • Detected by language processors.
  2. Logical Error:
    • Arises due to poorly developed logic.
    • Not detected by language processors.
  3. Runtime Error:
    • Occurs while the program is being executed.
    • Not detected by language translators.

Data Types:

  • Specify the type of data used in a program.
  • Numeric, character, etc.

Variable and Constant:

  • Variable: Holds numeric or alphanumeric values, changes during program execution.
  • Constant: Holds values that do not change during program execution.

Operands and Operators:

  • Operands: Entities operated on (e.g., variables).
  • Operators: Symbols representing operations (e.g., +, =).

Operations:

  • Specific tasks performed by operators. (sum=a+b)

Program Design Tools:

  1. Algorithm:

    • Step-by-step procedure for solving a problem.
    • Written in simple English language.
    • Not tied to any programming language.

Rules for writing algorithm

              1.          Be Clear: Make each step easy to understand.

              2.          Define Inputs/Outputs: Clearly say what goes in and what comes out.

              3.          Follow Logic: Organize steps in a logical order.

              4.          Save Resources: Be efficient in using time and space.

Example1:  Calculate sum of 5 and 4

Step1: Start

Step2: Read A as 5 and B as 4

Step3: Add A and B and store the result in sum.

Step4: Display the value of Sum.

Step5: Stop

Example2: Algorithm to Calculate Simple Interest:

Step 1: Start

Step 2: Read the principal amount (P), time duration (T), and rate of interest (R)

Step 3: Calculate simple interest using the formula:

          Simple Interest (I) = (P * T * R) / 100

Step 4: Display the calculated simple interest (I)

Step 5: Stop

Example 3: Algorithm to Check Greatest Number Among Two Numbers:

Step 1: Start

Step 2: Read two numbers, a and b

Step 3: If (a > b), then

           Display "The greatest number is a"

       Else

           Display "The greatest number is b"

       End If

Step 4: Stop

Example2: Algorithm to Calculate Area of Circle:

Step 1: Start

Step 2: Read the radius as (R)

Step 3: Calculate area of circle using this formula:

          Area (A) = 3.14*R^2

Step 4: Display the calculated Area (A)

Step 5: Stop

Flowchart: 

It is a diagrammatic or pictorial representation of steps involve in algorithm for solving a particular problem is known as flowchart.

It uses several geometrical shapes, symbol for representing different action involve in problem solving. There are two types of flowcharts:

a. Program Flowchart: It is a type of flowchart which focuses on a specific problem. This type of flowchart portage only a single task (logic) at a time. 






 



                                               C Programming Language:

  • Developed by Dennis Ritchie at AT&T's Bell Laboratories in 1970.
  • High-level and middle-level programming language, supporting both system and application programming.
  • Known for its efficiency, powerful tools, and keyword set.
  • Features its own text editor and compiler.
  • Supports structured programming.
  • Portable and flexible.

Advantages of C:

  • Suitable for system programming.
  • Easy interaction with hardware.
  • Efficient program development.
  • Compact and secure code.

Disadvantages of C:

  • Lack of runtime checking.
  • No strict data type checking.
  • Debugging complexity increases with program size.
  • Semicolon required for each statement.

Definitions:

  • Editor: The area where the program is typed; C language has its own text editor.
  • Pre-processor: Collection of codes using # directives, e.g., #include for header files.
  • Compiler: Transforms high-level source code into machine-readable object code.
  • Linker: Links various library files with object codes, creating an executable (.exe) file.

Fundamentals of C:

Character Set:

  • Set of alphabets, digits, white characters, and symbols used in a programming language.
  • C character set includes alphabets (both uppercase and lowercase), digits, and special symbols.

Comment:

  • Text or characters not compiled or executed, used for providing information to programmers.
  • Single-line comment: // Comment text
  • Multi-line comment: /* Comment text */

C Tokens:

  • Set of identifiers, keywords, constants, variables, and operators.
  • Remain as a single unit during compilation.

Definitions:

  • Identifiers: Names for entities like variables, functions, etc.
  • Keyword: Reserved words in C; cannot be used as identifiers.
  • Data Types: int, float, char, void represent types of data in C.
  • Constants: Fixed values in a program.
  • Variables: Named memory locations for storing data.
  • Statements: Set of keywords, identifiers, operators, variables, and constants, ending with a semicolon or curly brackets.

Escape Sequences:

  • Special sequences with \ or ' as escape characters.
  • Examples include \n for a new line and \t for horizontal tab.

Operators:

  • Symbols used for mathematical and logical calculations.
  • Binary, unary, and ternary operators.
  • Examples: Arithmetic, Relational, Logical, Assignment, Increment/Decrement, Conditional, Bitwise.

Library Functions:

  • Ready-made functions stored in the C language library.
  • Examples: abs(), printf(), scanf(), clrscr(), getch(), strlen(), sqrt().

Input/Output (I/O) Functions:

  • printf(): Formatted output function.
  • scanf(): Input function for reading values into the program.
Example programs
a) Write a c program to enter any two numbers and print the sum of them.
# include <stdio.h>
int main(){
    int a, b, sum;
    printf("Enter the first number: ");
    scanf("%d", &a);
    printf("Enter the second number: ");
    scanf("%d", &b);
    sum = a + b;
    printf("The sum is: %d\n", sum);
    return 0;
}

b) Write a C program to calculate the area of a rectangle. 
[Given values: Length = 20, Breadth = 15].

#include <stdio.h>
int main(){
    int l = 20, b = 15;
    int a;
    a = l * b;
    printf("The area is %d\n", a);
    return 0;
}

c) Write a C program to calculate the area of a rectangle. 

[Formula: Area = Length * Breadth]

#include <stdio.h>
int main(){
    int l, b, a;
    printf("Enter Length: ");
    scanf("%d", &l);
    printf("Enter Breadth: ");
    scanf("%d", &b);
    a = l * b;
    printf("The area is %d\n", a);
    return 0;
}

d) Write a C program to calculate the perimeter of a rectangle.

[ Formula: Perimeter = 2 * (Length + Breadth)]

#include <stdio.h>
int main(){
    int l, b, p;
    printf("Enter Length: ");
    scanf("%d", &l);
    printf("Enter Breadth: ");
    scanf("%d", &b);
    p = 2 * (l + b);
    printf("The perimeter is: %d\n", p);
    return 0;
}

e) Write a C program to calculate the area of a triangle.

  [ Formula: Area = 1/2 * base * height]

#include <stdio.h>
int main(){
    int b, h, a;
    printf("Enter base and height separated by space: ");
    scanf("%d %d", &b, &h);
    a = 0.5 * b * h;
    printf("The area is %d\n", a);
    return 0;
}

 f) Write a C program to calculate the area and perimeter of a square.

   Formula: Area = side * side, Perimeter = 4 * side

#include <stdio.h>
int main(){
    int l, a, p;
    printf("Enter length of a side: ");
    scanf("%d", &l);
    a = l * l;
    p = 4 * l;
    printf("The area is %d and the Perimeter is %d\n", a, p);
    return 0;
}

   g) Write a C program to calculate the volume of a box.

   [Formula: Volume = length * breadth * height]

#include <stdio.h>
int main(){
    int l, b, h, v;
    printf("Enter Length: ");
    scanf("%d", &l);
    printf("Enter Breadth: ");
    scanf("%d", &b);
    printf("Enter Height: ");
    scanf("%d", &h);
    v = l * b * h;
    printf("The volume is %d\n", v);
    return 0;
}

   h) Write a C program to enter the radius of a circle and print its circumference.

   [Formula: Circumference = 2 * pi * radius]

#include <stdio.h>
int main(){
    float r, c, pi = 3.14;
    printf("Enter the radius: ");
    scanf("%f", &r);
    c = 2 * pi * r;
    printf("The circumference is %f\n", c);
    return 0;
}
                           

Control structures

It in programming are constructs that enable developers to manage the flow of execution in a program. They determine the order in which statements are executed based on certain conditions or loops. There are three main types of control structures:

Sequence Structure:

This is the default flow of execution where statements are executed sequentially from top to bottom, following the natural order of the program.

Example program

  i) Write a C program to enter the length, breadth, and height of a box and calculate the total surface area (TSA).

    [Formula: TSA = 2 * (L * B + B * H + L * H)]

#include <stdio.h>
int main(){
    int l, b, h, tsa;
    printf("Enter length, breadth, and height separated by space: ");
    scanf("%d %d %d", &l, &b, &h);
    tsa = 2 * (l * b + b * h + l * h);
    printf("Total Surface Area is %d\n", tsa);
    return 0;
}

j) Write a C program to enter the radius of a circle and calculate the area.
    [ Formula: Area = pi * r * r]
#include <stdio.h>
int main(){
    float r, a;
    printf("Enter radius: ");
    scanf("%f", &r);
    a = 3.14159 * r * r;
    printf("The area is %f\n", a);
    return 0;
}
k) Write a C program to enter the radius of a circle and print its circumference.
 [Formula: Circumference = 2 * pi * radius]
#include <stdio.h>
int main(){
    float r, c;
    printf("Enter the radius: ");
    scanf("%f", &r);
    c = 2 * 3.14159 * r;
    printf("The circumference is %f\n", c);
    return 0;
}

Selection Structure (Conditional Statements): 

These structures allow the program to make decisions based on specific conditions. Common conditional statements include if, else if, and else. They enable the execution of different blocks of code based on whether certain conditions are true or false.

a) Conditional Statements: They break the program flow if the condition is matching. Eg. if, if….else and if…..else….if 

if statement:

The if statement is used to execute a block of code if a specified condition is true.

Syntax

if (condition) {
    // Code to be executed if the condition is true
}
Example
#include <stdio.h>
int main() {
    int num = 10;
    if (num > 0) {
        printf("The number is positive.\n");
    }
    return 0;
}

Looping Structure (Repetition Statements): 

Looping structures facilitate the repetition of a set of statements multiple times. This repetition is based on certain conditions, and common loop constructs include for, while, and do-while. Loops provide a way to efficiently repeat a block of code until a specified condition is met.         

l) Write a program to print "Ekta Academy" 100 times using for loop

#include <stdio.h>
int main(){
int i;
for(i=1;i<=100;i++){
printf("%d times Ekta Academy\n",i);
}
return 0;
}
                 

m) Write a program to print natural number upto 100 using for loop

#include <stdio.h>
int main(){
int i;
for(i=1;i<=100;i++){
printf("%d \n",i);
}
return 0;
}              

n) Write a program to print whole number upto 100 using for loop

#include <stdio.h>
int main(){
int i;
for(i=0;i<=100;i++){
printf("%d \n",i);
}
return 0;
}
                      

o) Write a program to print odd number upto 100 using for loop

#include <stdio.h>
int main(){
int i;
for(i=1;i<=100;i+=2){
printf("%d \n",i);
}
return 0;
}
              

p) Write a program to print even number upto 100 using for loop

#include <stdio.h>
int main(){
int i;
for(i=2;i<=100;i+=2){
printf("%d \n",i);
}
return 0;
}
       

q) Write a program to print following series

//  5,10,15,20.........50

#include <stdio.h>
int main(){
int i;
for(i=5;i<=50;i+=5){
printf("%d \n",i);
}
return 0;
}

r) Write a program to print following series

//  100,90,80........0

#include <stdio.h>
int main(){
int i;
for(i=100;i>=0;i-=10){
printf("%d \n",i);
}
return 0;
}

s) Write a program to print following series

//  1,4,9,16,25........10th term

#include <stdio.h>
int main(){
int a=1,i;
for(i=1;i<=10;i++){
a=i*i;
printf("%d \n",a);
}
return 0;
}

t) Write a program to print following series

// 1,3,5 ..... upto 15th term

#include <stdio.h>
int main(){
int a=1,i;
for(i=1;i<=15;i++){
printf("%d \n",a);
a=a+2;
}
return 0;
}

u) Write a program to print following series

// 2,4,7 ..... upto 8th term

#include <stdio.h>
int main(){
int a=2,i;
for(i=1;i<=8;i++){
printf("%d \n",a);
a=a+(i+1);
}
return 0;
}

v) Write a program to print following series

// 1,1,2,3,5 ..... upto 9th term (fibonacci series)

#include <stdio.h>
int main(){
int a=1,b=1,i,c;
printf("%d, %d",a,b);
for(i=1;i<=9;i++){
        c=a+b;
printf("%d,",c);
a=b;
b=c;
}
return 0;
}

w) WAP to calculate sum of numbers upto 10

#include <stdio.h>
int main(){
int sum=0,i;
for(i=1;i<=10;i++){
sum=sum+i;
}
printf("Sum of numbers=%d",sum);
return 0;
}

x) WAP to calculate sum of n-natural number.
#include <stdio.h>
int main(){
int sum=0,i,n;
printf("Enter a number\t");
scanf("%d",&n);
for(i=1;i<=n;i++){
sum=sum+i;
}
printf("Sum of numbers=%d",sum);
return 0;
}

y) WAP to calculate product of n-natural number.(factorial number)
#include <stdio.h>
int main(){
int prod=1,i,n;
printf("Enter a number\t");
scanf("%d",&n);
for(i=1;i<=n;i++){
prod=prod*i;
}
printf("prod of numbers=%d",prod);
return 0;
}


z) WAP to print following series
//3,12,27,48.......10th term
#include <stdio.h>
int main(){
int a,i;
for(i=1;i<=10;i++){
a=3*i*i;
printf("%d,\t",a);
}
return 0;
}

WHILE Loop (ENTRY CONTROL LOOP)

The While statement executes a statement  or block of statements till the given condition is true.
syntax
while(condition){
//Block of statement
}

aa) sum of n-natural number using while loop
#include <stdio.h>
int main(){
int sum=0,i=0,n;
printf("Enter a number\t");
scanf("%d",&n);
while(i<=n){
sum=sum+i;
i=i+1;
}
printf("sum of numbers=%d",sum);
return 0;
}

ab) factorial number of a number using while loop
#include <stdio.h>
int main(){
int fact=1,i=1,n;
printf("Enter a number\t");
scanf("%d",&n);
while(i<=n){
fact=fact*i;
i=i+1;
}
printf("factroial of number=%d",fact);
return 0;
}

//WAP to ask multi digit number and reverse it
#include <stdio.h>
int main(){
int n,r,rev;
printf("Enter a multi digit number\t");
scanf("%d",&n);
while(n!=0){
r= n%10;   
rev=rev*10+r;   
n=n/10;        
}
printf("Reverse  of number=%d",rev);
return 0;
}

//WAP to ask multi digit number and  
//check the number is palindrome or not [121 is palindrome number]
#include <stdio.h>
int main(){
int n,r,rev,z;
printf("Enter a multi digit number\t");
scanf("%d",&n);
z=n;
while(n!=0){
r= n%10;   
rev=rev*10+r;   
n=n/10;        
}
printf("Reverse  of number=%d\n",rev);
if(z==rev){
printf("It is palindrome number");
}else{
printf("It is not palindrome number");
}
return 0;
}

//WAP to ask a nummber and check prime or composite.
#include <stdio.h>
int main(){
int n,i,count;
printf("Enter a number\t");
scanf("%d",&n);
for(i=1;i<n;i++){
if(n%i==0){
count=count+1;
}
}
if(count==2){
printf("It is prime number");
}else{
printf("It is composite number");
}
return 0;
}

//WAP to ask multi digit number and reverse it using do while loop.

#include <stdio.h>
int main(){
int n,r,rev;
printf("Enter a multi digit number\t");
scanf("%d",&n);
do{
r= n%10;   
rev=rev*10+r;   
n=n/10;        
}while(n!=0);
printf("Reverse  of number=%d",rev);
return 0;
}


//WAP to ask multi digit number and  
//check the number is palindrome or not using do while loop[121 is palindrome number] 
#include <stdio.h>
int main(){
int n,r,rev,z;
printf("Enter a multi digit number\t");
scanf("%d",&n);
z=n;
do{
r= n%10;   
rev=rev*10+r;   
n=n/10;        
}while(n!=0);
printf("Reverse  of number=%d\n",rev);
if(z==rev){
printf("It is palindrome number");
}else{
printf("It is not palindrome number");
}
return 0;
}

Array:

An array is a collection of variables of same type that are represented by a common name i.e. array is the collection of homogeneous data type(Contains similar data type) . If we declare array as int then it must only contain all integer data and same for floats and char. Each array is specified by array name followed by index or subscript encloses in square bracket(eg a[10]) . Hence, it is also called indexed or subscript variables. In an array index of array represent the size of array.

Let us consider, int Age[5]
This means array name is age and it can store 5 different data under integer data type.
Age[0] Age[1] Age[2] Age[3] Age[4]

Type of array
1- Dimensional
2- Multi dimensional
a) 1- Dimensional:
This type of array has only one subscript.

Declaration
Data_type array_name[Size]
Example 
int salary[7]

# WAP to ask 10 different number and print them using array

#include <stdio.h>
int main(){
int n[10],i;
printf("Enter 10 different numbers\n");
for(i=0;i<10;i++){
scanf("%d",&n[i]);
}
for(i=0;i<10;i++){
printf("%d, ",n[i]);
    }
    return 0;
}

# WAP to ask 10 different number and print sum of them using array

#include <stdio.h>
int main(){
int n[10],i,sum=0;
printf("Enter 10 different numbers\n");
for(i=0;i<10;i++){
scanf("%d",&n[i]);
sum=sum+n[i];
}

printf("Sum of numbers=%d, ",sum);
    
    return 0;
}

# WAP to ask 10 different number and print product of them using array

#include <stdio.h>
int main(){
int n[10],i,prod=1;
printf("Enter 10 different numbers\n");
for(i=0;i<10;i++){
scanf("%d",&n[i]);
prod=prod*n[i];
}
printf("Product of numbers=%d, ",prod);
    return 0;
}

// WAP to ask marks of 6 different subject and print total and percentage
 them using array

#include <stdio.h>
int main(){
int marks[6],i;
float percent,total;
printf("Enter marks of 6 different subjects \n");
for(i=0;i<6;i++){
scanf("%d",&marks[i]);
total=total+marks[i];
}
percent=(total/600)*100;
printf("Total marks obtained =%f, ",total);
    printf("percentage  obtained =%f, ",percent);
    return 0;
}

b) Multi Dimensional:
This type of array has more than one subscript.

Declaration
Data_type array_name[Size][Size]
Example 
int n[3][3]

// Wap to input 2x2 matrix and display the data of it.
#include <stdio.h>
int main(){
int a[2][2],i,j;
printf("Enter 4 element of matrix 2X2\n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
scanf("%d",&a[i][j]);
}
}
printf("our matrix look like this = \n");
for(i=0;i<2;i++){
for(j=0;j<2;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}

// Wap to input 3x3 matrix and display the data of it.
#include <stdio.h>
int main(){
int a[3][3],i,j;
printf("Enter 9 element of matrix 3X3\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
printf("our matrix look like this = \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",a[i][j]);
}
printf("\n");
}
return 0;
}

// Wap to input 3x3 matrix addition and display ANSWER.
#include <stdio.h>
int main(){
int a[3][3],b[3][3],sum[3][3],i,j;
printf("Enter  element of Matrix A \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter  element of Matrix B \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
sum[i][j]=a[i][j]+b[i][j];
}
}
printf("sum of matrix look like this = \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}

// Wap to input 3x3 matrix product and display ANSWER.
#include <stdio.h>
int main(){
int a[3][3],b[3][3],prod[3][3],i,j,k;
printf("Enter  element of Matrix A \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
printf("Enter  element of Matrix B \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&b[i][j]);
}
}
for(i=0;i<3;i++){
for(j=0;j<3;j++){
prod[i][j]=0;
for(k=0;k<3;k++){
prod[i][j]= prod[i][j]+a[i][k]*b[k][j];
}
}
}
printf("Product of matrix look like this = \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%d\t",prod[i][j]);
}
printf("\n");
}
return 0;
}

Comments