0% found this document useful (0 votes)
8 views

c Programming Lab r20 Cse Final

The document is a lab manual for a C programming course at Audisankara College of Engineering and Technology, detailing various programming exercises. It includes algorithms, flowcharts, and sample programs for tasks such as evaluating algebraic expressions, finding the greatest number, and calculating factorials. The manual serves as a guide for students to practice and implement C programming concepts.

Uploaded by

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

c Programming Lab r20 Cse Final

The document is a lab manual for a C programming course at Audisankara College of Engineering and Technology, detailing various programming exercises. It includes algorithms, flowcharts, and sample programs for tasks such as evaluating algebraic expressions, finding the greatest number, and calculating factorials. The manual serves as a guide for students to practice and implement C programming concepts.

Uploaded by

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

AUDISANKARA COLLEGE

OF
ENGINEERING AND TECHNOLOGY
(AUTONOMOUS)
NH5,BYPASS ROAD,GUDUR.

DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
OF
C PROGRAMMING LAB
20CS102
AUDISANKARA COLLEGE
OF
ENGINEERING AND TECHNOLOGY
NH5,BYPASS ROAD,GUDUR.

AUDISANKARA
DEPARTMENT
OF
COMPUTER SCIENCE AND ENGINEERING

C PROGRAMMING LAB
LAB MANUAL

Prepared by: Verified by:


C PPRRO
OGGR
RAAM
MMMIIN
NGG LLA
ABB

INDEX

S .No Name of the Experiments

1 Programs on Expressions

2 Programs on Operators.

3 Programs on decision control statements

4 Programs on loop statements.

5 Programs on Nested Loops.

6 Programs using arrays.

7 Programs to implement on functions.

8 Programs using recursion.

9 Programs to implement string handling functions.

10 Programs to implement on pointers.

11 Programs to implement on structures.

12 Programs on files.
C PROGRAMMING LAB MANUAL

Exercise-1

1(A).Write a C program to evaluate the algebraic expression (ax+b)/(ax-b)

AIM: To evaluate algebraic exp (ax+b)/(ax-b)

ALGORITHM:
Step1: start

Step2: input a,b,x,s

Step3: s= (a*x+b)/(a*x-b)

Step4: Result s

Step 5: stop

FLOW CHART:
STOP
To evaluate algebraic exp (ax+b)/ (ax-b)

take a,b,x,S

a=5,b=4,x=3

S=(ax+b)/(ax-b)

Display S

STOP
C PROGRAMMING LAB MANUAL

PROGRAM:
To evaluate algebraic exp(ax+b)/(ax-b)
#include<stdio.h>
#include<conio.h>
void main()

int a,b,x;

float s;

clrscr();

printf("enter the values of a,b,x...");

scanf("%d%d%d",&a,&b,&x);

s=(a*x+b)/(a*x-b);

printf("the value of s=%f",s);

getch();

}
Input & Output:
Enter the values of a,b,x… 2 4 6

The value of s = 2.000000

1(B).Write a C program to evaluate algebraic expression


x power5 +10 x power 4+8 x power3+4x+2

AIM: To evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2

ALGORITHM:

Step1: start

Step2: input x,s

Step3: s=pow(x,s)+10*pow(x,4)+8*pow(x,3)+4*x+2

Step4: Result s

Step 5: stop

Audisankara College of Engg & Technology(Autonomous). 1


C PROGRAMMING LAB MANUAL

FLOWCHART:

To evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2

start

take x

x=6

S=pow(x,s)+10*pow(x,4)+8*pow(x,3)+4*x+2

Display S

stop

PROGRAM:

To evaluate algebraic exp x power5 +10 x power 4+8 x power3+4x+2

#include<stdio.h>
#include<math.h>
Void main ()
{
float x,s;
printf("enter the values of x:");
scanf("%f",&x);
s=pow(x,5)+10*pow(x,4)+8*pow(x,3)+4*x+2;
printf("the value of s=%f",s);
getch();
}

Audisankara College of Engg & Technology(Autonomous). 2


C PROGRAMMING LAB MANUAL

