C lab manual
C lab manual
CS3271
PROGRAMMING IN C LABORATORY
LAB MANUAL
ACADEMIC YEAR
2023-24 (EVEN)
LIST OF EXPERIMENTS
COURSE OUTCOMES: upon completion of the course, the students will be able to
1. Write a C program for getting the five subject marks from the user, and then calculate total
marks obtained and average of input passed and print the results.
2. Write a C program to calculate area of a circle and cylinder as well as circumference of a
circle and volume of a cone.
3. Write a C program to display the ASCII value of an entered character.
4. Write a C program to check whether given number is even or odd using ternary operator.
5. Write a C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f - g)). Read
the values a, b, c, d, e, f, g from the standard input device
6. Write a C program to print the sizeof () for all data types.
7. Write a C program to swap two numbers using temporary variable and without using
temporary variable
1. Write a C program to insert 5 elements into an array and print the elements of the array.
2. Write a C program to find whether a search element is present in the array or not.
3. Write a C program to sort the elements in the array using selection sort.
4. Write a C Program to perform Matrix Addition
5. Write a C Program to multiply two Matrices
6. Write a C program for transpose of a matrix
N.RAMYA, AP/CSE CS3271-PROGRAMMING IN C
4
Write a C program for getting the five subject marks from the user, and then calculate the total
marks obtained, average of input passed and print the results.
Aim:
To write a program for getting 5 subjects mark from user and print its total,
average and result.
Algorithm:
Step 1: Start the program.
Step 2: Declare the variables mark, result, total=0, i.
Step 3: Perform the below expression; total=total + mark and avg = (total)/5.
Step 4: Print pass, if avg greater than 50, otherwise print fail.
Step 5: Stop the program.
Program:
#include <stdio.h>
int main()
{
float eng, phy, chem, math, comp;
float total, average, percentage;
printf("Enter marks of five subjects: \n");
scanf("%f%f%f%f%f", &eng, &phy, &chem, &math, &comp);
total = eng + phy + chem + math + comp;
average = total / 5.0;
percentage = (total / 500.0) * 100;
printf("Total marks = %.2f\n", total);
printf("Average marks = %.2f\n", average);
printf("Percentage = %.2f", percentage);
return 0;
}
Output:
Enter marks of five subjects:
34 43 54 67 78
Total marks = 276.00
Average marks = 55.20
Percentage = 55.20
Result:
Thus the C program to calculate the total marks obtained, average of input passed and
print the results has been successfully executed and verified.
Write a C program to calculate area of a circle and cylinder as well as circumference of a circle
and volume of a cone.
Aim:
To write a C program to calculate the area of circle and cylinder, circumference of
a circle and volume of a cone.
Algorithm:
Step 1: Start the program.
Step2: Declare the variables c_radi, cyl_radi, cyl_h, cone_radi, cone_h, area_c,
circum_c, area_cy and vol_cone.
Step 3: Perform the below expressions:
area_c=(pi*c_radi*c_radi)
circum_c=(2*pi*c_radi)
area_cy=((2*pi*cyl_radi*cyl_h)+(2*pi*cyl_radi*cyl_radi))
vol_cone=((pi*cone_radi*cone_radi*cone_h)/(3)).
Step 4: Display the output to the user.
Step 5: Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
#define pi 3.14
void main()
{
int c_radi, cyl_radi, cyl_h, cone_radi, cone_h;
float area_c, circum_c, area_cy, vol_cone;
clrscr();
printf("\n Enter radius of circle");
scanf("%d",&c_radi);
printf("\n Enter radius of cylinder");
scanf("%d",&cyl_radi);
printf("\n Enter height of cylinder:");
scanf("%d",&cyl_h);
printf("\n Enter radius of cone:");
scanf("%d",&cone_h);
printf("\n Enter height of cone:");
scanf("%d",&cone_h);
area_c=(pi*c_radi*c_radi);
circum_c=(2*pi*c_radi);
area_cy=((2*pi*cyl_radi*cyl_h)+(2*pi*cyl_radi*cyl_radi));
vol_cone=((pi*cone_radi*cone_radi*cone_h)/(3));
printf("\n t\The area of a circle is:%f",area_c);
printf("\n The circumference of a circle is:%f",circum_c);
printf("\n The area of a cylinder is:%f",area_cy);
printf("\n The volume of a cone is:%f",vol_cone);
getch();
}
Output:
Enter radius of circle 5
Enter radius of cylinder 5
Enter height of cylinder: 6
Enter radius of cone: 5
Enter height of cone: 6
The area of a circle is: 78.54
The circumference of a circle is: 31.42
The area of a cylinder is: 345.58
The volume of a cone is: 157.08
Result:
Thus the C program to calculate the area of circle and cylinder, circumference of a circle
and volume of a cone was successfully executed and verified.
Aim:
To write a C program to display the ASCII value of an Entered character.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
char a;
printf("Enter a letter:");
scanf("%c",&a);
printf("\n The ASCII value is:%d",a);
getch();
}
Output:
Enter a letter: G
The ASCII value is: 71
Result:
Thus the C program to display the ASCII value of an Entered character was
successfully executed and verified.
Write a C program to check whether given number is even or odd using ternary operator.
Aim:
To write a C program to check whether a given number is even or odd using
Ternary operator.
Algorithm:
Step3: Get the value of the number to be checked from the user as ‘a’.
b=a%2==0? 1 : 0.
step5: If b==1; print the number is even. Else print the number is odd.
Program:
#include <stdio.h>
int main()
int a,b;
scanf("%d",&a);
b=a%2==0?1:0;
if (b==0)
else
return 0;
Output:
Enter value of a: 32
Result:
Thus the C program to display the ASCII value of an Entered character was
successfully executed and verified.
Write a C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f - g)). Read the
values a, b, c, d, e, f and g from the standard input device.
Aim:
To write a C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f -
g)) by taking input from a, b, c, d, e, f and g.
Algorithm:
Program:
#include<stdio.h>
#include<conio.h>
void main()
int a, b, c, d, e, f, g, result;
scanf("%d", &a);
scanf("%d", &b);
scanf("%d", &c);
scanf("%d", &d);
scanf("%d", &e);
scanf("%d", &f);
scanf("%d", &g);
getch();
Output:
Enter value of a: 1
Enter value of b: 6
Enter value of c: 3
Enter value of d: 4
Enter value of e: 3
Enter value of f: 2
Enter value of g: 1
Result:
Thus the C program to evaluate the arithmetic expression ((a + b / c * d - e) * (f -g)) by
taking input from a, b, c, d, e, f and g was successfully executed and verified.
Aim:
To write a C program to print the sizeof () for all data types.
Algorithm:
Step 1: Start the program.
Step 2: Get four different variables from the user to display the data type.
Program:
#include <stdio.h>
#include<conio.h>
int main()
{
int integerType;
char charType;
float floatType;
double doubletype;
printf("Size of int is: %ld”, sizeof(integerType));
printf("Size of char is: %ld”, sizeof(charType));
printf("Size of float is: %ld”, sizeof(floatType));
printf("Size of double is: %ld”, sizeof(doubleType));
return 0;
getch();
}
Output:
Size of int is: 4 Size
of char is: 1 Size of
float is: 4 Size of
double is: 8
Result:
Thus the C program to print the sizeof () for all data types was successfully
executed and verified.
1g. Swap two numbers using temporary variable and without using temporary
variable
Write a C program to swap two numbers using temporary variable and without using
temporary variable.
Aim:
To write a program to swap two numbers without using a temporary variable and
with using a temporary variable.
Algorithm:
(a) Without using a temporary variable:
Step 1: Start the program.
Step 2: Get the value of two numbers from the user, let it be a and b.
Step 3: Now perform these operations:
a=a-b
b=a+b
a=a+b
Step 4: Print the variables.
Step 5: Stop the program.
Program:
int a, b;
printf("Enter a: ");
scanf("%d", &a);
printf("Enter b: ");
scanf("%d", &b);
a = a - b;
b = a+b;
a = b - a;
printf("After swapping, a = %f\n", a);
printf("After swapping, b = %.d", b);
return 0;
getch();
}
Output:
Enter a: 25
Enter b: 50
After swapping, a=50
After swapping, b=25
Result:
Thus the C program to swap two numbers with using temporary variable and
without using temporary variable was successfully executed and the output was verified.
Aim:
To write a C program to check whether a given year is leap year or not using if-else.
Algorithm:
Step 1: Start the program.
Step 2: Take integer variable year
Step 3: Assign value to the variable
Step 4: Check if year is divisible by 4 but not 100, DISPLAY "leap year"
Step 5: Check if year is divisible by 400, DISPLAY "leap year"
Step 6: Otherwise, DISPLAY "not leap year"
Step 7: Stop the program.
Program:
#include<stdio.h>
int main()
{
int year;
printf(“Enter a year:\n”);scanf(“%d”, &year);
if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0)) printf("%d is a leap
year", year);
else
printf("%d is not a leap year", year);
return 0;
}
Output:
Enter a year: 2016 2016 is a leap year Enter a year: 2018 2018 is not a leap year
Result:
Thus the C program to check whether a given year is leap year or not using if-else has
been executed successfully and verified.
Aim:
To write a C program to check whether a given number is odd or even using if-else.
Algorithm:
int main() {
// Declare variable to store the number
int num;
Enter a number: 7
The number is odd
Enter a number: 12
The number is even
Result:
Thus the C program to check whether a given number is odd or even using if-else has been
executed successfully and verified.
Aim:
To write a C program to check the student is pass or fail using if-else.
Algorithm:
int main() {
// Declare variable to store the number
int num;
if (num>=35) {
printf("The student is pass ");
} else {
printf("The student is fail");
}
Enter a number: 7
The number is odd
Enter a number: 12
The number is even
Result:
Thus the C program to check whether the student is pass or fail using if-else has been executed
successfully and verified.
Write a C program to find the biggest among given three integers using nested if-else.
Aim:
To write a C program to find the biggest among given three integers using nested if-else.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the variables a, b and c.
Step 3: Compare the variables using nested if-else.
Step 4: Print the largest integer number.
Step 5: Stop the program.
Program:
#include<stdio.h>
void main()
{
int a, b, c;
printf("Enter three numbers\n");
scanf("%d %d %d", &a, &b, &c); if(a > b)
{
if(a > c)
printf("a: %d is largest\n", a);else
printf("b: %d is largest\n", b);
}
else if(b > c)
printf("b: %d is largest\n", b);else
printf("c: %d is largest\n", c);
}
Output:
Enter three numbers
45
89
63
b: 89 is largest
Result:
Thus the C program to find the biggest among given three integers using nested if-else
has been executed successfully and verified.
2e. Generate numbers between 20 and 100 divisible by 2 and not divisible by 3 and 5
Write a C program to generate numbers between 20 and 100 which are divisible by 2 and not
divisible by 3 and 5.
Aim:
To write a C program to generate numbers between 20 and 100 which are divisible by 2
and not divisible by 3 and 5.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the value of i to 20. And check the value of i less than or equal to 100.
If true go to step 3, otherwise go to step 6.
Step 3: Check whether i is divisible by 2 and not by 3 and 5. If yes print the value of i
else go to step 5.
Step 4: Increment the value of i by 1 and repeat step 3.
Step 5: Stop the program.
Program:
#include<stdio.h>
#include<conio.h>
int main()
{
printf(“The values between 20 and 100 that are divisible by 2 and not by 3 and 5 are”);
for(int i=20; i<100; i++)
{
if((i%2==0)&&(i%3!=0)&&(i%5!=0))
{
printf(“\n %d”,i);
}
}
getch();
return 0;
}
Output:
The values between 20 and 100 that are divisible by 2 and not by 3 and 5 are
22
26
28
32
34
38
44
46
52
56
58
62
64
68
74
76
82
86
88
92
94
98
Result:
Thus the C program to generate numbers between 20 and 100 which are divisible by 2
and not divisible by 3 and 5.
Break Statement:
Aim:
To write a C program to illustrate the function of break statement.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the input variable i.
Step 3: Increment the value of variable from 1 to 10 using for loop.
Step 4: If the value of i is 7, then break the condition.
Step 5: So the value of i prints from 1 to 6.
Step 6: Stop the program.
Program:
#include <stdio.h>
int main()
{
int i;
for(i=1;i<=10;++i)
{
if(i==7)
break;
printf("%d",i);
}
return 0;
}
Output:
123456
Continue Statement:
Aim:
To write a C program to illustrate the function of continue statement.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the input variable i.
Step 3: Increment the value of variable from 0 to 10 using for loop.
Step 4: If the value of i is 7, then break the condition.
Step 5: Print i value and 0 to 10 will be printed.
Step 6: Stop the program.
Program:
#include <stdio.h>
int main()
{
int i;
for(i=0;i<=10;++i)
{
if(i==7)
continue;
printf("%d",i);
}
return 0;
}
Output:
01234568910
Result:
Thus the C program to illustrate the function of break and continue statement was
executed successfully and verified.
Write a C program to perform the basic calculator operations, namely, addition, subtraction,
multiplication, division and square of a number using Switch case.
Aim:
To write a C program to perform the basic calculator operations, namely, addition,
subtraction, multiplication, division and square of a number using Switch case.
Algorithm:
Step 1: Start the program.
Step 2: Get two integer numbers from the user.
Step 3: Create a switch statement to get the calculator options add, subtract, multiply,
divide and square in cases.
Step 4: Ask the user to give their operation by selecting Case1: Sum, Case2: Subtract,
Case 3: Multiply, Case 4: Divide and Case 5: Square.
Step 5: If not given any of cases in Step 4, Print “Enter a valid operator”.
Step 6: Stop.
Program:
#include <stdio.h>
int main()
{
int a, b, c;
char operation;
printf(" Enter two numbers:");
scanf("%d%d", &a, &b)
printf("\n Press 1 to add %d and %d", a, b);
printf("\n Press 2 to subtract %d and %d", a, b);
printf("\n Press 3 to multiply %d and %d", a, b);
printf("\n Press 4 to divide %d and %d", a, b);
printf(“\n Press 5 to square %d”, a);
printf("\n Enter the operation: ");
scanf("%d", &c);
switch (c)
{
case 1:
printf("Sum: %d", a + b);
break;
case 2:
printf("Subtract :%d", a - b);
break;
case 3:
printf("Multiply :%d", a * b);
break;
case 4:
if (b == 0)
printf("\n Denominator cannot be zero");
else
printf("Divide :%d", a / b);
break;
case 5:
printf(“Square: %d”, a*a);break;
default:
printf("Enter a valid operator!");
}
return 0;
Output:
Enter two Numbers: 3 4
Press 1 to add 3 and 4
Press 2 to subtract 3 and 4
Press 3 to multiply 3 and 4
Press 4 to divide 3 and 4
Press 5 to square 3
Result:
Thus the C program to perform the basic calculator operations, namely, addition,
subtraction, multiplication, division and square of a number using Switch case has been executed
successfully and verified.
Aim:
To write a C program to display the first 25 positive integers.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the value to variable.
Step 3: In order to display the first 25 positive integers, iterate a loop usingfor.
Here i++ denotes the increment of numbers.
Step 4: Display the values in i.
Step 5: Stop the program.
Program:
#include<stdio.h>
void main()
{
int i;
printf("The first 25 positive integers are:\n ");
for (i = 1; i <=25; i++)
{
printf("%d ,", i);
}
}
Output:
The first 25 positive integers are:
1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ,10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,20 ,21 ,22 ,23
,24 ,25
Result:
Thus the C program to display the first 25 positive integers has been executed
successfully and verified.
Aim:
To write a C program to find the factorial of a given number
Algorithm:
Step 2. Input the number (let's call it num) whose factorial is to be calculated.
int main()
{
// Declare variables
int num, factorial = 1, counter = 1;
return 0;
}
Output:
Enter a number: 5
The factorial of 5 is 120
Result:
Thus the C program to find the factorial of the given number has been executed
successfully and verified.
Aim:
To write a C program to reverse the given number.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the value to variable 0 to rev, n, rem.
Step 3: Ask the user to enter the value of n.
Step 4: Check the value of n is not equal to zero if true goto step 5, otherwise go to
step 7.
Step 5: Perform the following expression to reverse the given number
rem=n%10
rev=rev*10+rem
n/=10
Step 6: Display the value of rev.
Step 7: Stop the program.
Program:
#include <stdio.h>
int main()
{
int n, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
printf("Reversed number = %d", rev);
return 0;
}
Output:
Enter the integer: 1231
Reversed number: 1321
Result:
Thus the C program to reverse the given number has been executed successfully and
verified.
Aim:
To write a C program to check whether a number is Palindrome or not.
Algorithm:
Step 1: Start the program.
Step 2: Initialize the value to variable 0 to rev, a to n, rem.
Step 3: Ask the user to enter the value of n.
Step 4: Check the value of n is not equal to 0, if true goto step 5, otherwise goto 7.
Step 5: Perform the following expression to check the given integer is palindrome
or not.
rem=n%10
rev=rev*10+rem
n/=10
Step 6: If a equals to rev, display “The given number is Palindrome” otherwise display
“The given number is not Palindrome”.
Step 7: Stop the program.
Program:
#include <stdio.h>
int main()
{
int n, rev = 0, rem, a;
printf("Enter an integer: ");
scanf("%d", &n);
a = n;
while (n != 0)
{
rem = n % 10;
rev = rev * 10 + rem;
n /= 10;
}
if (a == rev)
printf("%d is a Palindrome.", a);
else
printf("%d is not a Palindrome.", a);
return 0;
}
Output:
Enter an integer: 676
676 is a Palindrome.
Result:
Thus the C program to check whether the given number is Palindrome ornot has been executed
successfully and verified.
Write a C program to display odd and even numbers using while loop within a given
range.
Aim:
To write a C program to display odd and even number using while loop in a given
range.
Algorithm:
Step 1: Start the program.
Step 2: Read the lower limit as i.
Step 8: Stop.
Program:
#include
<stdio.h>
int main()
{
int odd[100],even[100],i,n,j=0,k=0;
printf(“Enter lower limit :”);
scanf(“%d”,&i);
printf(“Enter upper limit :”);
scanf(“%d”,&n);
while(i<=n)
{
if(i%2==0)
{
even[j]
=i;j++;
}
else
{
odd[k]
=i;k++;
}
i++;
}
printf(“The even numbers in the given range are:\n”);
for(i=0;i<k;++i)
{
printf(“%d\t”,odd[i]);}
printf(“\n”);
printf(“The even numbers in the given range are:\n”);
for(i=0;i<j;++i)
{
printf(“%d\t”,even[i]);}
return 0;
Output:
Enter lower limit: 50
Enter upper limit: 80
The even numbers in the given range are:
51 53 55 57 59 61 63 65 67 69 71 73
75 77 79
50 52 54 56 58 60 62 64 66 68 70 72
74 76 78 80
Result:
Thus the C program to display even and odd numbers using while loopwithin a
given range has been executed successfully and verified.
Aim:
To write a C program to print the elements of an array.
Algorithm:
Step 1: Start the program.
Step 2: Initialize an array ‘a’ with a size of 5.
Step 3: Create a loop that runs 5 times to insert the elements.
Step 4: Prompt the user to enter an element and store it in a variable element.
Step 5: Insert element into the array at the current index.
Step 6: Print the elements of the array.
Step 7: Create a loop that iterates over each element in the array.
Step 8: Print each element.
Step 9: Stop.
Program:
#include<stdio.h>
int main()
{
int a[]={2,4,6,8};
for(inti=0;i<=a[i];i++)
{
printf(“%d”,a[i]);
}
return 0;
}
Output:
2
4
6
8
Result:
Thus the C program to print the elements of the array has been done successfully and
the output is verified.
Aim:
To write a C program to find whether a search element is present in the array or not
(LINEAR SEARCH)
Algorithm
Step 2. Input the array of elements (let's call it arr) and the element to be searched (let's call it key).
Step 3. Initialize a variable (let's call it found) to keep track of whether the element is found, initially set
it to false.
Step 5. If found is true, print "Element found at index i", where i is the index of the element in the array.
If found is false, print "Element not found".
Program:
#include <stdio.h>
int main() {
int arr[100], n, key;
return 0;
}
Output:
(BINARY SEARCH)
Algorithm
Step 2. Input a sorted array of elements (let's call it arr) and the element to be searched (let's call it key).
Step 4. Repeat the following steps while low is less than or equal to high:
4.1. Compute mid as the average of low and high, i.e., mid = (low + high) / 2.
4.2. If the element at index mid is equal to the key, return mid (element found).
4.3. If the element at index mid is greater than the key, set high = mid - 1 (discard the right half).
4.4. If the element at index mid is less than the key, set low = mid + 1 (discard the left half).
Program:
#include <stdio.h>
int main() {
// Binary search
int low = 0, high = n - 1, found = 0, mid, index;
if (arr[mid] == key) {
found = 1;
index = mid;
break;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return 0;
}
Output:
Enter the number of elements in the array: 5
Enter 7 sorted elements:
1
3
5
7
9
11
13
Enter the element to be searched: 5
Element found at Position 3.
Result:
Thus the C program to find whether a search element is present in the array or not has
been done successfully andthe output is verified.
4c.Selection sort
Aim:
To write a C program to sort the elements in the array using selection sort.
Algorithm
Step 3. Iterate through the array from the first element to the second last element:
3.1. Assume the current element as the minimum element (let's call it min).
3.2. Iterate through the remaining unsorted elements:
3.2.1. If any element is smaller than min, update min to that element.
3.3. Swap the current element with the minimum element (min) found in the unsorted portion.
Program:
#include <stdio.h>
int main() {
int arr[100], n;
// Selection sort
for (int i = 0; i < n - 1; i++) {
// Assume the current element as the minimum
int min_index = i;
return 0;
}
Output:
Result:
Thus the C program to sort the elements in the array using selection sort has been done
successfully andthe output is verified.
Aim:
To write a C program to perform matrix addition.
Algorithm:
Step 1: Start the program.
Step 2: Initialize an empty matrix C with the same dimensions asmatrices A and B.
Step 3: Iterate over each row i and column j of matrices A, B, and C.
Step 4: Calculate the sum of the corresponding elements from matrices A and B.
Step 5: store it in the corresponding position of matrix C (C[i][j] = A[i][j] + B[i][j]).
Step 6: After iterating over all the elements, matrix C will contain the result of the
addition.
Step 7: Stop.
Program:
#include<stdio.h>
int main()
{
inti=0,j=0;
int g ,h;
printf(“Enter row size”);
scanf(“%d”,&g);
printf(Enter column size”);
scanf(“%d”,&h);
int a[g][h];
int b[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ ”);
printf(“Enter Second Matrix elements:” );
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“b[%d][%d]= ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf( “Second Matrix: ”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d ”,b[i][j]);
}
printf(“ ”);
}
printf(“Addition of Two Matrix “);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d ”,a[i][j]+b[i][j]);
}
printf(“ ” );
}
return 0;
}
Output:
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Second Matrix:
1 2 3
4 5 6
7 8 9
Result:
Thus the C program to print the Addition of Two Matrix has been done successfully
and the output is verified.
Aim:
To write a C program to perform matrix multiplication.
Algorithm:
Step 1: Start the program.
Step 2: Initialize an empty matrix C with the appropriate dimensions for the resulting
matrix.
Step 3: Iterate over each row i of matrix A.
Step 4: Iterate over each column j of matrix B.
Step 5: Iterate over each element k of the row i of matrix A and the column j of matrix
B.
Step 6: Multiply the corresponding elements from matrix A and matrix B, and
accumulate the result.
Step 7: Store the accumulated value in the corresponding position of matrix C (C[i][j]
= accumulated value).
Step 8: Repeat steps 2-6 until all rows and columns have been processed.
Step 9: After iterating over all the elements, matrix C will contain the result of the
matrix multiplication.
Step 10: Stop the program.
Program:
#include<stdio.h>
int main()
{
inti=0,j=0;
int g ,h;
printf(“enter row size”);
scanf(“%d”,&g);
printf(“enter column size”);
scanf(“%d”,&h);
int a[g][h];
int b[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=i,j);
scanf(“%d”,&a[i][j]);
}
}
Printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ ”);
printf(“Enter Second Matrix elements:” );
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“b[%d][%d]= ”,i,j);
scanf(“%d”,&b[i][j]);
}
}
printf( “Second Matrix: ”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
Printf(“%d ”,b[i][j]);
}
printf(“ ”);
}
printf(“Multiplication of Two Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
int c=0;
for(int k=0;k<h;k++)
{
c=c+(a[i][j]*b[i][j]);
}
printf(“%d”,c);
printf(“ “);
}
printf(“ “);
}
return 0;
}
Output:
Enter row size:3
Enter column size:3
Enter First Matrix elements:
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
First Matrix:
1 2 3
4 5 6
7 8 9
Enter Second Matrix elements:
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
Second Matrix:
1 2 3
4 5 6
7 8 9
Multiplication of two matrix:
30 36 42
66 81 96
102 126 150
Result:
Thus the C program to print the multiplication of Two Matrix has been done
successfully and the output is verified.
Aim:
To write a C program to perform transpose of a matrix.
Algorithm:
Step 1: Start the program.
Step 2: Initialize an empty matrix C with dimensions equal to the number of columns
in the original matrix A and the number of rows in A.
Step 3: Iterate over each row i and column j of matrix A.
Step 4: Assign the element A[i][j] to the position C[j][i].
Step 5: After iterating over all the elements, matrix C will be the transpose of matrix
A.
Step 6: Stop the program.
Program:
#include<stdio.h>
int main()
{
inti=0,j=0;
int g ,h;
printf(“enter row size”);
scanf(“%d”,&g);
printf(“enter column size”);
scanf(“%d”,&h);
int a[g][h];
int c[g][h];
printf(“Enter the First Matrix elements :”);
for (i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“a[%d] [%d]=”,i,j);
scanf(“%d”,&a[i][j]);
}
}
printf(“First Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf(“%d”,a[i][j]);
}
printf(“ “);
}
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
c[i][j]=a[i][j];
}
}
printf(“Transpose of Matrix:”);
for(i=0;i<g;i++)
{
for(j=0;j<h;j++)
{
printf( “%d”,c[i][j]);
}
printf(“ ”);
}
return 0;
}
Output:
Enter row size: 3
Enter column size: 3
Enter First Matrix elements:
a[0][0]=1
a[0][1]=2
a[0][2]=3
a[1][0]=4
a[1][1]=5
a[1][2]=6
a[2][0]=7
a[2][1]=8
a[2][2]=9
First Matrix:
1 2 3
4 5 6
7 8 9
Transpose of Matrix:
1 4 7
2 5 8
3 6 9
Result:
Thus the C program to print the transpose of a matrix has been done successfully and the
output is verified.