Sei sulla pagina 1di 68

M S Ramaiah Institute of Technology, Bangalore

(Autonomous Institute Affiliated to VTU)

Fundamentals of Computing Lab Manual


for

I/II Semester
Subject Code: CSL101
Prepared By:
Pramod Sunagar

Reviewed By:
Jayalakshmi D. S.

Department of Computer Science and Engineering

List of Exercises for FOC Lab, Sep-Jan 2012


Sl.No

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.

1. Write a C program to find check whether the number is positive or negative.


2. Write a C program to find check whether the number is odd or even.
3. Write a C program to check whether the triangle is isosceles, equilateral or scalene using
if-else.
4. Write a program to check whether a given year is a leap year.
5. Write a C program to find the biggest of three numbers.

1. Write a C program to find grade based on percentage using else if statement.


2. Write a C program to perform a desired arithmetic operation using switch statement and
declaring choice as char data type.
3. Write a C program to find roots of a quadratic equation using switch.
4. Write a C program to find area of a triangle/square/circle/rectangle using switch statement.

Write a C program to find area of a rectangle.


Write a C program to find area and circumference of a circle by defining the value of PI.
Write a C program to swap 2 variables without using temporary variable.
Write a C program to find simple interest.
Write a C program to find area of a triangle, given its sides.

1. Write a C program to demonstrate use of Size of operator.


2. Write a C program to find whether a entered character is a number or digit using both
ASCII values and built-in functions.
3. Write a C program to convert a lower case alphabet to uppercase and vice-versa using both
ASCII values and built-in functions.
4. Write a C program to find sum of odd numbers and even numbers in first n natural
numbers
5. Write a C program to find factorial of a given number.

Dept. of Computer Science & Engineering, MSRIT

Page 2

Sl.No

Exercises

1.
2.
3.
4.
5.
6.

1. Write a C program to find sum of all elements of an array.


2. Write a C program to search for an element in array using linear search.
3. Write a C program to search for an element in array using binary search.

1.
2.
3.
4.

Write a C program to find GCD and LCM of two numbers.


Write a C program to find sum of digits of a given number
Write a C program to find whether a given number is palindrome or not.
Write a C program to print first N fibonacci numbers.
Write a C program to print prime numbers within given range.
Write a C program to to find sine series x - x^3/3! + x^5/5! - .... upto N terms accuracy.

Write a program to find length of string without using built-in functions.


Write a program to copy from one string to another without using built-in functions.
Write a program to combine two strings without using built-in functions.
Write a program to compare two strings without using built-in functions.

1. Write a program to find reverse of a string without using built-in functions.


2. Write a program to find whether given string is palindrome or not without using built-in
functions.
3. Write C Program with some built-in functions of a string.
4. Write a C program to add two matrices.

10

1. Write a C program to find trace and norm of a given matrix.


2. Write a C program to multiply two matrices.
3. Write a C program to sort elements in ascending order using bubble sort with functions.

11

1. Write a C program to multiply two matrices using functions.


2. Write a program to determine the length of a character string using pointers.
3. 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.

12

1.
2.
3.
4.

Write a program to illustrate use of pointers in expressions.


Write a program to compute sum of all elements stored in an array using pointers
Write a program to swap two numbers using pointers.
Write a program to illustrate function returning pointer.

Dept. of Computer Science & Engineering, MSRIT

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.

Dept. of Computer Science & Engineering, MSRIT

Page 4

5. Minimize: It appears as an icon in the taskbar.


6. It is important to note that the taskbar is always visible.
7. Title bar: The TITLE BAR appears at the top of the window and it contains the name of
the document followed by the name of the program. At the right hand corner of the bar
there are three buttons: Minimize, Maximize/Restore and Close button.
Entering text
1. It is done through the alpha numeric keys on the key board.
2. The text gets inserted to the left of where Insertion pointer is positioned on the screen.
The pointer keeps moving towards the right as more and more text is inserted. The
insertion pointer can be identified as a blinking I-beam shape on the document window.
If it is not blinking then no text can be inserted.
Saving a document
1. First decide a file name and location; that is, in which drive and/or folder you want to
save this file.
2. Select SAVE AS..from FILE menu and press ENTER.
3. When a existing document needs to be saved with a different name, different format or at
a different location then only SAVE AS.. can be used.
Editing text
1. In the above three paragraphs, some words and lines are been edited with the help of
editing functions that are available on the sub menu bar. They are:
Each new line of the paragraph is started from fresh line with leaving some space at
the prefix so that it will make easier to understand a new paragraph.
Two lines have been written in Italics(I) style with the help of Font dialogue box to
highlight the importance of C language.
Two words are been colored with the help of Font Color to highlight the important
languages used.
When you find some of the words underlined with zigzag green , it means some
spelling mistake, space or signs.
Some words are been in written in capital and made bold to highlight the differences
between the two different functions using Bold(B)s option from the Font dialogue
box.
To make more clear of the document or text Underlines are used using Underline(U)
option from the Font dialogue box.
The whole paragraph is aligned using various options from the Paragraph dialogue
box like; align text left, centre, right or both left &right margins to make the
document more neat and easy to read.
There are bullets and numbers used from the paragraph dialogue box to note some of
the important points and reduce the length of the paragraphs and make it easy to
remember.