Input & Output:

Enter the values of x:1

The value of s = 25

1(C). Write a C program to Evaluate Area of triangle (sqrt(s(s-a)(s-b)(s-c)))

AIM: To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)))

ALGORITHM:

Step1: start

Step2: input a,r,t,s;


Step3 area=sqrt(s*(s-a)*(s-b)*(s-c));
Step4: Result s

Step 5: stop
FLOWCHART:
To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)))

Start

take a,b,c,s,a

a=5,b=4,c=2

s=(a+b+c)/2

area=sqrt(s-a)(s-b)(s-c)

Print Area

Stop

Audisankara College of Engg & Technology(Autonomous). 3


C PROGRAMMING LAB MANUAL

PROGRAM:

To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)))


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float s,area;
clrscr();
printf("enter the values of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);
s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("the area of a trangle is =%f",area);
getch();
}

Input & Output:


enter the values of a,b,c
5
6
7
The area of a trangle is = 14.696939

1(D). Write a C program to Swapping of given two numbers

AIM: Program to swap two numbers

ALGORITHM:

Step1: start

Step2: input a,b

Step3: a=a+b

Step4: b=a-b

Step 5: a=a-b

Step6: Result a,b

Step7: stop

Audisankara College of Engg & Technology(Autonomous). 4


C PROGRAMMING LAB MANUAL

FLOWCHART:
Program to swap two numbers

start

take a,b

a=1,b=10

a=a+b;b=a-b;a=a-b

Print a,b

stop

PROGRAM: Program to swap two numbers

void main()
{
int a,b;
clrscr();
printf("enter the values of a,b\n");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("the values of a,b are: %d %d",a,b);
getch();
}
Input & Output:

Enter the values of a,b


10
20
The values of a, b are: 20 10

Audisankara College of Engg & Technology(Autonomous). 5


C PROGRAMMING LAB MANUAL

Exercise-2

2(A). Write a C program to Find greatest number in given two numbers


using conditional operator

AIM: Program to find greatest of 2 numbers using conditional operator

ALGORITHM:

Step1: start

Step2: input a,b,c

Step3: c=(a>b)?a:b

Step4: Result c

Step 5: stop

FLOWCHART:

To Find Greatest of Two numbers.

Start

Take a,b

C= (a>b)? a:b

Print c

Stop

Audisankara College of Engg & Technology(Autonomous). 6


C PROGRAMMING LAB MANUAL

PROGRAM: Program to find greatest of 2 numbers


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b\n");
scanf("%d%d",&a,&b);
c=(a>b)?a:b;
printf("the biggest no is %d",c);
getch();
}

Result:

Enter the values of a,b


5
8
The biggest number is : 8

2(B). Write a program to find greatest among 3 numbers

AIM: Program to find greatest among 3 numbers

ALGORITHM:

Step 1: start

Step 2: input a,b,c

Step 3: if(a>b) &&(a>c)

Step 4: display a is greater

Step 5: else

Step 6: if(b>c)

Step 7: display b is greater

Step 8: else

Step 9: display c is greater

Step10: stop

Audisankara College of Engg & Technology(Autonomous). 7


C PROGRAMMING LAB MANUAL

FLOWCHART: To find greatest among 3 numbers

Start

Take a,b,c

If
(a>b)&&(a>c)

Display a

If (b>c)

Display b
Display c

Stop

Audisankara College of Engg & Technology(Autonomous). 8


C PROGRAMMING LAB MANUAL

PROGRAM: Program to find greatest among 3 numbers

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
clrscr();
printf("enter the values of a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("a is greatest of %d %d %d", a,b,c);
else
if(b>c)
printf("b is greatest of %d %d %d",a,b,c);
else
printf("c is greatest of %d %d %d",a,b,c);
getch();
}

Input & Output:

Enter the values of a,b and c

10

30

20

30 is greatest of 10 30 20

Audisankara College of Engg & Technology(Autonomous). 9


C PROGRAMMING LAB MANUAL

2(C). Write a Program to perform the arithmetic expression using switch


statement

AIM: Program to perform the arithmetic expression using switch statement

