KEMBAR78
C Lab Programs2025 | PDF | Text File | Computer Engineering
0% found this document useful (0 votes)
22 views46 pages

C Lab Programs2025

The document provides a series of C programming exercises including algorithms and flowcharts for tasks such as calculating the sum and average of three numbers, finding the largest of three numbers, summing individual digits of a positive integer, generating Fibonacci sequences, identifying prime numbers, and checking for Armstrong and perfect numbers. Each exercise includes a detailed algorithm, flowchart, and sample C code. The document serves as a comprehensive guide for learning basic programming concepts in C.

Uploaded by

Krish Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views46 pages

C Lab Programs2025

The document provides a series of C programming exercises including algorithms and flowcharts for tasks such as calculating the sum and average of three numbers, finding the largest of three numbers, summing individual digits of a positive integer, generating Fibonacci sequences, identifying prime numbers, and checking for Armstrong and perfect numbers. Each exercise includes a detailed algorithm, flowchart, and sample C code. The document serves as a comprehensive guide for learning basic programming concepts in C.

Uploaded by

Krish Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 46

DEPARTMENT OF CSE

1. a) Write a C program to find the sum and average of three numbers.

Algorithm:

Step 1: Start

Step 2: Declare variables num1, num2,num3 and sum,average.

Step 3: Read values num1,num2,num3

Step 4: Add num1,num2,num3 and assign the result to sum.

sum←num1+num2 +num3

average ← sum/3

Step 5: Display sum and average

Step 6: Stop

Flow Chart:

MRCET EAMCET CODE: MLRD


DEPARTMENT OF CSE

Program:

#include<stdio.h>
int main( )
{
int a,b,c;
int sum,average;
printf("Enter any three integers:
"); scanf("%d%d %d",&a,&b,&c);
sum = a+b+c;
average=sum/3
printf("Sum and average of three integers: %d %d",sum,average);
return 0;
}

SAMPLE INPUT:
Enter any three integers:2 4 5

EXPECTED OUTPUT:

Sum and average of three integers: 11 3

1. Algorithm:
1. Start
2. Input three numbers (A, B, and C).
3. Compare A with B:
o If A > B, go to step 4; otherwise, go to step 5.
4. Compare A with C:
o If A > C, A is the largest number.
o Otherwise, C is the largest number.
5. Compare B with C:
o If B > C, B is the largest number.
o Otherwise, C is the largest number.
6. Display the largest number.
7. End

MRCET EAMCET CODE: MLRD


DEPARTMENT OF CSE
2. Flowchart:
Here’s a textual representation of a flowchart:
sql
Copy
+---------------------+
| Start |
+---------------------+
|
v
+---------------------+
| Input A, B, C |
+---------------------+
|
v
+---------------------+
| Is A > B? |
+---------------------+
| No | Yes
v v
+---------------------+ +---------------------+
| Is A > C? | | Is B > C? |
+---------------------+ +---------------------+
| No | Yes | No | Yes
v v v v
+---------------------+ +---------------------+ +---------------------+
| C is largest | | A is largest | | B is largest |
+---------------------+ +---------------------+ +---------------------+
| | |
+------------------------+-----------------------+
|
v
+------------------------+
| Display largest number |
+------------------------+
|
v
+------------------------+
| End |
+------------------------+
3. C Program:
c
Copy
#include <stdio.h>

int main() {
int a, b, c;

// Input three numbers


printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
MRCET EAMCET CODE: MLRD
DEPARTMENT OF CSE

// Finding the largest number


if (a >= b && a >= c) {
printf("%d is the largest number.\n", a);
}
else if (b >= a && b >= c) {
printf("%d is the largest number.\n", b);
}
else {
printf("%d is the largest number.\n", c);
}

return 0;
}

Algorithm:
1. Read three numbers, say a, b, and c.
2. Initialize a variable big to store the largest number.
3. Compare a and b using if-else to find the larger of the two, and store the result in big.
4. Compare big with c using if-else to find the largest among big and c.
5. Print the largest number stored in big.
Flowchart:
plaintext
Copy
+------------------+
| Start |
+------------------+
|
v
+------------------+
| Input a, b, c |
+------------------+
|
v
+------------------+
| If a > b then |-----No-----> [Else]
| big = a |
+------------------+
|
v
+-------------------+
| If big > c then |-----No-----> [Else]
| big = c |
+-------------------+
|
v
MRCET EAMCET CODE: MLRD
DEPARTMENT OF CSE
+-------------------+
| Display big |
+-------------------+
|
v
+------------------+
| End |
+------------------+
C Program:
c
Copy
#include <stdio.h>

int main() {
int a, b, c;
int big;

// Input three numbers


printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

// Compare a and b
if (a > b) {
big = a;
} else {
big = b;
}

// Compare big with c


if (big < c) {
big = c;
}

// Output the largest number


printf("The largest number is: %d\n", big);

return 0;
}

Example Output:
yaml
Copy
Enter three numbers: 12 45 23
The largest number is: 45

MRCET EAMCET CODE: MLRD


DEPARTMENT OF CSE

C program to find the largest of three numbers without using relational


operators (>, <, >=, <=, ==, or !=):
c
Copy
#include <stdio.h>

int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

// Using arithmetic operators to find the largest number


int max = a;
max = (a - b + abs(a - b)) / 2; // Between a and b
max = (max - c + abs(max - c)) / 2; // Between max and c

printf("The largest number is: %d\n", max);


return 0;
}