Dept. of Computer Science & Engineering, MSRIT

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

Date: 16th Oct 2012

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.

Title of the book

01

Programming in ANSI C

02

Let Us C

03

Data Structures Using C

Details of the books


Author
Edition Publishers
E Balagurusamy
5th
Tata MC GrawHill Publishers
co. ltd.
Yashwanth
8th
PQR
Kanitkar
Publishers
Aaron M.
3rd
Pearson
Tenenbaum
Education, Inc. &
Yedidyah Langsam
Dorling
Moshe J.
Kindersley
Augenstein
Publishing Inc.

No. of
copies
10

10
10

Thanking you,
Yours Faithfully,
ABC

Dept. of Computer Science & Engineering, MSRIT

Page 6

Steps:

Open MS Word document.


Type the letter to be sent to the desired address using Tab button to write the
addresses.
Using Align Text Right option, write date.
Using Insert option from the main menu, insert a table with needed rows and columns
to fill the information.

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.

Open MS Excel from MS Office


A blank sheet is open with number of Rows(in numbers) & Columns(in alphabets)
All kind of Calculations can be performed by using appropriate formulas
Graphical presentation of the information can also be presented using Insert option
When you open an MS Excel page, you can find 3 sheets already displayed on the 1st
page.

Dept. of Computer Science & Engineering, MSRIT

Page 7

6. Here we have taken the example of a Marksheet of a student


7. The name & class of the student is mentioned in C4 &C5 ie, column C & row 4,5
8. Similarly the subjects & marks are displayed using B8,C8, D8 to B14,C14,D14
9. The total of Max marks is calculated using the formula =sum(C8:C15)
10. Similarly the total of Marks obtained is calculated using the formula =sum(D8:D15)
11. The percentage is calculated using the formula =sum(D17/C17)*100
12. The grade is obtained using the formula
=If(D18>90,"S",If(D18>80,"A",If(D18>70,"B",If(D18>60,"C",If(D18>50,"D","F")))))
13. When you hold the mouse on any of the calculations, you can find the formulas used to
calculate on display bar below the submenus
14. You can use the font color to highlight any main notes, rows or columns by selecting the
appropriate column name & row number.

d) Create a Power-point presentation with at least 6 slides.


Step 1: Go to start, then programs, and select Microsoft PowerPoint.
Step 2: Click File, then new
Step 3: Once you have a new presentation open, go to format and select slide layout. Select title
slide , click on it to apply it to your first slide.
Step 4: Click on the first block and type your name. Next click on the second block and type
Black Family Technology Day.
Step 5: Once you have finished entering information for your first slide. You are ready to add
animation to your text, or picture.
Step 6: Click on Box one. Next Click the slide show tab on the toolbar
Select custom animation.
Now click add effect, then click emphasis, and then click Grow/Shrink.
Next you want to check the setting of your animation. Look where it says modify
Grow/Shrink. Click the down arrow next to on click and select after previous.
Next go to slide show, and click view show to view your animation. Once you have
seen your animation and your slide show, right click, and select end show.
This takes you back to your work area.
Step 7: The next step is to add another slide to your show
On the toolbar click insert, and select new slide.
Once you select new slide, go to slide layout (right side of screen), select the title and
2text layout.
Go to the first box, type Workshops, now go to the second box and type Computer
Repair and hit enter , next type "Electronic Communication and hit enter,

Dept. of Computer Science & Engineering, MSRIT

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!

Dept. of Computer Science & Engineering, MSRIT

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();

Dept. of Computer Science & Engineering, MSRIT

Page 10

printf("Enter the length and breadth of a rectangle");