ALGORITHM:

Step1: start

Step2: input a,b

Step3: switch(result)

Step4: case 1: print num of a& b is a+b

Step5: case 2: print num of a& b is a-b

Step6: case 3: print num of a& b is a*b

Step7: case 4: print num of a& b is a/b

Step8: case 5: print num of a& b is a%b

Step9: default: invalid option

Step10: stop
PROGRAM: Program to perform the arithmetic expression using switch statement

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int op;
clrscr();
printf(" 1.addition\n 2.subtraction\n 3.multiplication\n 4.division\n");
printf("enter the values of a & b:");
scanf("%d%d",&a,&b);
printf("enter your choice : ");
scanf("%d",&op);
switch(op)
{
case 1:printf("sum of %d and %d=%d",a,b,a+b);
break;
case 2:printf("difference of %d and %d=%d",a,b,a-b);
break;
case 3:printf("multiplication of %d and %d=%d",a,b,a*b);
break;

Audisankara College of Engg & Technology(Autonomous). 10


C PROGRAMMING LAB MANUAL

case 4:printf("Divisionn of two numbers is %d=",a/b);


break;
default: printf(" Enter Your Correct Choice.");
break;
}
getch();
}

Input & Output:


1. Addition

2. Substraction

3. Multiplication

4. Division

Enter your choice : 1

Enter a and b values 10 20

Sum of 10 and 20 = 30

Audisankara College of Engg & Technology(Autonomous). 11


C PROGRAMMING LAB MANUAL

Exercise-3
3(A).Write a Program to find the factorial of a given number

AIM: Program to find the factorial of a given number

ALGORITHM:

Step1: start

Step2: input n,i,fact

Step3: fact=i=1

Step4: if(i<=n)

Step5: fact=fact*i

Step6: i=i+1

Step7: repeat from step5 to step6 till steps true

Step8: print fact

Step9: stop

Audisankara College of Engg & Technology(Autonomous). 12


C PROGRAMMING LAB MANUAL

FLOWCHART:

Program to find the factorial of a given number

Start

Take n

Fact=i=1

If
(i<=n)

fact=fact*i; i=i+1

Display fact

Stop

Audisankara College of Engg & Technology(Autonomous). 13


C PROGRAMMING LAB MANUAL

PROGRAM:

Program to find the factorial of a given number

// FACTORIAL(using FOR LOOP)

#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact=1,i=1;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
fact=fact*i;
}
printf("Factorial value of %d is :%d",n,fact);
getch();
}

//FACTORIAL(using WHILE LOOP)

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,n,fact=1;
clrscr();
printf("Enter a number: ");
scanf("%d",&n);
while(i<=n)
{
fact=fact*i;
i++;
}
printf("Factorial value of %d is :%d",n,fact);
getch();
}

Audisankara College of Engg & Technology(Autonomous). 14


C PROGRAMMING LAB MANUAL

// FACTORIAL (using DO WHILE LOOP)

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=1,fact=1;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
do
{
fact=fact*i;
i++;
}
while(i<=n);
printf("Factorial value of %d is :%d",n,fact);
getch();
}

Input & Output:

Enter a number :5

Factorial value of 5 is : 120

Audisankara College of Engg & Technology(Autonomous). 15


C PROGRAMMING LAB MANUAL

3(B). Write a program to generate all prime numbers up to nth number

AIM: Program to generate prime number till nth number

PROGRAM:

Program to generate prime number till nth number

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,r;
clrscr();
printf("enter the range:");
scanf("%d",&r);
for(i=2;i<=r;i++)
{
k=1;
for(j=2;j<=i-1;j++)
{
if(i%j==0)
{
k=0;
}
}
if(k!=0)
printf("%d\n",i);
}
getch();
}
Input & Output:

Enter the range: 10


Prime numbers are
2 3 5 7

Audisankara College of Engg & Technology(Autonomous). 16


C PROGRAMMING LAB MANUAL

3(C).Write a program to find total of first n natural numbers

AIM: Program to find sum of n natural numbers

Algorithm:

