Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
I/II Semester
Subject Code: CSL101
Prepared By:
Pramod Sunagar
Reviewed By:
Jayalakshmi D. S.
Exercises
1. Create a document using a suitable word processing package, like MS Word, with at least
three paragraphs and perform editing operations.
2. Create a formal letter using a suitable word processing package, like MS Word, to place a
purchase order for procurement of books. Use tables for list of books.
3. Create a Spreadsheet in MS-EXCEL and enter the marks of a student, calculate total and
print grade if the student has passed in all subjects.
4. Create a Power-point presentation with at least 6 slides.
1.
2.
3.
4.
5.
Page 2
Sl.No
Exercises
1.
2.
3.
4.
5.
6.
1.
2.
3.
4.
10
11
12
1.
2.
3.
4.
Page 3
Lab Exercise: 1
a) Create a document using a suitable word processing package, like MS Word, with at
least three paragraphs and perform editing operations.
The increasing popularity of C is probably due to its many desirable qualities. It is a robust
language whose rich set of built-in functions and operators can be used to write any complex
program. The C complier combines the capabilities of an assembly language with the features of
a high-level language and therefore it is well suited for writing both system software and
business packages.
Programs written in C are efficient and fast. This is due to its variety of data types and
powerful operators. It is many times faster than BASIC. There are 32 keywords in ANSI C and
its strength lies in its built-in functions.
C is highly portable. This means that C programs written for one computer can be run on
another with little or no modification. In fact, many of the C compliers available in the market
are written in C.
Features of C:
C language is well suited for structured programming.
C program is basically a collection of functions that are supported by the C library.
With the availability of a large number of functions, the programming task becomes
simple.
The modular structure makes program debugging, testing and maintenance easier.
Steps:
Opening Word
1. Click Start button, then go to programs submenu and then select Microsoft word. In some
computers Microsoft Word could be inside Microsoft Office sub menu as it is a part of
larger package called MS Office.
2. Press Enter, Word will open and you will get the message Microsoft Word Document 1
Edit.
The Word Screen
1. Like all applications in Windows the program window of MS Word contains the Title bar
at the top followed by the Menu bar, the toolbars, one or more toolbar, formatting bar, the
document window and the status bar at the bottom. There also could be ruler bar below
the formatting bar.
2. This complete program window can appear in any of three sizes on the screen
3. Restore: It covers 2/3 rd part of the screen
4. Maximize: It covers the complete screen except the area of the task bar.
Page 4
Page 5
Closing a document
1. Go to file menu and press ENTER on CLOSE to close the document which is active.
b) Create a formal letter using a suitable word processing package, like MS Word, to place
a purchase order for procurement of books. Use tables for list of books.
From,
M S Ramaiah Institute of Technology
Department of Computer Science & Engineering,
Bangalore 01
To,
The Manager
XYZ Book Depot,
West Patel Nagar,
New Delhi 110008
Sir,
Sub: Procurement of books.
I kindly request you to send the following books that are mentioned in the below
list to the above address at the earliest. The payment will be done as soon the delivery of the
books is received.
Sl. no.
01
Programming in ANSI C
02
Let Us C
03
No. of
copies
10
10
10
Thanking you,
Yours Faithfully,
ABC
Page 6
Steps:
c) Create a Spreadsheet in MS-EXCEL and enter the marks of a student, calculate total
and print grade if the student has passed in all subjects.
Steps:
1.
2.
3.
4.
5.
Page 7
Page 8
next type Internet Based Research and hit enter, next type Navigating the Internet
and hit enter,
next type Introduction to PowerPoint .
Step 8: Once you have finished entering information for your first slide. You are ready to add
animation to your text, or picture.
Step 9: Click the slide show tab on the toolbar, and select animation schemes.
Go to the right of your screen, push the down arrow in the box apply to selected
slides. Select fade in one by one.
Next go to slide show, and click view show to view your animation.
After the first slide appears select the page down button to get to the next slide,
Continue to push the page down button until you get to the end of the second slide.
Right click, and select end show. This takes you back to your work area.
Step 10: Once you have done this, go to slide show on your toolbar and select view show!
Page 9
Lab Exercise: 2
a) Write a C program to find area of a rectangle.
Flowchart:
Program:
/* Header Files */
#include<stdio.h>
#include<conio.h>
void main()
{
int length=0, breadth=0;
int area=0;
clrscr();
Page 10
Output:
Enter the length and breadth of a rectangle
4
5
Area of the Rectangle : 20
b) Write a C program to find area and circumference of a circle by defining the value of PI.
/* Header Files */
#include<stdio.h>
#include<conio.h>
#define PI 3.147
void main()
{
float radius=0;
float area=0, circumf=0;
clrscr();
printf("Enter the radius of the Circle");
scanf("%f", &radius);
area = PI * radius * radius;
circumf = 2 * PI * radius;
printf("\nArea of the Circle : %f\n", area);
printf("Circumference of the Circle : %f", circumf);
getch();
}
Output:
Enter the radius of the Circle
Page 11
6.5
Area of the Circle : 132.728
Circumference of the Circle : 40.839
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
Dept. of Computer Science & Engineering, MSRIT
Page 12
Output:
Enter 2 numbers
15
6
Numbers before swapping
a : 15
b:6
Numbers after swapping
a:6
b : 15
Page 13
scanf("%f", &time);
printf("\nEnter the rate of interest");
scanf("%f", &rate);
simple_interest = (principle * time * rate)/100;
printf("\nSimple Interest : %f\n", simple_interest);
getch();
}
Output:
Enter principle amout
1200
Enter time duration
12
Enter rate of interest
8
Simple Interest : 1152
Page 14
Output:
Enter 3 sides of a triangle
20
13
24
Area of Triangle : 129.988
Page 15
Lab Exercise: 3
a) Write a C program to find check whether the number is positive or negative.
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int number=0;
clrscr();
printf("Enter a number");
Dept. of Computer Science & Engineering, MSRIT
Page 16
scanf("%d", &number);
if(number >= 0)
printf("\nGiven number is Positive");
else
printf("\nGiven number is Negative");
getch();
}
Output:
Enter a number
15
Given number is Positive
Output:
Enter a number
15
Given number is a Odd Number
Page 17
c) Write a C program to check whether the triangle is isosceles, equilateral or scalene using
if-else.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0, b=0, c=0;
clrscr();
printf("Enter 3 sides of a triangle");
scanf("%d%d%d", &a,&b,&c);
if(a==b && b==c && a==c)
printf("\nTriangle is Equilateral Triangle");
else if((a==b || a==c) || (b==a || b==c) || (c==a || c==b))
printf("\nTriangle is Isosceles Triangle");
else
printf("\nTriangle is Scalene Triangle");
getch();
}
Output:
Enter 3 sides of a triangle
20
14
22
Triangle is Scalene Triangle
Page 18
Output:
Enter a Year
2000
2000 is a Leap Year
Page 19
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0, b=0, c=0;
clrscr();
printf("Enter 3 numbers");
scanf("%d%d%d", &a,&b,&c);
if(a>b)
{
if(a>c)
printf("\n%d is largest", a);
else
printf("\n%d is largest", c);
}
else
{
if(b>c)
printf("\n%d is largest", b);
else
printf("\n%d is largest", c);
}
getch();
}
Output:
Enter 3 numbers
8
9
7
9 is largest
Page 20
Lab Exercise: 4
a) Write a C program to find grade based on percentage using else if statement.
Flowchart:
Page 21
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
long int m1=0, m2=0, m3=0, m4=0, m5=0, m6=0;
long int perce=0;
clrscr();
printf("Enter the marks for 6 subjects");
scanf("%ld%ld%ld%ld%ld%ld",&m1,&m2,&m3,&m4,&m5,&m6);
perce = ((m1+m2+m3+m4+m5+m6)*100/600);
printf("\nPercentage : %ld\n",perce);
if(perce >= 90)
printf("\n S grade");
else if(perce >= 80)
printf("\n A grade");
else if(perce >= 70)
printf("\n B grade");
else if(perce >= 60)
printf("\n C grade");
else if(perce >= 50)
printf("\n D grade");
else
printf("\n Fail");
getch();
}
Output:
Enter marks for 6 subjects
75 85 88 89 82 80
Percentage: 83
A grade
Page 22
Page 23
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
float a=0, b=0, result=0;
char opcode;
clrscr();
printf("Enter 2 Numbers");
scanf("%f%f",&a,&b);
fflush(stdin);
printf("\nEnter + to add\n - to subtract\n * to multiply\n / to divide");
scanf("%c",&opcode);
switch(opcode)
{
case '+':
result = a+b;
printf("\nSum of %f & %f is : %f", a,b,result);
break;
case '-':
result = a-b;
printf("\nSubtraction of %f & %f is : %f", a,b,result);
break;
case '*':
result = a*b;
printf("\nMultiplication of %f & %f is : %f", a,b,result);
break;
case '/':
if(b==0)
{
printf("Divide By Zero Error\n");
exit(0);
}
else
{
result = a/b;
printf("\nDivision of %f & %f is : %f", a,b,result);
}
break;
default:
Page 24
printf("\nInvalid Operator");
}
getch();
}
Output:
Enter 2 numbers
8 10
Enter + to add - to subtract * to multiply / to divide
*
Multiplication of 8 & 10 is : 80
Page 25
{
case 1:
printf("Roots are Equal\n");
root1 = (-b)/(2*a);
printf("\nRoot : %f", root1);
break;
case 0:
printf("\nRoots are Imaginary");
break;
default:
printf("\nInvalid Value");
}
break;
}
default:
printf("\nInvalid Choice");
}
getch();
}
Output:
Enter the values of a, b & c
4 8 3
Roots are Complex
Root1: -7.50000
Root2: -8.50000
Page 26
Output:
Enter 1 to find area of triangle 2 to find area of Square
3 to find area of circle 4 to find area of rectangle
2
Enter length of a square
5
Area of Square : 25
Page 27
Lab Exercise: 5
a) Write a C program to demonstrate use of Size of operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
float b;
char c;
double d;
long int l;
clrscr();
printf("Size of Intereger Variable is : %d\n", sizeof(a));
printf("Size of Float Variable is : %d\n", sizeof(b));
printf("Size of Char Variable is : %d\n", sizeof(c));
printf("Size of Double Variable is : %d\n", sizeof(d));
printf("Size of Long Integer Variable is : %d\n",sizeof(l));
getch();
}
Output:
Size of Intereger Variable is : 2
Size of Float Variable is : 4
Size of Char Variable is : 1
Size of Double Variable is : 8
Size of Long Intereger Variable is : 4
b) Write a C program to find whether a entered character is a number or digit using both
ASCII values and built-in functions.
/* Program without using Built-In Functions */
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
Page 28
printf("Enter a character\n");
scanf("%c",&ch);
if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))
printf("\n%c is a character",ch);
else if(ch>=48 && ch<=56)
printf("\n%c is a Number",ch);
else
printf("\nInvalid Character");
getch();
}
Output:
Enter a character
B
B is a character
Page 29
c) Write a C program to convert a lower case alphabet to uppercase and vice-versa using
both ASCII values and built-in functions.
/* Program without using Built-In Functions */
#include<stdio.h>
#include<ctype.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character\n");
scanf("%c",&ch);
if(ch>='a' && ch<='z')
{
ch=ch-32;
printf("\nUpper Case : %c",ch);
}
else if(ch>='A' && ch<='Z')
{
ch = ch + 32;
printf("\nLower Case : %c",ch);
}
else
printf("\nInvalid Character");
getch();
}
Page 30
{
printf("\nUpper Case : %c",toupper(ch));
}
else if(isupper(ch))
{
printf("\nLower Case : %c",tolower(ch));
}
else
printf("\nInvalid Character");
getch();
}
Output:
Enter a character
S
Lower Case : s
d) Write a C program to find sum of odd numbers and even numbers in first n natural
numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int N=0;
int OSum=0, ESum=0;
int i=0;
clrscr();
printf("Enter the value of N\n");
scanf("%d",&N);
while(i<=N)
{
if((i%2)==0)
ESum=ESum+i;
else
OSum=OSum+i;
i++;
}
printf("\nSum of Even Numbers from 1 to %d is : %d",N,ESum);
Page 31
Output:
Enter the value of N
15
Sum of Even Numbers between 1 to 15 is : 56
Sum of Odd Numbers between 1 to 15 is : 64
Page 32
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int N=0;
int i=0;
long int fact=1;
clrscr();
printf("Enter a Number\n");
scanf("%d",&N);
i=N;
while(i>0)
{
fact=fact*i;
i--;
}
printf("\nFactorial of %d is : %ld",N,fact);
getch();
}
Output:
Enter a Number
8
Factorial of 8 is : 40320
Page 33
Lab Exercise: 6
a) Write a C program to find GCD and LCM of two numbers.
Flowchart:
Program:
#include<stdio.h>
#include<conio.h>
Page 34
void main()
{
int a=0, b=0, rem=0, lcm=0;
int temp1=0, temp2=0;
clrscr();
printf("Enter 2 numbers\n");
scanf("%d%d",&a,&b);
temp1=a;
temp2=b;
do
{
rem=a%b;
a=b;
b=rem;
}while(rem);
printf("\nGCD of %d and %d is : %d",temp1,temp2,a);
lcm = (temp1 * temp2)/a;
printf("\nLCM of %d and %d is : %d",temp1,temp2,lcm);
getch();
}
Output:
Enter 2 numbers
10 8
GCD of 10 and 8 is : 2
LCM of 10 and 8 is : 40
Page 35
while(i>0)
{
sum=sum+(i%10);
i=i/10;
}
printf("\nSum of digits of the number %d is : %d",n,sum);
getch();
}
Output:
Enter a Number
1882
Sum of digits of the number 1882 is : 19
Page 36
Output:
Enter 4-Digit Number
1882
1882 is a Palindrome Number
Page 37
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,n=0;
int a=0,b=1,fib=0;
clrscr();
printf("Enter a Number\n");
scanf("%d",&n);
if(n<=0)
{
printf("\nInvalid Input");
exit(0);
}
printf("\nFibonacci Series :\n\n");
if(n==1)
printf("%d\t",a);
else if(n>=2)
{
printf("%d\t",a);
printf("%d\t",b);
for(i=0;i<n-2;i++)
{
fib = a + b;
printf("%d\t",fib);
a = b;
b = fib;
}
}
printf("\n\nFibanocci Series Ends");
getch();
}
Output:
Enter a Number
10
Fibonacci Series :
0 1 1 2 3 5 8 13 21 34
Page 38
Output:
Enter the range (m<n)
10 30
Prime Numbers between 10 & 30 :
11
13
17
19
23
29
f) Write a C program to to find sine series x - x^3/3! + x^5/5! - .... upto N terms accuracy.
#include<stdio.h>
#include<math.h>
Page 39
#include<conio.h>
void main()
{
int i = 2, n, s = 1, x, pwr = 1, dr;
float nr = 1, x1, sum;
clrscr();
printf("\nEnter the Angle");
scanf("%d", &x);
x1 = 3.142 * (x/180.0);
sum = x1;
printf("\nEnter the number of Terms");
scanf("%d", &n);
while(i <= n)
{
pwr = pwr + 2;
dr = dr * pwr * (pwr - 1);
sum = sum + (nr / dr) * s;
s = s * (-1);
nr = nr * x1 * x1;
i+= 2;
}
printf("\nSum of the Sine series is : %f",sum);
getch();
}
Output:
Enter the Angle : 30
Enter the number of Terms : 4
Sum of the Sine series is : 0.523
Page 40
Lab Exercise: 7
a) Write a C program to find sum of all elements of an array.
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20];
int i=0, sum=0, size;
clrscr();
printf("Enter the size of the array");
scanf("%d",&size);
printf("\nEnter %d elements into Array",size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
printf("\nArray Elements are :\n");
for(i=0;i<size;i++)
printf("%d\t",arr[i]);
for(i=0;i<size;i++)
sum = sum + arr[i];
printf("\n\nSum of Array Elements : %d",sum);
getch();
}
Output:
Enter the size of the array
6
Enter 6 elements into Array
5 8 4 6 2 1
Array Elements are :
5 8 4 6 2 1
Sum of Array Elements : 26
Page 41
Page 42
Page 43
}
if(key < arr[mid])
{
high = mid - 1;
}
if(key == arr[mid])
{
flag = 1;
break;
}
}
if(flag == 1)
printf("\n%d is found at position %d",key,mid+1);
else
printf("\nElement not found");
getch();
}
Output:
Enter the size of the array
6
Enter 6 elements into Array
3 8 4 6 2 1
Array Elements are :
3 8 4 6 2 1
Enter the element to search in the array
2
2 is found at position 5
Page 44
Lab Exercise: 8
a) Write a program to find length of string without using built-in functions.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20], c;
int i;
clrscr();
printf("\nEnter the String");
scanf("%s",str);
printf("\n\nEntered string is %s\n",str);
for(i=0;c!='\0';++i)
{
c=str[i];
}
printf("\nLength of the entered string is : %d",i-1);
getch();
}
Output:
Enter the String
MSRIT
Entered string is MSRIT
Length of the entered string is : 5
b) Write a program to copy from one string to another without using built-in functions.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20], new_str[20];
int i;
clrscr();
printf("\nEnter the String");
Page 45
scanf("%s",str);
printf("\n\nEntered string is %s\n",str);
for(i=0;str[i]!='\0';++i)
{
new_str[i]=str[i];
}
new_str[i]='\0';
printf("\nCopied string is : %s",new_str);
getch();
}
Output:
Enter a string
QWERTY
Entered string is QWERTY
Copied string is : QWERTY
Page 46
while(str2[i]!='\0')
{
new_str[i]=str2[i];
i++;
}
new_str[i]='\0';
printf("\nNew String is : %s",new_str);
getch();
}
Output:
Enter the First String
ABC
Enter the Second String
XYZ
New String is : ABCXYZ
Page 47
}
i++;
j++;
}
if(flag==1)
{
printf("\nStrings are equal");
}
else
printf("\nStrings are not equal");
getch();
}
Output:
Enter the First String
ABCDEF
Enter the Second String
ABCDDF
Strings are not equal
Page 48
Lab Exercise: 9
a) Write a program to find reverse of a string without using built-in functions.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20], str1[20];
int i=0,j=0;
clrscr();
printf("\nEnter a string");
scanf("%s",str);
printf("\nEntered string is : %s\n",str);
while(str[i]!='\0')
{
i++;
}
i--;
while(i>=0)
{
str1[j]=str[i];
j++;
i--;
}
str1[j]='\0';
printf("\nReversed string is : %s\n",str1);
getch();
}
Output:
Enter a string
AVENGERS
Entered string is : AVENGERS
Reversed string is : SREGNEVA
Page 49
b) Write a program to find whether given string is palindrome or not without using built-in
functions.
#include<stdio.h>
#include<conio.h>
void main()
{
char str[20];
int i=0,j=0;
int flag=1;
clrscr();
printf("\nEnter a string");
scanf("%s",str);
printf("\nEntered string is : %s\n",str);
while(str[i]!='\0')
{
i++;
}
i--;
while(str[j]!='\0')
{
if(str[j]!=str[i])
{
flag=0;
break;
}
j++;
i--;
}
if(flag)
printf("\nEntered string is a Palindrome\n");
else
printf("\nEntered string is not a Palindrome\n");
getch();
}
Output:
Enter a string
GADAG
Entered string is : GADAG
Entered string is a Palindrome
Page 50
Output:
Enter a string
CSE
Copied string is : CSE
Enter a string
ISE
Strings are not equal
Length of the string CSE is : 3
Concatenated String is : CSEISE
Page 51
printf("\nEnter the number of rows & columns for the second matrix\n");
scanf("%d%d",&rows1,&cols1);
printf("\nEnter the elements for the Second Matrix:\n");
for(i=0;i<rows1;i++)
{
for(j=0;j<cols1;j++)
{
scanf("%d",&b[i][j]);
}
}
if(rows != rows1 || cols != cols1)
{
printf("\nAddition can not be performed\n");
exit(0);
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
Page 52
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\nResultant Matrix is :\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Enter the number of rows & columns for the first matrix
3 3
Enter the elements of the First Matrix
1 2 3 4 5 6 7 8 9
Enter the number of rows & columns for the second matrix
3 3
Enter the elements of the Second Matrix
9 8 7 6 5 4 3 2 1
Resultant Matrix:
10 10 10
10 10 10
10 10 10
Page 53
Lab Exercise: 10
a) Write a C program to find trace and norm of a given matrix.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a[10][10];
int i=0, j=0;
int rows=0, cols=0;
int trace=0, norm=0;
clrscr();
printf("Enter the number of rows & columns for the first matrix\n");
scanf("%d%d", &rows,&cols);
printf("\nEnter the elements for the Matrix:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nMatrix is :\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
if(i==j)
trace = trace + a[i][j];
norm += pow(a[i][j],2);
Page 54
}
}
printf("\nTrace of the Entered Matrix : %d", trace);
printf("\nNorm of the Entered Matrix : %d", norm);
getch();
}
Output:
Enter the number of rows & columns for the first matrix
3 3
Enter the elements for the Matrix
1 2 3 4 5 6 7 8 9
Matrix is :
1 2 3
4 5 6
7 8 9
Trace of the Entered Matrix : 15
Norm of the Entered Matrix : 285
Page 55
scanf("%d",&m1[i][j]);
}
printf("First Matrix :\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");
}
printf("Enter the elements for the Second matrix \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("Second Matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
printf("\n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
mult[i][j]=0;
for(k=0;k<r1;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];
}
}
printf("\n");
}
printf("\nResultant Matrix :\n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",mult[i][j]);
}
printf("\n");
}
getch();
Page 56
}
else
{
printf("Matrix multiplication cannot be done");
}
}
Output:
Enter number of rows and columns of First Matrix
2 2
Enter number of rows and columns of Second Matrix
2 2
Enter the elements for the First matrix
2 3 4 5
Enter the elements for the First matrix
5 4 3 2
Resultant Matrix:
19 14
35 26
c) Write a C program to sort elements in ascending order using bubble sort with functions.
#include<stdio.h>
#include<conio.h>
void main()
{
int size=0,arr[10];
int i=0, j=0, temp=0;
clrscr();
printf("\nEnter the size of the array\n");
scanf("%d",&size);
printf("\nEnter the %d elements into the array\n",size);
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
Page 57
if(arr[i]>arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\nSorted Array : \n");
for(i=0;i<size;i++)
{
printf("%d\t",arr[i]);
}
getch();
}
Output:
Enter the size of the array
6
Enter the 6 elements into the array
9 2 8 4 6 1
Sorted Array:
1 2 4 6 8 9
Page 58
Lab Exercise: 11
a) Write a C program to multiply two matrices using functions.
#include<stdio.h>
#include<conio.h>
void multiply_matrix(int [10][10], int [10][10], int [10][10], int, int);
void main()
{
int m1[10][10],m2[10][10],m3[10][10];
int r1,c1,r2,c2;
int i=0, j=0;
clrscr();
printf("Enter number of rows and columns of First Matrix\n");
scanf("%d%d",&r1,&c1);
printf("Enter number of rows and columns of Second Matrix\n");
scanf("%d%d",&r2,&c2);
if(r2==c1)
{
printf("Enter the elements for the First Matrix \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&m1[i][j]);
}
printf("First Matrix : \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%d\t",m1[i][j]);
printf("\n");
}
printf("Enter the elements for the Second Matrix \n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
scanf("%d",&m2[i][j]);
}
printf("Second Matrix :\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
printf("%d\t",m2[i][j]);
Page 59
printf("\n");
}
multiply_matrix(m1,m2,m3,r1,c2);
getch();
}
else
{
printf("Matrix multiplication cannot be done");
}
}
void multiply_matrix(int m1[10][10], int m2[10][10],int mult[10][10], int row, int col)
{
int i, j, k;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
mult[i][j]=0;
for(k=0;k<row;k++)
{
mult[i][j]+=m1[i][k]*m2[k][j];
}
//printf("%d\t",mult[i][j]);
}
}
printf("\nResultant Matrix :\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
printf("%d\t",mult[i][j]);
}
printf("\n");
}
}
Output:
Enter number of rows and columns of First Matrix
2 2
Enter number of rows and columns of Second Matrix
2 2
Enter the elements for the First matrix
Page 60
2 3 4 5
Enter the elements for the First matrix
5 4 3 2
Resultant Matrix:
19 14
35 26
Output:
Enter a string
MSRIT
Length of the string MSRIT is : 5
Page 61
c) Write a program to input N student information such as name, id, percentage. Display
the information of the student with the highest percentage using array of structures.
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
char name[20];
int id;
int perc;
}stud[10];
int i=0, n=0, index=0;
int high;
clrscr();
printf("\nEnter number of records to read\n");
scanf("%d",&n);
printf("\nN: %d\n",n);
for(i=1;i<=n;i++)
{
printf("\nEnter the name, id and percentage of %d student",i);
scanf("%s",stud[i].name);
scanf("%d",&stud[i].id);
scanf("%d",&stud[i].perc);
}
high = stud[0].perc;
index = 0;
for(i=0; i<n; i++)
{
if(high<stud[i].perc)
{
index = i;
high = stud[i].perc;
}
}
printf(\nStudent with highest percentage is :\n);
printf("\n\n%s\t%d\t%d\n",stud[index].name, stud[index].id, stud[index].perc);
getch();
}
Page 62
Output:
Enter the number of records to read
4
Enter the name, id and percentage of
ABC 100 88
Enter the name, id and percentage of
MNO 101 90
Enter the name, id and percentage of
STU 102 91
Enter the name, id and percentage of
XYZ 103 89
1 student
2 student
3 student
4 student
Page 63
Lab Exercise: 12
a) Write a program to illustrate use of pointers in expressions.
#include<stdio.h>
#include<conio.h>
void main()
{
int *p, *q;
int m, n;
int res, res1;
clrscr();
printf("\nEnter two numbers");
scanf("%d%d",&m,&n);
p = &m;
q = &n;
res = (m + n) * (m * n);
printf("Result (using numbers) : %d\n",res);
res1 = (*p + *q) * (*p * *q);
printf("Result (using pointers) : %d\n",res1);
getch();
}
Output:
Enter two numbers
4 6
Result (using numbers) : 240
Result (using pointers) : 240
b) Write a program to compute sum of all elements stored in an array using pointers
#include<stdio.h>
#include<conio.h>
void main()
{
int *p;
int arr[20],sum=0;
int i=0,n;
clrscr();
Page 64
Page 65
printf("\nBefore Swapping\n");
printf("a : %d\nb : %d\n",a,b);
temp = *p;
*p = *q;
*q = temp;
printf("\nAfter Swapping\n");
printf("a : %d\nb : %d\n",a,b);
getch();
}
Output:
Enter two numbers
10 20
Before Swapping
a : 10
b : 20
After Swapping
a : 20
b : 10
Page 66
Program:
#include<stdio.h>
#include<conio.h>
int *largest(int *, int *, int *);
void main()
{
int a, b, c;
int *p;
clrscr();
printf("\nEnter three numbers\n");
scanf("%d%d%d",&a,&b,&c);
p = largest(&a, &b, &c);
printf("\nLargest of three numbers is : %d", *p);
getch();
}
Page 67
return x;
else if(*y > *z)
return y;
else
return z;
}
Output:
Enter three numbers
5 10 8
Largest of three numbers is : 10
Page 68