MRCET EAMCET CODE: MLRD


DEPARTMENT OF CSE

2. b) Write a C program to find the sum of individual digits of positive


integer.

AIM:
To find the sum of individual digits of positive integer.
Description:
Summation of digits of a number
Ex: 1234
Summation =1+2+3+4=10
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Initialize sum ← 0
Step 4: while(n!=0)
Begin
Step 5: r←n%10
Step 6: sum←sum+r
Step 7: n←n/10
End
Step 8: Print “sum”
Step 9: Stop
FLOWCHART: Start

Read n

Sum = 0

while False
n!=0

True
r=n%10
sum=sum+r
n=n/10

Print Sum

Stop
MRCET EAMCET CODE: w w w .m r c e t.
DEPARTMENT OF CSE

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,sum=0;
clrscr();
printf("ENTER A POSITIVE INTEGER \n");
scanf("%d",&n); while(n!
=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
printf("THE SUMOF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..%d",sum);
getch();
}

SAMPLE INPUT:
ENTER A POSITIVE
INTEGER 5 3 2 1

EXPECTED OUTPUT:
THE SUM OF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..11

3).c) Fibonacci Sequence is defined as follows: the first and second


terms in the sequence are 0 and 1. Subsequent terms are found by
adding the preceding two terms in the sequence. Write a C program
to generate the first n terms of the sequence.

AIM: To generate the first n terms of the Fibonacci sequence..


Description: Initial Fibonacci numbers are 0 and 1. Next number can be generated by adding two
numbers. So 0+1=1. Therefore next number can be generated by adding two previous . so Fibonacci series
is 0 1 1 2 3 5 ……

ALGORITHM:
Step 1 : Start
Step 2 : Read n
Step 3 : Initialize f0 ← 0, f1 ← 1, f ← 0
Step 4 :i=0
Step 5 : while(i<=n) do as follows printf("%d\
t",f0);
f=f0+f1;
f0=f1;
f1=f;
DEPARTMENT OF CSE
i=i+1;
If not goto step 7
Step 6 : Stop
FLOWCHART:
Start

f0=0, f1=1

i=0

FALSE
while
i<n

Stop TRUE Print f0


f=f0+f1;
Print f0 f0=f1;
f1=f;
i=i+1;

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in


DEPARTMENT OF CSE

PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int f0,f1,f,n,i;
clrscr();
printf("ENTER THE VALUE FOR n \n");
scanf("%d",&n);
f0=0;
f1=1;
printf("FIBONACCI SEQUENCE FOR THE FIRST %d TERMS:\n",n);
i=0;
while(i<n)
{
printf("%d\t",f0);
f=f0+f1;
f0=f1;
f1=f;
i=i+1;
}
}

INPUT:
ENTER THE VALUE FOR n
10

OUTPUT:
FIBONACCI SEQUENCE FOR THE FIRST 10 TERMS:
0 1 1 2 3 5 8 13 21 34