Step1: start
Step2: read n
Step3: i=0,sum=0
Step4: perform from step 5 to step 6 until i<=n
Step5: sum=sum+i;
Step6: i++;
Step7: write sum
Step8: stop

Flow chart: start

Read n

i=0;sum=0

F
While(i<=n)
T

Sum=sum+i;

i++

PRINT sum

stop

Audisankara College of Engg & Technology(Autonomous). 17


C PROGRAMMING LAB MANUAL

Program:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i=0,sum=0;
clrscr( );
printf("Enter Limit : ");
scanf("%d",&n);
while(i<=n)
{
sum=sum+i;
i++;
}
printf("Sum of %d natural numbers = %d",n,sum);
getch();
}

Result:
Enter Limit : 10
Sum of 10 natural numbers = 55
OR
Sum of N natural numbers. n*(n+1)/2

#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=0;
clrscr();
printf("Enter the n value:");
scanf("%d",&n);
sum=n*(n+1)/2;
printf("sum=%d",sum);
getch();
}
Result:
Input & output:
Enter the n value:5
Sum:=15

Audisankara College of Engg & Technology(Autonomous). 18


C PROGRAMMING LAB MANUAL

Exercise-4
4(A).Write a program to find the given number is Palindrome or not
PROGRAM // Given number is Palindrome or not
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,s=0,r;
clrscr();
printf("enter n value:");
scanf("%d",&n);
num=n;
while(n!=0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
if(s==num)
{
printf("%d is a palindrome number",num);
}
else
{
printf("%d is not a palindrome number",num);
}
getch();
}
Input & Output:
Enter n value : 121

121 is a Palindrome number.

Enter n value :123

123 is not a palindrome number.

Audisankara College of Engg & Technology(Autonomous). 19


C PROGRAMMING LAB MANUAL

4(B).Write a program to find the given number is Armstrong or not

PROGRAM // Given number is Armstrong number or not


#include<stdio.h>
#include<conio.h>
void main()
{
int r,n,s,num;
clrscr();
printf("enter n value: ");
scanf("%d",&n);
num=n;
s=0;
do {
r=n%10;
s=s+r*r*r;
n=n/10;
}
while(n!=0);

if(s==num)
{
printf("%d is an armstrong number\n",num);
}
else
{
printf("%d is not an armstrong number",num);
}
getch();
}
Input & Output:
Enter n value : 153

153 is an Armstrong number.

Audisankara College of Engg & Technology(Autonomous). 20


C PROGRAMMING LAB MANUAL

4(C).Write a program to find Roots of a Quadratic Equation

PROGRAM //ROOTS OF QUADRATIC EQUATION//

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c,d;
float x1,x2;
clrscr();
printf("Enter a,b,c values\n");
scanf("%d %d%d",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
printf("\nroots are imaginary");
else if(d>0)
{
printf("roots are real & un equal\n");
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("x1=%f\nx2=%f\n",x1,x2);
}
else
{
printf("\nroots are real & equal\n");
x1=x2=-b/(2*a);
printf("x1=%f\nx2=%f\n",x1,x2);
}
getch();
}
Result:
1.Enter a,b,c values: 1 2 3
Roots are imaginary.
2.Enter a,b,c values: 1 2 1
Roots are real & equal. x1=-1.000000 x2=-1.000000

Audisankara College of Engg & Technology(Autonomous). 21


C PROGRAMMING LAB MANUAL

4(D). Write a program to find the Summation of given Digits

PROGRAM // Summation of digits

#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,i,t;
long int n;
clrscr();
printf("Enter n value:");
scanf("%ld",&n);
do
{
sum=0;
do
{
t=n%10;
sum=sum+t;
n=n/10;
}while(n>0);
n=SUM;
}while(n>9);
printf("Sum=%d",sum);
getch();
}

Input & Output:

Enter n value:138

Sum=3

Audisankara College of Engg & Technology(Autonomous). 22


C PROGRAMMING LAB MANUAL

Exercise-5

PROGRAM
5. Write a program to find the Max and Min number by using functions.

