Jadavpur PDF
Jadavpur PDF
ROLL NO. :
LAB: ……………………………………………………………………………………………….
NAME: …………………………………………….. ROLL: ………………….
YEAR …………………………………. SEMESTER …………………………………………….
INDEX
Exp. Date Name of the experiment Page Signature Remarks
No No.
1|Page
Programs using MATLAB:
Assignment 1:
Create 2 distinct matrix and perform addition, subtraction, multiplication, operation on it.
a=[1,2,3]
a=
1 2 3
b=[3 2 1]
b=
3 2 1
(Addition)
C= a+b
c=
4 4 4
(Subtraction)
D=a-b
D=
-2 0 2
(Multiplication)
l=7*a
l=
7 14 21
Assignment 2.
Create a matrix g=[2,5,7;8,1,0] and multiply the matrix with a scalar.
g= [2 5 7; 8 1 0]
g=
2 5 7
8 1 0
s=2*g
s=
4 10 14
16 2 0
2|Page
Assignment 3.
Find out transpose of a matrix.
p=s'
p=
4 16
10 2
14 0
Assignment 4.
matrix multiplication using 'mtimes' command.
1 2 3
4 5 6
7 8 9
4 5 6
7 5 3
9 5 1
mtimes(A,B)
ans =
45 30 15
105 75 45
165 120 75
Assignment 5.
Find out inverse and rank of a matrix.
v= [1 2 3; 4 5 6;7 8 9];
q=inv(v)
q=
1.0e+16 *
k=rank (v)
3|Page
k=
Assignment 6.
Create and plot a matrix using plot, grid on, hold on commands.
plot (v);
Output:
grid on;
Output:
4|Page
Hold on;
Output:
Assignment 7.
Plot a 2-dimentional matrix using 'surf' command.
Output:
5|Page
Assignment 8.
Plot a 2-dimentional matrix using ' contourf ' command
contourf(g);
Output:
Assignment 9.
Have a matrix a=[1,2,3,4;1,2,3,4;1,2,3,4]. From it print b=[1 1 1;2 2 2;3 3 3;4 4 4] and c=[1 2 3 4;1
2 3 4;1 2 3 4] using for loop.
a=[1 2 3 4; 1 2 3 4;1 2 3 4]
a=
1 2 3 4
1 2 3 4
1 2 3 4
for j=1:1:4
for i=1:1:3
b(j,i)=a(i,j);
end
end
for i=1:1:3
for j=1:1:4
c(i,j)=a(i,j);
end
end;
6|Page
Output:
b=
1 1 1
2 2 2
3 3 3
4 4 4
c=
1 2 3 4
1 2 3 4
1 2 3 4
Assignment 10.
Have a matrix a=[1,2,3,4;1,2,3,4;1,2,3,4]. From it print b=[1 1 1;2 2 2;3 3 3;4 4 4] and c=[1 2 3 4;1
2 3 4;1 2 3 4] using " reshape" command.
a=[1,2,3,4;1,2,3,4;1,2,3,4]
a=
1 2 3 4
1 2 3 4
1 2 3 4
c=reshape(a,3,4)
c=
1 2 3 4
1 2 3 4
1 2 3 4
b=c.'
b=
1 1 1
2 2 2
3 3 3
4 4 4
7|Page
Assignment 11.
Have a matrix g=[1 2 3 4]. From it print [1 2 3 4 1 2 3 4 1 2 3 4] and [1 2 3 4;1 2 3 4;1 2 3 4] using
"repmat"command.
g=[1 2 3 4]
g=
1 2 3 4
repmat (g,1,3)
ans =
1 2 3 4 1 2 3 4 1 2 3 4
repmat (g,3,1)
ans =
1 2 3 4
1 2 3 4
1 2 3 4
Assignment 12.
Print [1 1 1;2 2 2;3 3 3;4 4 4] from previous matrix 'g'
g=[1 2 3 4];
S=repmat (g,3,1)
S=
1 2 3 4
1 2 3 4
1 2 3 4
f=S.'
f=
1 1 1
2 2 2
3 3 3
4 4 4
8|Page
Programs using C language:
Program 1:
Find a given number is odd or even.
Coding:
#include<stdio.h>
#include<conio.h>
Int main ()
{
int a,b;
printf("Enter a integer: \n");
scanf(“%d”,&a);
b=a%2;
if(b==0)
printf (“The number is even. \n”);
else
printf (“The Number is odd. \n”);
}
Output:
Program 2:
Swap two variable without using third variable.
Coding:
#include<stdio.h>
#include<conio.h>
Int main()
{
int a,b;
printf("Enter two integer: ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("After swapping a= %d, b= %d",a,b);
9|Page
getch();
return 0;
}
Output:
Program3:
Print the Fibonacci series.
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int a=0,b=1,s=0,n,i;
printf("Enter the value of n: ");
scanf("%d",&n);
printf("%d %d",a,b);
for(i=0;i<=n;i++)
{
s=a+b;
printf(" %d ",s);
a=b;
b=s;
}
}
Output:
10 | P a g e
Program 4:
Print the pattern
*
**
***
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
Output:
11 | P a g e
Program 5:
Print the pattern
*
**
***
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k;
for(i=0;i<3;i++)
{
for(k=i;k<2;k++)
{
printf(" ");
}
for(j=0;j<=i;j++)
{
printf("*");
}
printf("\n");
}
}
Output:
12 | P a g e
Program 6:
Perform Post increment and Pre increment in c :
Post increment:
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int i;
for(i=0;i<5;i++)
{
printf("%d ",i++);
}
}
Output:
Pre increment:
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int i;
for(i=0;i<5;i++)
{
printf("%d ",++i);
}
}
13 | P a g e
Output:
Program 7:
Find the Sum of vowels from a given string using for loop.
Coding
#include<stdio.h>
#include<conio.h>
main()
{
char c[40],A,a;
int s=0,i=0;
printf("Enter the word: ");
gets(c);
for(i=0;c[i]!='\0';i++)
{
if(c[i]=='a'||c[i]=='A'||c[i]=='e'||c[i]=='E'||c[i]=='i'||c[i]=='I'||c[i]=='o'||c[i]=='O'||c[i]=='u'||c[i]
=='U')
{
s=s+1;
}
}
printf("Number of vowels: %d",s);
}
Output:
14 | P a g e
Program 8.
Find the Sum of vowels in a given string using while loop :
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
char c[40],A,a;
int s=0,i=0;
printf("Enter the word: ");
gets(c);
while(c[i]!='\0')
{
if(c[i]=='a'||c[i]=='A'||c[i]=='e'||c[i]=='E'||c[i]=='i'||c[i]=='I'||c[i]=='o'||c[i]=='O'||c[i]=='u'||c[i]=='U
')
{
s=s+1;
}
i++;
}
printf("Number of vowels: %d",s);
}
Output:
15 | P a g e
Program 9.
Count a and b in a given string:
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
char c[40],A,a,b,B;
int s=0,i=0,sum=0;
printf("Enter the word: ");
gets(c);
while(c[i]!='\0')
{
if(c[i]=='a'||c[i]=='A')
{
s=s+1;
}
i++;
}
printf("Number of a: %d\n",s);
i=0;
while(c[i]!='\0')
{
if(c[i]=='b'||c[i]=='B')
{
sum=sum+1;
}
i++;}
printf("Number of b: %d",sum);
}
Output:
16 | P a g e
Program 10.
Programs using pointers:
I)Print the values of pointer by using an array.
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int a[4],i,*pa;
printf("\n enter the values of the array \n");
for (i=0;i<4;i++)
{
scanf("%d",&a[i]);
}
pa=a;
printf("\n the value of pointer:%d",*pa);
}
Output:
17 | P a g e
II) Print the values of pointers by using a string.
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int i;
char *pa,a[4];
printf("\n enter the characters \n");
scanf("%s",a);
pa=a;
for (i=0;a[i]!='\0';i++)
{
printf("%c\n",*(pa+i));
}
}
Output:
18 | P a g e
III) Print the values of pointer by using an array.
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int i,*pa,a[4];
printf("\n enter the values of array \n");
for (i=0;i<4;i++)
{
scanf("%d",&a[i]);
}
pa=a;
for (i=0;i<4;i++)
{
printf("\n The value of the pointer:%d",*(pa+i));
}
}
Output:
19 | P a g e
IV) Stores the address of a variable
Coding:
#include<stdio.h>
#include<conio.h>
main()
{
int v,*pv;
v=10;
pv=&v;
printf("\n value of variable v=%d",*pv);
printf("\n Address of variable=%x",pv);
*pv=20;
printf("\n value of variable v=%d",*pv);
getch();
}
Output:
20 | P a g e