4) a)Write a C program to generate all prime numbers


between 1 and n. Where n is the value supplied by
the user.
Aim: To print a prime numbers up to 1 to n
Description:
Prime number is a number which is exactly divisible by one and itself only
Ex: 2, 3,5,7,………;

Algorithm:
Step 1: start
Step 2: read n
Step 3: initialize i=1,c=0
Step 4:if i<=n goto step
5
If not goto step 10
Step 5: initialize j=1
Step 6: if j<=i do the following. If no goto step 7
i) if i%j==0 increment c
DEPARTMENT OF CSE
ii) increment j
iii) goto Step 6
Step 7: if c== 2 print i
Step 8: increment i start
Step 9: goto step 4
Step 10: stop

FLOWCHART: Read n

false I=1

I<=n

J = 1

J++
false true

If fal
If I %
j
Output == 0
i true

Fact

MRCET EAMCEeTndCODE: w w w .m r c e t. ac.in 7


MLRD
DEPARTMENT OF CSE

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact,j;
clrscr();
printf("enter the number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ fact=0;
//THIS LOOP WILL CHECK A NO TO BE PRIME NO. OR NOT.
for(j=1;j<=i;j++)
{
if(i%j==0)
fact++;
}
if(fact==2)
printf("\n %d",i);
}
getch( );
}
Output:
Enter the number : 5
2 3 5

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 8


DEPARTMENT OF CSE

5) b) Write a C program to Check whether given number is Armstrong Number


or Not.

AIM: To Check whether given number is Armstrong Number or Not

Algorithm:
Armstrong number
Step 1: start
Step 2:read n
Step 3:assign sum 0,Imn,count =0
Step 4:if m>0 repeat
Step 4.1:mm/10
Step 4.2:count++
Step 4.3:until the condition fail
Step5: if I>0 repeat step 4 until condition
fail
Step 5.1:remI%10
Step 5.2:sumsum+pow(rem,count)
Step 5.3:II/10
Step 6:if n=sum print Armstrong otherwise print not armstrong
Step 7:stop

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 9


DEPARTMENT OF CSE

Program:

#include <stdio.h>
int main()
{
int n, n1, rem, num=0;
printf("Enter a positive integer: ");
scanf("%d", &n);
n1=n; while(n1!
=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}
if(num==n)
printf("%d is an Armstrong number.",n);
else
printf("%d is not an Armstrong number.",n);
}
Input:
Enter a positive integer: 371
Output:
371 is an Armstrong number.

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 10


DEPARTMENT OF CSE

6) a). Write a C program to check whether given number is perfect number or


Not

AIM: To Check whether given number is perfect number or not

Algorithm:
Perfect number
Step 1: read n
Step 2: assign i=1,sum=0
Step 3: while(i<n) goto step 4

Step 4: if(n%i==0)
sum=sum+i
i++
step 5: if(sum==n) print given number is perfect number otherwise not a perfect number.

Program:

#include<stdio.h>
int main()
{
int n,i=1,sum=0;

printf("Enter a number: ");


scanf("%d",&n);

while(i<n)
{
if(n%i==0)
sum=sum+i;
i++;
}
if(sum==n)
printf("%d is a perfect number",i);
else
printf("%d is not a perfect number",i);

return 0;
}

Input:

Enter a number:6

Output:

6 is a perfect number

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 11


DEPARTMENT OF CSE

7) b) Write a C program to check whether a number is strong number or not.

AIM: To check whether given number is strong number or not


Algorithm:
Strong number
Step 1:read num,i,f,r,sum=0,temp
Step 2: assign num to temp
Step 3: while(num) goto step 4
Step 4: i=1,f=1
r=num%10
while(i<=r) goto step
5
Step 5: f=f*i
i=i+1
Step 6: sum=sum+f;
Step 7: num=num/10;
Step 8: if sum and temp are equal got step 9
Step 9: print strong number otherwise not a strong number
Program:
#include<stdio.h>
int main() {
int num,i,f,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num) {
i=1,f=1;
r=num%10;
while(i<=r)
{
f=f*i;
i++;
}
sum=sum+f;
num=num/10;
}
if(sum==temp)
printf("%d is a strong number",temp); else
printf("%d is not a strong number",temp);
return 0;
}
Input:
Enter a number:145
Output:
MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 12
DEPARTMENT OF CSE
145 is a strong number

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 13