#include<stdio.h>
#include<conio.h>
int big(int[],int);
int small(int[],int);
void main()
{
int a[10],i,n,max,min;
clrscr();
printf("Enter size of an array:");
scanf("%d",&n);
printf("Enter the elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
max=big(a,n);
min=small(a,n);
printf("max=%d",max);
printf("min=%d",min);
getch();
}

int big(int a[],int n)


{
int max,i;
max = a[0];
for(i=1;i<n;i++)
{
if(max<a[i])
max=a[i];
}
return max;
}

Audisankara College of Engg & Technology(Autonomous). 23


C PROGRAMMING LAB MANUAL

int small(int a[],int n)


{
int min,i;
min=a[0];
for(i=1;i<=n;i++)
{
if(min>a[i])
min=a[i];
}
return min;
}

Input & Output:

Enter size of an Array:6

Enter the elements: 55 98 47 21 64 34

Max=98
Min=21

Audisankara College of Engg & Technology(Autonomous). 24


C PROGRAMMING LAB MANUAL

Exercise-6

6(A).Swapping of two numbers by using call by value

PROGRAM

#include<stdio.h>
#include<conio.h>
void swap(int,int);
void main()
{
int x,y;
clrscr();
printf("enter the values of x, y:\n");
scanf("%d %d",&x,&y);
swap(x,y);
printf("in the main function\n");
printf("x=%d\ny=%d\n",x,y);
getch();
}
void swap (int a,int b)
{
int t;
printf("before swapping\n");
printf("x=a=%d\n",a);
printf("y=b=%d\n",b);
t=a;
a=b;
b=t;
printf("after swapping\n");
printf("x=a=%d\n",a);
printf("y=b=%d\n",b);
}

INPUT AND OUTPUT


Enter the values of x,y:
50
60
before swapping
x=a=50
y=b=60
after swapping

Audisankara College of Engg & Technology(Autonomous). 25


C PROGRAMMING LAB MANUAL

x=a=60
y=b=50
in the main function
x=50
y=60

6(B). Swapping of two numbers by using call by reference


#include<stdio.h>
#include<conio.h>
void swap(int *,int *);
void main()
{
int x,y;
clrscr();
printf("enter the values of x,y:\n");
scanf("%d %d",&x,&y);
swap(&x,&y);
printf("in the main function\n");
printf("x=%d\n",x);
printf("y=%d\n",y);
getch();
}
void swap (int *a,int *b)
{
int t;
printf("before swapping\n");
printf("x=a=%d\n",*a);
printf("y=b=%d\n",*b);
t=*a;
*a=*b;
*b=t;
printf("after swapping\n");
printf("x=a=%d\n",*a);
printf("y=b=%d\n",*b);
}

INPUT AND OUTPUT


Enter the values of x,y:
50
60
before swapping

Audisankara College of Engg & Technology(Autonomous). 26


C PROGRAMMING LAB MANUAL

x=a=50
y=b=60
after swapping
x=a=60
y=b=50
in the main function
x=60
y=50

Exercise-7

7(A).Write a program to find the Factorial number by using Recursion.

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int n,fact;
int fact1(int);
clrscr();
printf("Enter n value:");
scanf("%d",&n);
fact=fact1(n);
printf("Factorial value is:%d",fact);
getch();
}

int fact1(int n)
{
if(n==1 || n==0)
return(1);
else
return n*fact1(n-1);
}

Audisankara College of Engg & Technology(Autonomous). 27


C PROGRAMMING LAB MANUAL

Input & Output:

Enter n value:5

Factorial value is:120

7(B).Write a program to find the Fibonacci number by using Recursion

PROGRAM

#include<stdio.h>
#include<conio.h>
void main()
{
int n,f;
int fibo(int);
clrscr();
printf("Enter any number:");
scanf("%d",&n);
f=fibo(n);
printf("The %dth fibonoci number is:%d",n,f);
getch();
}

int fibo(int x)
{
if(x==1)
return(0);
else if(x==2)
return(1);
else
return(fibo(x-1)+fibo(x-2));
}

Audisankara College of Engg & Technology(Autonomous). 28