scanf("%d%d", &length,&breadth);
area=length * breadth;
printf("Area of the Rectangle : %d", area);
getch();
}

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

Dept. of Computer Science & Engineering, MSRIT

Page 11

6.5
Area of the Circle : 132.728
Circumference of the Circle : 40.839

c) Write a C program to swap 2 variables without using temporary variable.


Flowchart:

Program:
#include<stdio.h>
#include<conio.h>
void main()
{
Dept. of Computer Science & Engineering, MSRIT

Page 12

int a=0, b=0;


clrscr();
printf("Enter 2 numbers");
scanf("%d%d", &a,&b);
printf("Numbers before swapping\n");
printf("a : %d\n", a);
printf("b : %d\n", b);
a = a + b;
b = a - b;
a = a - b;
printf("Numbers after swapping\n");
printf("a : %d\n", a);
printf("b : %d\n", b);
getch();
}

Output:
Enter 2 numbers
15
6
Numbers before swapping
a : 15
b:6
Numbers after swapping
a:6
b : 15

d) Write a C program to find simple interest.


#include<stdio.h>
#include<conio.h>
void main()
{
float principle=0, time=0, rate=0;
float simple_interest=0;
clrscr();
printf("Enter principle amount");
scanf("%f", &principle);
printf("\nEnter time duration");

Dept. of Computer Science & Engineering, MSRIT

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

e) Write a C program to find area of a triangle, given its sides.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float side1=0, side2=0, side3=0;
float s=0,area=0;
clrscr();
printf("Enter 3 sides of a triangle\n");
scanf("%f%f%f", &side1,&side2,&side3);
s = (side1 + side2 + side3)/2;
area = sqrt(s*(s-side1)*(s-side2)*(s-side3));
printf("\nArea of Triangle : %f\n", area);
getch();
}

Dept. of Computer Science & Engineering, MSRIT

Page 14

Output:
Enter 3 sides of a triangle
20
13
24
Area of Triangle : 129.988

Dept. of Computer Science & Engineering, MSRIT

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

b) Write a C program to find check whether the number is odd or even.


#include<stdio.h>
#include<conio.h>
void main()
{
int number=0;
clrscr();
printf("Enter a number");
scanf("%d", &number);
if((number % 2) == 0)
printf("\nGiven number is a Even Number");
else
printf("\nGiven number is a Odd Number");
getch();
}

Output:
Enter a number
15
Given number is a Odd Number

Dept. of Computer Science & Engineering, MSRIT

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

d) Write a program to check whether a given year is a leap year.


#include<stdio.h>
#include<conio.h>
void main()
{
int year=0;
clrscr();
printf("Enter a Year");
scanf("%d",&year);

Dept. of Computer Science & Engineering, MSRIT

Page 18

if(((year%4 == 0)&& (year%100 != 0)) || (year%400 == 0))


printf("\n%d is a Leap Year", year);
else
printf("\n%d is not a Leap Year", year);
getch();
}

Output:
Enter a Year
2000
2000 is a Leap Year

e) Write a C program to find the biggest of three numbers.


Flowchart:

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

Page 20

Lab Exercise: 4
a) Write a C program to find grade based on percentage using else if statement.
Flowchart:

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

Page 22

b) Write a C program to perform a desired arithmetic operation using switch statement


and declaring choice as char data type.
Flowchart:

Dept. of Computer Science & Engineering, MSRIT

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:

Dept. of Computer Science & Engineering, MSRIT

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

c) Write a C program to find roots of a quadratic equation using switch.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a=0, b=0, c=0;
float D=0, root1=0, root2=0;
clrscr();
printf("Enter the values of a, b and c");
scanf("%d%d%d",&a,&b,&c);
D=((b*b)-(4*a*c));
switch(D>0)
{
case 1:
printf("\nRoots are Complex");
root1 = (-b)+(sqrt(D)/(2*a));
root2 = (-b)-(sqrt(D)/(2*a));
printf("\nRoot1 : %f", root1);
printf("\nRoot2 : %f", root2);
break;
case 0:
{
switch(D==0)

Dept. of Computer Science & Engineering, MSRIT

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

d) Write a C program to find area of a triangle/square/circle/rectangle using switch


statement.
#include<stdio.h>
#include<conio.h>
#define PI 3.147
void main()
{
float radius = 0, length = 0, breadth = 0;
float base = 0, height = 0, area = 0;
int choice = 0;
clrscr();

Dept. of Computer Science & Engineering, MSRIT

Page 26

printf("Enter \n1 to find area of triangle\n2 to find area of Square");


printf("\n3 to find area of circle\n4 to find area of rectangle\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter base & height of a triangle");
scanf("%f%f",&base,&height);
area = (1.0/2)*base*height;
printf("\nArea of Triangle : %f", area);
break;
case 2:
printf("\nEnter length of a Square");
scanf("%f",&length);
area = length * length;
printf("\nArea of Square : %f", area);
break;
case 3:
printf("\nEnter the radius of a Circle");
scanf("%f",&radius);
area = PI * radius * radius;
printf("\nArea of Circle : %f", area);
break;
case 4:
printf("\nEnter the length & breadth of a Rectangle");
scanf("%f%f",&length,&breadth);
area = length * breadth;
printf("\nArea of Rectangle : %f", area);
break;
default:
printf("\nInvalid Choice");
}
getch();
}

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