DEPARTMENT OF CSE

8) a) Write a C program to find the roots of a quadratic equation.

AIM: To find the roots of a quadratic equation.


−𝑏±√𝑏2−4
Description: roots of quadratic equation are 𝑎𝑐
2𝑎
ALGORITHM:
Step 1: Start
Step 2: Read a,b,c
Step 3: calculate disc = b*b-4*a*c
Step 4: if(disc>0)
Begin
Step 5: root1=(-b+sqrt(disc))/(2*a)
Step 6: root2=(-b-sqrt(disc))/(2*a)
Step 7: Print “Root1” , “Root2”
End
Step 8: else if(disc=0)
Begin
Step 9: root1=-b/(2*a)
Step 10: root2=root1;
Step 11: Print “Root1” , “Root2”
End
Step 12: else
Step 13: Print Roots are imaginary
Step 14: Stop

Flow Chart

EAMCET CODE: MLRD ww


MRCET
DEPARTMENT OF CSE

PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int a,b,c;
float disc,root1,root2;
float img,real;
printf("ENTER VALUES FOR a,b,c:\n");
scanf("%d%d%d",&a,&b,&c);
disc=(float)b*b-4*a*c;
if(disc>0)
{ printf("THE ROOTS ARE REAL & UNEQUAL:\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
}
else if(disc==0)
{
printf("THE ROOTS ARE REAL AND EQUAL:\n");
root1=-b/(2*a);
root2=root1;
printf("Root1=%f\n",root1);
printf("Root2=%f\n",root2);
}
else
{ printf("THE ROOTS ARE IMAGINARY:\n");
disc=-disc;
img=(float)disc/2*a;
real=(float)-b/2*a;
if (img>0)
{ printf("Root1=%f + i%f\n",real,img);
printf("Root2=%f - i%f\n",real,img);
}
else
{ img=-img;
printf("Root1=%f + i%f\n",real,img);
printf("Root2=%f - i%f\n",real,img);
}

}
return 0;
}

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 17


DEPARTMENT OF CSE

INPUT:
ENTER VALUES FOR a,b,c
1 4 4
OUTPUT:
THE ROOTS ARE EQUAL AND THEY ARE.. Root1=-2 Root2=-2

Record at least 3 results

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 18


DEPARTMENT OF CSE

9) b). Write a C program which takes two integer operands and


one operator from the user, performs the operation and then prints
the result.(Consider the operators +,-,*,/,% and use Switch
Statement.)

AIM:
To perform arithmetic operations using switch statement.
Algorithm:
Step 1: Read a,b
Step 2: Print “Menu Options”
Step 3: do
Begin Step 4: Read ch
Step 5: switch(ch)
Begin Step 6:
case 1:
Begin
Calculate c = a+b
Print “c”
break;
End
case 2:
Begin
Calculate c = a-b
Print “c”
break;
End
case 3:
Begin
Calculate c = a*b
Print “c”
break;
End
case 4:
Begin
Calculate c = a/b
Print “c”
break;
End
case 5:
Begin
Calculate c = a%b
Print “c”
break;
End
default:
Print “Invalid choice”

End

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 19


DEPARTMENT OF CSE

Flowchart

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,ch; clrscr();
printf("ENTER TWO VALUES FOR a & b\n"); scanf("%d %d",&a,&b);
while(1) {
printf("MENU OPTIONS \n");
printf("************\n");
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Division\n");
printf("5.Modulus\n"); printf(“6.Exit\
n”);
printf("\n");
printf("ENTER UR CHOICE\n");
scanf("%d",&ch);
switch(ch) {

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 20


DEPARTMENT OF CSE

case 1: c=a+b;
printf("The addition of %d and %d is..%d\n",a,b,c); break;
case 2: c=a-b;
printf("The subtraction of %d and %d is..%d\n",a,b,c); break;
case 3: c=a*b;
printf("The multiplication of %d and %d is..%d\n",a,b,c);
break; case 4: c=a/b;
printf("The division of %d and %d is..%d\n",a,b,c); break;
case 5: c=a%b;
printf("The modulus of %d and %d is..%d\n",a,b,c); break;
case 6:exit(0); default:printf("INVALID CHOICE\n"); }
}
getch();
}