C PROGRAMMING LAB MANUAL

Input & Output:

Enter any number:8

The 5th Fibonacci number is :13

// Fibonacci series
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=1,n,c,i=2;
clrscr();
printf("enter how many fibonacci numbers u want :");
scanf("%d",&n);
printf("Fibonacci numbers are...");
printf("\n%d %d",a,b);
c=a+b;
do
{
printf("%5d",c);
a=b;
b=c;
c=a+b;
i++;
}while(i<n);
printf("\n");
getch();
}
INPUT AND OUTPUT
Enter how many fibonacci numbers u want :6
Fibonacci numbers are: 0 1 1 2 3 5
Enter how many fibonacci numbers u want :8
Fibonacci numbers are: 0 1 1 2 3 5 8 13
Result:

Audisankara College of Engg & Technology(Autonomous). 29


C PROGRAMMING LAB MANUAL

Exercise-8
8(A). Matrix Addition
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
clrscr();
printf("enter the 1st matrix size\n");
scanf("%d%d",&m,&n);
printf("enter the 2nd matrix size\n");
scanf("%d%d",&p,&q);
if(m!=p||n!=q)
{
printf("matrix Addition is not possible");
getch();
exit(0);
}
printf("enter 1st matrix elements\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter 2nd matrix elements\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
c[i][j]=0;
c[i][j]=a[i][j]+b[i][j];
}
printf("Matrix Addition is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
printf("%5d\t",c[i][j]);
}

Audisankara College of Engg & Technology(Autonomous). 30


C PROGRAMMING LAB MANUAL

}
getch();
}
INPUT AND OUTPUT
enter the 1st matrix size
2
2
enter the 2nd matrix size
2
2
enter 1st matrix elements
2
2
2
2
enter 2nd matrix elements
2
2
2
2
Matrix Addition is:
4 4
4 4

8(B). Matrix multiplication


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
clrscr();
printf("enter the 1st matrix size\n");
scanf("%d%d",&m,&n);
printf("enter the 2nd matrix size\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("matrix multiplication is not possible");
getch();
exit(0);
}

Audisankara College of Engg & Technology(Autonomous). 31


C PROGRAMMING LAB MANUAL

printf("enter 1st matrix elements\n");


for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("enter 2nd matrix elements\n");
for(i=0;i<p;i++)
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m;i++)
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]+=a[i][k]*b[k][j];
}
printf("Matrix Multiplication is:");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%5d\t",c[i][j]);
}
}
getch();
}
INPUT AND OUTPUT
Enter the 1st matrix size : 2 2
Enter the 2nd matrix size : 3 3
Matrix multiplication is not possible.
Case2:
Enter the 1st matrix size : 2 2
Enter the 2nd matrix size : 2 2
Enter the 1st matrix elements: 2 2 2 2
Enter the 2nd matrix elements: 2 2 2 2
Matrix multiplication is :
8 8
8 8

Audisankara College of Engg & Technology(Autonomous). 32


C PROGRAMMING LAB MANUAL

8(C). Transpose of a matrix

#include <stdio.h>
#include<conio.h>
void main()
{
int a[10][10];
int i, j, m, n;
clrscr();
printf("Enter the order of the matrix \n");
scanf("%d %d", &m, &n);
printf("Enter the elements of the matrix\n");
for(i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The given matrix is \n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
printf("Transpose of matrix is \n");
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
printf(" %d", a[i][j]);
}
printf("\n");
}
getch();
}

Audisankara College of Engg & Technology(Autonomous). 33


C PROGRAMMING LAB MANUAL

Input & output:


Enter the order of the matrix :3 3
Enter the elements of the matrix:
1 2 3 4 5 6 7 8 9
The given matrix is
1 2 3
4 5 6
7 8 9
Transpose of the matrix is:
1 4 7
2 5 8
3 6 9

Exercise-9

9(A). Write a program to sort 5 city names in alphabetical order

AIM: Program to read five cities and sort them and print sorted list of
citied in alphabetical order

ALGORITHM:

Step1:start
Step2:enter 5 city names

Step3:take I and j loop variables