Dept. of Computer Science & Engineering, MSRIT

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();

Dept. of Computer Science & Engineering, MSRIT

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();
}

/* Program using Built-In Functions */


#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter a character\n");
scanf("%c",&ch);
if(isalpha(ch) > 0)
printf("\n%c is a character",ch);
else if(isdigit(ch) > 0)
printf("\n%c is a Number",ch);
else
printf("\nInvalid Character");
getch();
}

Output:
Enter a character
B
B is a character

Dept. of Computer Science & Engineering, MSRIT

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();
}

/* Program 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(islower(ch))

Dept. of Computer Science & Engineering, MSRIT

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);

Dept. of Computer Science & Engineering, MSRIT

Page 31

printf("\nSum of Odd Numbers from 1 to %d is : %d",N,OSum);


getch();
}

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

e) Write a C program to find factorial of a given number.


Flowchart:

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

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>

Dept. of Computer Science & Engineering, MSRIT

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

b) Write a C program to find sum of digits of a given number


#include<stdio.h>
#include<conio.h>
void main()
{
int i=0,n=0;
int sum=0;
clrscr();
printf("Enter a Number\n");
scanf("%d",&n);
i=n;

Dept. of Computer Science & Engineering, MSRIT

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

c) Write a C program to find whether a given number is palindrome or not.


#include<stdio.h>
#include<conio.h>
void main()
{
int rem=0,temp=0;
int number1=0, number2=0;
clrscr();
printf("Enter 4-Digit Number\n");
scanf("%d",&number1);
temp = number1;
while(temp)
{
rem = temp % 10;
number2 = (number2 * 10) + rem;
temp = temp / 10;
}
if(number1 == number2)
printf("\n%d is a Palindrome Number",number1);
else
printf("\n%d is not a palindrome number",number1);
getch();
}

Dept. of Computer Science & Engineering, MSRIT

Page 36

Output:
Enter 4-Digit Number
1882
1882 is a Palindrome Number

d) Write a C program to print first N fibonacci numbers.


Flowchart:

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

Page 38

e) Write a C program to print prime numbers within given range.


#include<stdio.h>
#include<conio.h>
void main()
{
int i=0;
int m=0, n=0;
clrscr();
printf("\nEnter the range :(m<n)\n");
scanf("%d%d", &m,&n);
printf("\nPrime Numbers between %d & %d :\n",m,n);
while(m<=n)
{
i=2;
while(i<=m)
{
if(m%i==0)
break;
i++;
}
if(i==m)
printf("%d\t",m);
m++;
}
getch();
}

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>

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

Page 41

b) Write a C program to search for an element in array using linear search.


#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20];
int i=0, size=0;
int key=0, flag=0;
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]);
printf("\nEnter the element to search in the array");
scanf("%d", &key);
for(i=0;i<size;i++)
{
if(key == arr[i])
{
flag = 1;
break;
}
}
if(flag == 1)
printf("\n%d is found at position %d",key,i+1);
else
printf("\nElement not found");
getch();
}
Output:
Enter the size of the array
6

Dept. of Computer Science & Engineering, MSRIT

Page 42

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

c) Write a C program to search for an element in array using binary search.


#include<stdio.h>
#include<conio.h>
void main()
{
int arr[20];
int i=0, size=0;
int key=0, flag=0;
int high=0, low=0, mid=0;
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]);
printf("\nEnter the element to search in the array\n");
scanf("%d", &key);
high = size - 1;
low = 0;
while(low<high)
{
mid = (high + low)/2;
if(key > arr[mid])
{
low = mid + 1;

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

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");

Dept. of Computer Science & Engineering, MSRIT

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

c) Write a program to combine two strings without using built-in functions.


#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20], str2[20];
char new_str[40];
int i;
clrscr();
printf("\nEnter the First String");
scanf("%s",str1);
printf("\nEnter the Second String");
scanf("%s",str2)
i=0;
while(str1[i]!='\0')
{
new_str[i]=str1[i];
i++;
}

Dept. of Computer Science & Engineering, MSRIT

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

d) Write a program to compare two strings without using built-in functions.


#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20], str2[20];
int i,j,flag=1;
clrscr();
printf("\nEnter the First String");
scanf("%s",str1);
printf("\nEnter the Second String");
scanf("%s",str2);
i=0;
j=0;
while(str1[i]!='\0' || str2[j]!='\0')
{
if(str1[i]!=str2[j])
{
flag=0;
break;

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

Page 50

c) Write C Program with some built-in functions of a string.


#include<stdio.h>
#include<conio.h>
void main()
{
char str[20], str1[20], str2[20], str3[20];
int i=0;
clrscr();
printf("\nEnter a string");
scanf("%s",str);
strcpy(str1,str);
printf("\nCopied string is : %s\n",str1);
printf("\nEnter a string");
scanf("%s",str2);
if((strcmp(str,str2))==0)
printf("\nStrings are equal\n");
else
printf("\nStrings are not equal\n");
i = strlen(str);
printf("\nLength of string %s is : %d\n",str,i);
printf("\nConcatenated String is : %s\n",strcat(str,str2));
getch();
}

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

Dept. of Computer Science & Engineering, MSRIT

Page 51

d) Write a C program to add two matrices.


#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10], b[10][10],c[10][10];
int i=0, j=0;
int rows=0, cols=0;
int rows1=0, cols1=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 First Matrix:\n");
for(i=0;i<rows;i++)
{
for(j=0;j<cols;j++)
{
scanf("%d",&a[i][j]);
}
}

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++)
{

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

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);

Dept. of Computer Science & Engineering, MSRIT

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

b) Write a C program to multiply two matrices.


#include<stdio.h>
#include<conio.h>
void main()
{
int m1[10][10],m2[10][10],mult[10][10];
int r1,c1,r2,c2;
int i=0, j=0, k=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++)

Dept. of Computer Science & Engineering, MSRIT

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();

Dept. of Computer Science & Engineering, MSRIT

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++)
{

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

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]);

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

Page 60

2 3 4 5
Enter the elements for the First matrix
5 4 3 2
Resultant Matrix:
19 14
35 26

b) Write a program to determine the length of a character string using pointers.


#include<stdio.h>
#include<conio.h>
void main()
{
int length=0;
char *p;
char str[20];
clrscr();
printf("\nEnter a string\n");
scanf("%s", str);
p = str;
while((*p)!='\0')
{
length++;
p++;
}
printf("\nLength of the %s is : %d", str,length);
getch();
}

Output:
Enter a string
MSRIT
Length of the string MSRIT is : 5

Dept. of Computer Science & Engineering, MSRIT

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();
}

Dept. of Computer Science & Engineering, MSRIT

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

Student with highest percentage is :


STU 102 91

Dept. of Computer Science & Engineering, MSRIT

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();

Dept. of Computer Science & Engineering, MSRIT

Page 64

printf("\nEnter the size of the array");


scanf("%d",&n);
printf("Enter the array elements\n");
while(i<n)
{
scanf("%d",&arr[i]);
i++;
}
p = arr;
for(i=0;i<n;i++)
{
sum = sum + (*p);
p++;
}
printf("\nSum : %d",sum);
getch();
}
Output:
Enter the size of the array
6
Enter the array elements
2 4 6 8 10 12
Sum : 42

c) Write a program to swap two numbers using pointers.


#include<stdio.h>
#include<conio.h>
void main()
{
int *p, *q;
int a=0, b=0;
int temp;
clrscr();
printf("\nEnter two numbers");
scanf("%d%d",&a,&b);
p = &a;
q = &b;

Dept. of Computer Science & Engineering, MSRIT

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

d) Write a program to illustrate function returning pointer.


Flowchart:

Dept. of Computer Science & Engineering, MSRIT

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();
}

int *largest(int *x, int *y, int *z)


{
if(*x > *y && *x > *z)

Dept. of Computer Science & Engineering, MSRIT

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

Dept. of Computer Science & Engineering, MSRIT

Page 68

Potrebbero piacerti anche