INPUT:
ENTER TWO VALUES FOR a & b: 20 16

OUTPUT:

MENU OPTIONS
1.Addition 2.Subtraction 3.Multiplication 4.Division 5.Modulus
6.Exit

ENTER UR CHOICE 1
The addition of 20 and 16 is..36

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 21


DEPARTMENT OF CSE

10) a) Write a C program to find the factorial of a given integer


using non-recursive function.

AIM:
To find the factorial of a given number using non-recursive function.
ALGORITHM:
Step 1: Start
Step 2: Read n
Step 3: Call fact(n) goto step 6
Step 4: Store result in “f”
Step 5: Print “f” goto step 10
Step 6: Begin //sub program
Initialize f ← 1
Step 7: for i is 1 to n by step 2
Step 8: Calculate f = f*i
Step 9: return “f”
End
Step 10: Stop
FLOWCHART: Start

Read n

f = fact(n)

f=1
Print “f”

For i is 1 to n by
Step 1
FALSE
Stop TRUE
f = f*i

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 22


DEPARTMENT OF CSE

PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,i,f;
clrscr();
printf("ENTER A VALUE FOR n:\n");
scanf("%d",&n);
f=fact(n);
printf("THE FACTORIAL OF A GIVEN NO IS..%d",f);
getch();
}
int fact(int n)
{
int i,f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f);
}
INPUT:
ENTER A VALUE FOR n
5
OUTPUT:
THE FACTORIAL OF A GIVEN NUMBER IS..120

Record at least 3 results

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 23


DEPARTMENT OF CSE

11) b) Write a C program to find the factorial of a given integer using


recursive function.

AIM:
To find the factorial of a given number using recursive function.

ALGORITHM:
main program
Step 1: start
Step 2: read n
Step 3: call sub program as f=fact(n)
Step 4: print f value
Step 5: stop

Sub program:
Step 1: initialize the f
Step 2: if n= = 0 or n == 1 return 1 to main program if not goto step 3
Step 3: return n*fact(n-1) to main program
FLOW CHART:

Main Program Sub Program

Start

Fact ()

Read n
False
If n=0 || n=1

Call subprogram
F = fact(n) True
Return
n*fact(n-1)

Print f

Return n to
main program
Stop

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 24


DEPARTMENT OF CSE

PROGRAM:
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,res;
clrscr();
printf("ENETR A NUMBER:\n");
scanf("%d",&n);
res=fact(n);
printf("THE FACTORIAL OF A GIVEN NUMBER IS..%d",res);
getch();
}
int fact(int n)
{
int r;
if(n==0)
return(1);
else
{
r=n*fact(n-1);
return(r);
}
}

INPUT:
ENTER A VALUE FOR n
5

OUTPUT:
THE FACTORIAL OF A GIVEN NUMBER IS..120

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 25


DEPARTMENT OF CSE

12) b) Write a C Program to Sort the Array in an Ascending Order.

Program:
C Program to Sort the Array in an Ascending Order
#include <stdio.h>

void main()

int i, j, a, n, number[30];

printf("Enter the value of N \n");

scanf("%d", &n);

printf("Enter the numbers \n");

for (i = 0; i < n; ++i)

scanf("%d", &number[i]);

for (i = 0; i < n; ++i)

for (j = i + 1; j < n; ++j)

if (number[i] > number[j])

a = number[i];

number[i] = number[j];

number[j] = a;

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 26


DEPARTMENT OF CSE

printf("The numbers arranged in ascending order are given below \n");

for (i = 0; i < n; ++i)

printf("%d\n", number[i]);

}
Output:
Enter the value of N
6

Enter the numbers


3
78
90
456
780
200
The numbers arranged in ascending order are given below
3
78
90
200
456
780

Record at least 3 results

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 27


DEPARTMENT OF CSE

12) a) Write a C program to perform addition of two matrices.