For(i=65;i<122;i++)

for(j=0;j<5;j++)

if(city[j][0]==i)

printf(”\n%s”,city[j]);

Step4:stop

Audisankara College of Engg & Technology(Autonomous). 34


C PROGRAMMING LAB MANUAL

PROGRAM:

A program to read five cities and sort them and print sorted list of citied in
alphabetical order

#include<stdio.h>
#include<conio.h>
void main()
{
char city[5][20];
int i,j;
clrscr();
printf("enter the names of cities...\n");
for(i=0;i<5;i++)
scanf("%s",&city[i]);
printf("\nsorted list of cities...\n\n");
for(i=65;i<122;i++)
{
for(j=0;j<5;j++)
{
if(city[j][0]==i)
printf("\n%s",city[j]);
}
}
getch();
}

Result:

Enter the names of cities…


Hyd
Chennai
Bombay
goa
vizag

Sorted list of cities….


Bombay
Chennai
Goa
Hyd
vizag

Audisankara College of Engg & Technology(Autonomous). 35


C PROGRAMMING LAB MANUAL

9(B). String operations using built-in (pre-defined) functions


#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
int choice,i,j,l;
char s[50],s1[50],ch;
clrscr();
printf("Enter the String:");
gets(s);
do
{
printf("\n MENU");
printf("\n1.Length of the string");
printf("\n2.Reverse of a string");
printf("\n3.Copying of string");
printf("\n4.Comparison of string");
printf("\n5.Concatenation of string");
printf("\n6.Exit");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:printf(" The string length is %d",strlen(s));
break;
case 2:printf("Reverse of the string is %s",strrev(s));
break;
case 3:printf("Copied string is %s",strcpy(s1,s));
break;
case 4:printf("Enter the string to be compared:");
scanf("%s",s1);
if(strcmp(s,s1)==0)
printf("The given strings are equal");
else
printf("The given strings are unequal");
break;
case 5:printf("Enter the other string:");
scanf("%s",s1);
printf("The Concatenated string is %s",strcat(s,s1));
break;

Audisankara College of Engg & Technology(Autonomous). 36


C PROGRAMMING LAB MANUAL

case 6:exit(0);
default:printf("You Entered Wrong choice");
}
printf("\n Do you want to continue(y or n)");
ch=getch();
}
while(ch=='Y'|| ch=='y');
getch();
}

INPUT AND OUTPUT


Enter the String:RAJESH
MENU
1.Length of the string
2.Reverse of a string
3.Copying of string
4.Comparison of string
5.Concatenation of string
6.Exit
Enter your choice:1
The string length is :6

Exercise-10
10(A). // Print Address of a Variable

#include<stdio.h>
#include<conio.h>
void main()
{
int *m,n=20;
clrscr();
m=&n;
printf("Address of n is =%u\n",&n);
printf("value of n is =%d\n",*(&n));
printf("value of m is =%d\n",*m);
printf("address m is =%u\n",&m);
getch();
}

Audisankara College of Engg & Technology(Autonomous). 37


C PROGRAMMING LAB MANUAL

INPUT AND OUTPUT


Address of n is=65522
value of n is =20
value of m is =20
Address of m is=65524

10(B). // Print the element of array using pointers


#include<stdio.h>
#include<conio.h>
void main()
{
int a[]={20,30,40,50,60};
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("a[%d]=value of address %u=%d\n",i,&a[i],i[a]);
}
printf("Third element value is:%d",a[3]);
getch();
}

INPUT AND OUTPUT

a[0]=value of address 65516=20


a[1]=value of address 65518=30
a[2]=value of address 65520=40
a[3]=value of address 65522=50
a[4]=value of address 65524=60
Third element value is : 50

Audisankara College of Engg & Technology(Autonomous). 38


C PROGRAMMING LAB MANUAL

Exercise-11

11(A). /* EMPLOYEE RECORD USING STRUCTURES */

