Fundamentals of Compu Fundamentals of Computing Lab Manual I ...

126 downloads 1476 Views 764KB Size Report
List of Exercises for FOC Lab, Sep-Jan 2012. Sl.No. Exercises. 1. 1. Create a document using a suitable word processing package, like MS Word, with at least.
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

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.

2

1. 2. 3. 4. 5.

3

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.

4

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.

5

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

6

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

7

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.

8

1. 2. 3. 4.

9

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 #include 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 #include #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 #include 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 #include 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 #include #include 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 #include 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 #include 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 #include 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 #include 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 #include 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 #include 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 #include 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 #include #include 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 #include #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 #include 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 #include 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='A' && ch=48 && 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 #include #include void main() { char ch; clrscr(); printf("Enter a character\n"); scanf("%c",&ch); if(ch>='a' && ch='A' && ch0) { 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 #include 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 #include 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=2) { printf("%d\t",a); printf("%d\t",b); for(i=0;i