AIM:
To perform addition of two matrices.
ALGORITHM:
Step 1: Start
Step21: for i is 0 to 2 by step 1
for j is 0 to 2 by step 1
Step 3: Read a[i][j],b[i][j]
Step 4: goto step 2
Step 5: calculate c[i][j]=a[i][j]+b[i]
[j] Step 6: goto step 2
Step 7: Print c[i]
[j] Step 8: Stop
Flow Chart:
Start

For i=0;i<3;i++ F
For j=0;j<3;j++
T

Read a[i][j], b[i][j]

For i=0;i<3;i++
For j=0;j<3;j++
F
T
c[i][j]=a[i][j]+b[i][j]

For i=0;i<3;i++
For j=0;j<3;j++

T
Print c[i][j] Stop

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 28


DEPARTMENT OF CSE

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3],b[3][3],c[3][3];
int i,j;
clrscr();
printf("ENTER A MATRIX\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("ENTER B MATRIX\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++) c[i]
[j]=a[i][j]+b[i][j];
}
printf(" After addition of two matrices :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 29


DEPARTMENT OF CSE

INPUT:
ENTER a MATRIX
1 2 3
4 5 6
7 8 9
ENTER b MATRIX
1 1 1
1 1 1
1 1 1

OUTPUT:
After addition of two matrices is..
2 3 4
5 6 7
8 9 10

Record at least 3 results

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 30


DEPARTMENT OF CSE

13) b)Write a C program that uses functions to perform Multiplication of


Two Matrices.

AIM:
To perform multiplication of two matrices.
ALGORITHM:
Step 1: Start
Step21: for i is 0 to 2 by step 1
for j is 0 to 2 by step 1
Step 3: Read a[i][j],b[i][j]
Step 4: goto step 2
Step 5: calculate c[i][j]=c[i][j]+a[i][k]*b[k][j]
Step 6: goto step 2
Step 7: Print c[i][j]
Step 8: Stop

Program:
#include<stdio.h >
#include<conio.h>
int i,j,k;
void main()
{
int a[10][10],b[10][10],c[10][10],m,n,p,q;
void mul(int x[10][10],int y[10][10],int z[10][10],int m,int n,int p,int q);
void read(int x[10][10],int m,int n);
void display(int x[10][10], int m,int n);
clrscr();
printf("Enter the size of A Mtrix (Row and Col): \n"); scanf("%d
%d",&m,&n);
printf("Enter the size of B Mtrix (Row and Col): \n"); scanf("%d
%d",&p,&q);
if(n!=p)
{
printf("Multiplication Not Possible\n Please re-enter\
n"); printf("correct size and try again \n");
}
else
{
read(a,m,n);
read(b,m,n);
mul(a,b,c,m,n,p,q);
printf("A Matrix is :\n");
display(a,m,n);

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 31


DEPARTMENT OF CSE

printf("B Matrix is :\n");

display(b,m,n);
printf("C Matrix is :\n");
display(c,m,n);
}
getch();
}
void mul(int x[10][10],int y[10][10],int z[10][10],int m,int n,int p,int q)
{
for (i=0;i<m;i++)
for(j=0;j<q;j++)
{
z[i][j]=0;
for(k=0;k<n;k++)
z[i][j]+= x[i][k]*y[k][j];
}

void read(int x[10][10], int m,int n)


{
printf("Enter Matrix Value Row by Row\n");
for (i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);

void display(int x[10][10], int m,int n)


{
for (i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",x[i][j]); printf("\
n");
}
printf("\n");

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 32


DEPARTMENT OF CSE

Input:
Enter the size of A Mtrix (Row and Col): 2 2

Enter the size of B Mtrix (Row and Col): 2 2

Enter Matrix Value Row by Row


1 0
2 6

Enter Matrix Value Row by Row

3 4
4 2

Output:

A matrix is:
1 0
2 6

B Matrix is:

3 4
4 2

C matrix is:

3 4
24 20

Record at least 3 results

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 33


DEPARTMENT OF CSE

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 34


DEPARTMENT OF CSE

13) a) Write a C program using user defined functions to determine whether the
given string is palindrome or not.

Aim: To determine if the given string is palindrome or not.


Description :
Palindrome means string on reversal should be same as original
Ex: madam on reversal is also madam
Algorithm:
Step 1: start
Step 2: read string A
Step 3: copy string A into
B Step 4: reverse string B
Step 5: compare A &B
If A equals B to got step 6
Else goto step 7
Step 6:print given string A is pallindrom
Step 7:print given string is not pallindroma
Step 8: stop
Flow Chart:

MRCET EAMCET CODE: MLRD www.


DEPARTMENT OF CSE

Program:
#include <stdio.h>
#include
<string.h> void
main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;

printf("Enter a string \n");


gets(string);
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}

for (flag = 1, i = 0; i < length ; i++)


{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}
Input:
Enter a string
madam
Output:
The length of the string 'madam' = 5
madam is a palindrome
Record at least 3 results
DEPARTMENT OF CSE

MRCET
CET EAMCET CODE: MLRD w w w .m r c e t. ac.in 37
DEPARTMENT OF CSE

14) b) Write a C Program to Calculate Total and Percentage marks of a student