#include<stdio.h>
#include<conio.h>
struct emp
{
int dep;
char name[50];
int basic;
}a[10];
void main()
{
float da,hra,pf,sal;
int i,n;
clrscr();
printf("enter how many employees\n");
scanf("%d",&n);
printf("enter deptcode name and basic of employee..\n");
for(i=0;i<n;i++)
{
scanf("%d%s%d",&a[i].dep,a[i].name,&a[i].basic);
}
for(i=0;i<n;i++)
{
da=a[i].basic*0.20;
hra=a[i].basic*0.5;
pf=a[i].basic*0.10;
sal=a[i].basic+da+hra-pf;
printf("\ndept\t name basic \tda \t hra\t pf\t sal\n ");
printf("%d\t%s %d %f %f %f
%f\n",a[i].dep,a[i].name,a[i].basic,da,hra,pf,sal);
}
getch();
}

Audisankara College of Engg & Technology(Autonomous). 39


C PROGRAMMING LAB MANUAL

INPUT AND OUTPUT


Enter how many employees: 1
enter deptcode name and basic of employee..
10
rajesh
5000
dept name basic da hra pf sal
10 rajesh 5000 1000.000000 2500.000000 500.000000 8000

11(B). // passing Structure as an arguments

#include<stdio.h>
#include<conio.h>
struct student
{
char name[50];
int roll;
};
void Display(struct student stu);
void main()
{
struct student s1;
clrscr();
printf("Enter student's name: ");
scanf("%s",&s1.name);
printf("Enter roll number:");
scanf("%d",&s1.roll);
Display(s1); // passing structure variable s1 as argument
getch();
}
void Display(struct student stu){
printf("Output\nName: %s",stu.name);
printf("\nRoll: %d",stu.roll);
}

Audisankara College of Engg & Technology(Autonomous). 40


C PROGRAMMING LAB MANUAL

INPUT AND OUTPUT


Enter student's name:RAJESH
Enter roll number:123
OUTPUT:
Name:RAJESH
Roll:123

Exercise-12

12(A). // Write on Data File and Read From Data File


#include<stdio.h>
struct Student
{
int roll;
char name[12];
int percent;
} s1 = { 10, "RAJESH", 80 };
Void main()
{
FILE *fp;
struct Student s2;
//Write details of s1 to file
fp = fopen("ip.txt", "w");
fwrite(&s1, sizeof(s1), 1, fp);
fclose(fp);
fp = fopen("ip.txt", "r");
fread(&s2, sizeof(s2), 1, fp);
fclose(fp);
printf("\nRoll : %d", s2.roll);
printf("\nName : %s", s2.name);
printf("\nPercent : %d", s2.percent);
getch();
}
INPUT AND OUTPUT
Roll :10
Name:RAJESH
Percent:80

Audisankara College of Engg & Technology(Autonomous). 41


C PROGRAMMING LAB MANUAL

12(B). /* Program to copy the data one file in to another file */


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *fs,*ft;
char ch;
clrscr();
fs=fopen("raja.txt","r");
if(fs==NULL)
{
printf("file cannot open");
exit(0);
}
ft=fopen("raja1.txt","w");
while(1)
{
ch=fgetc(fs);
if(ch==EOF)
break;
fputc(ch,ft);
}
fclose(ft);
fclose(fs);
}

INPUT AND OUTPUT


Raja.txt
Hai how r u.

Raja1.txt
Hai how r u.

Audisankara College of Engg & Technology(Autonomous). 42


C PROGRAMMING LAB MANUAL

12(B). COMMAND LINE ARGUMENTS

Write a C Program to print Command line arguments

Aim: To design and develop a C Program to print Command line arguments

Source Code:

/*To Print Command line arguments*/


#include<stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
int i;
clrscr();
printf("Display Command line arguments\n");
printf("\n********************************\n");
for(i=0;i<argc;i++)
printf("argv[%d]=%s\n",i,argv[i]);
getch();
}
SYSTEM INPUT AND OUTPUT:
Display Command line arguments
argv[0]=C:\TC\COMMANDL.EXE
argv[1]=welcome
argv[2]=s
argv[3]=50
argv[4]=8.3

Audisankara College of Engg & Technology(Autonomous). 43

You might also like