using structure.

Program:

#include<stdio.h>
#include<conio.h>
struct student
{
int rl;
char nm[20];
int m1;
int m2;
int m3;
int t;
float per;
};
void main()
{
struct student a;
clrscr();
printf(" Enter RollNo, Name amd three sub marks\n"); scanf("%d%s%d
%d%d",&a.rl,&a.nm,&a.m1,&a.m2,&a.m3); a.t=a.m1+a.m2+a.m3;
a.per=a.t/3.0; printf("rollno=%d\
n",a.rl); printf("Name=%sk\
n",a.nm); printf("m1=%d\n",a.m1);
printf("m2=%d\n",a.m2);
printf("m3=%d\n",a.m3);
printf("total=%d\n",a.t);
printf("per=%f\n",a.per);
getch();
}
Input:
Enter RollNo, Name and three sub marks
12 rama 30 40 50
Output:
rollno=12
Name=rama
m1=30
m2=40
m3=50
total=120
per=40.000000

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 38


DEPARTMENT OF CSE

14)b) write a c program to display the contents of a file.

Aim: To display the contents of a file.

Program:

#include <stdio.h>
#include <conio.h>
#include<string.h>
#include <process.h>
int main()
{
FILE *fs;
char ch;
char *fname;

printf("Enter the file name :");


gets(fname);
fs = fopen(fname,"r");
if(fs==NULL)
{
puts("Source file cannot be opened.");
getch();
}
else
{
while((ch=fgetc(fs))!=EOF)
{

putchar(ch);

}
}
getch();
return 0;
}

Input:
Enter the file name :sample.txt
Output:
this is my first program

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 39


DEPARTMENT OF CSE

Record at least 3 results

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 40


DEPARTMENT OF CSE

15)a) Write a C program to copy the contents of one file to another.

Aim:
Program which copies one file to another

Algorithm:
Step 1: Start
Step 2: read command line arguments
Step 3: check if no of arguments =3 or not. If not print invalid no of arguments
Step 4: open source file in read mode
Step 5: if NULL pointer, then print source file can not be open
Step 6: open destination file in write mode
Step 7: if NULL pointer, then print destination file can not be open
Step 8 : read a character from source file and write to destination file until EOF
Step 9: Close source file and destination file
Step 10: Stop

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 41


DEPARTMENT OF CSE

Program:
#include<stdio.h>
#include<process.h>
#include<conio.h>
void main()
{
FILE *ft,*fs;
int c=0;
clrscr();
fs=fopen("a.txt","r");
ft=fopen("b.txt","w");
if(fs==NULL)
{
printf("Source file opening error\n");
exit(1);
}
else
if(ft==NULL)
{

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 42


DEPARTMENT OF CSE

printf("Target file opening error\n");


exit(1);
}
while(!feof(fs))
{
fputc(fgetc(fs),ft);
c++;
}
printf("%d bytes copied from 'a.txt' to 'b.txt'",c);
c=fcloseall();
printf("%d files closed",c);
}

INPUT:
a.txt
An array is a collection of elements of similar datatypes

OUTPUT:
57 bytes copied from ‘a.txt’ to ‘b.txt’
2 files closed
Record at least 3 results

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 43


DEPARTMENT OF CSE

MRCET EAMCET CODE: MLRD w w w .m r c e t. ac.in 44

You might also like