Sem 1 C-CPP Lab Excercise Manual PDF
Document Details
Uploaded by GratefulSwan3091
University of Papua New Guinea
Tags
Summary
This is a manual for C/C++ programming lab exercises, covering various topics and programming concepts. It includes a comprehensive index of the exercises and topics covered.
Full Transcript
C & C++ LAB EXERCISE MANUAL Version 1.2 Datec Learning Centres All rights rese...
C & C++ LAB EXERCISE MANUAL Version 1.2 Datec Learning Centres All rights reserved. No part of this document may be reproduced or used in any manner without permission of the owner INDEX C/C++ Exercises List Conditional Structures (if. Else and switch. Case) Basics 12. Comparing 2 numbers equal or not 1. Print Hello World 13. Greatest of 2 numbers 2. Print First Name, Last Name and Age 14. Greatest of 3 numbers using nested if method 3. Read First Name and Last Name, Print Full Name 15. Greatest of 3 numbers using "logical AND" 4. Sum of two/three numbers method 5. Product of two numbers 16. Odd / Even number Check 6. Arithmetic operations using two numbers 17. Print day of week using if condition 7. Square root of a number 18. Print day of week using switch 8. Swap two variables using temporary variable 19. Area of a circle, square and rectangle using if 9. Swap two variables without using temporary variable condition 10. Area of a rectangle 20. Area of a circle, square and rectangle using switch condition 11. Area and Circumference of a circle 21. Arithmetic Operations using switch condition Looping Structures (for, while, do. While loops) Pointers 22. Print hello world n times (for loop) 48. A program to print the memory address of variable 23. Print name n times (for loop) 49. Using Pointers to store and access a variable through its memory address 24. Print natural numbers up to limit in ascending/descending order (for loop) File Handling 25. Print odd/even numbers up to limit (for loop) 50. Creating a text file and writing data into it 26. Sum of natural/odd/even number series up to limit (for loop) 51. Read info from a text file 27. Print Factorial (for loop) Structures and Unions 28. Prime Number Check (for loop) 52.Creating a structure named Student and Store Name, Roll No and Marks 29. Print hello world n times (while loop) 53. Structure Arrays 30. Print name n times (while loop) 54. Size of a Union variable 31. Print natural numbers up to limit in ascending/descending order (while loop) 55. Disadvantage of Union over Structure 32. Print odd/even numbers up to limit (while loop) 56. Creating a Union variable to store an integer, float and string variable 33. Sum of natural/odd/even number series up to limit (while loop) C++ only 34. Print Factorial (while loop) 57. Adding 2, 3 numbers using function overloading 35. Prime Number Check (while loop) 58. Greatest of 2, 3 numbers using function overloading 36. Print hello world n times (do-while loop) 59. Area of a circle, rectangle and square using function 37. Print name n times (do-while loop) overloading 38. Print natural numbers up to limit (do-while loop) 60. Creating a Class Student to Read and Print Student info 39. Print odd/even numbers up to limit (do-while loop) 61. Creating a Class to read a value and print it. 40. Sum of natural/odd/even number series up to limit (do-while 62. Creating a Class to print the area of a rectangle loop) 63. Implementing Inheritance using a Subclass Arrays 64. Implementing constructors and destructors 41 Read and print integer array 65. Implementing Function Overriding 42 Sum and average of elements in array 43 Print an array in reverse order Functions 44. Add 2 numbers using function 45. Odd/Even check using function 46. Area of rect, circle and square using functions 47. Greatest of three numbers using functions EX.1: C PROGRAM TO PRINT HELLO WORLD. EX.1.C. -OUTPUT: PROGRAM TO PRINT HELLO WORLD. Ex. 2.C: PROGRAM TO PRINT YOUR FIRST AND LAST NAME AND AGE. EX.2. C. -OUTPUT: PRINTS YOUR FIRST AND LAST NAME AND AGE. EX.3.C:PROGRAM TO PRINT YOUR FULL NAME. EX.3.C. - OUTPUT:PROGRAM TO PRINT YOUR FULL NAME. EX.4.C: PROGRAM TO PRINT SUM OF TWO NUMBERS. EX.4.C.OUTPUT: PROGRAM PRINT THE SUM OF TWO NUMBERS. EX.5.C: PROGRAM TO PRINT PRODUCT OF TWO NUMBERS. EX.5. C. -OUTPUT: PROGRAM TO PRINT PRODUCT OF TWO NUMBERS. EX.6.C: ARITHMETIC OPERATIONS ON TWO FLOATING POINT NUMBERS. EX.6.C.- OUTPUT: ARITHMETIC OPERATIONS ON TWO FLOATING POINT NUMBERS. EX.7.C: SQUARE ROOT OF A NUMBER. EX.7.C. -OUTPUT: PROGRAM TO FIND A SQUARE ROOT OF THE GIVEN NUMBER EX.8.C:PROGRAM TO SWAP TWO VARIABLES USING TEMPORARY VARIABLE. EX.8.C.-OUTPUT: PROGRAM TO SWAP TWO VARIABLES USING TEMPORARY VARIABLE. EX.9.C:PROGRAM TO SWAP TWO VARIABLES WITHOUT USING TEMPORARY VARIABLE EX.9.C.OUTPUT:-PROGRAM TO SWAP TWO VARIABLES WITHOUT USING TEMPORARY VARIABLE. EX.10.C: PROGRAM TO PRINT THE AREA OF A RECTANGLE. EX.10. C. – OUTPUT: PROGRAM TO PRINT THE AREA OF A RECTANGLE. EX.11.C.- OUTPUT: A PROGRAM TO FIND THE CIRCUMSTANCE OF A CIRCLE. EX.11.C.OUTPUT: A PROGRAM TO FIND THE CIRCUMSTANCE OF A CIRCLE. EX.12. C: PROGRAM TO COMPARE TWO VALUES (EAUAL OR NOT). EX.12. C. -OUTPUT: PROGRAM TO COMPARE TWO VALUES (EAUAL OR NOT). EX.13.C: PROGRAM TO FIND GREATEST OF TWO NUMBERS EX.13. C. -OUTPUT: PROGRAM TO FIND GREATEST OF TWO NUMBERS. EX.14.C: PROGRAM TO FIND THE LARGEST OF THREE NUMBERS (NESTED IF). EX.14. C. OUTPUT: - PROGRAM TO FIND THE LARGEST OF THREE NUMBERS (NESTED IF). EX.15.C: PROGRAM TO FIND THE LARGEST OF THREE NUMBERS (LOGICAL-IF). EX.15.C. - OUTPUT: - PROGRAM TO FIND THE LARGEST OF THREE NUMBERS (LOGICAL-IF). EX.16.C.OUTPUT: PROGRAM TO FIND WHETHER THE GIVEN NUMBER IS ODD/EVEN. EX.16. C.-OUTPUT: PROGRAM TO FIND WHETHER THE GIVEN NUMBER IS ODD/EVEN. EX.17.C:PROGRAM TO PRINT A DAY OF A WEEK. EX.17.C. - OUTPUT: PROGRAM TO PRINT A DAY OF A WEEK. EX.18.C: PROGRAM TO PRINT A DAY OF WEEK (SWITCH CASE METHOD) EX.18.C-OUTPUT: PROGRAM TO PRINT A DAY OF WEEK (SWITCH CASE METHOD). EX.19.C: PROGRAM TO PRINT THE AREA OF A SQUARE,RECTANGLE OR CIRCLE. Continued on next page… EX.19.C -OUTPUT: PROGRAM TO PRINT THE AREA OF A SQUARE,RECTANGLE OR CIRCLE. EX.20.C: PRINTS THE AREA OF CIRCLE, RECTANGLE AND SQUARE USING SWITCH-CASE METHOD EX.20.C -OUTPUT: PRINTS THE AREA OF CIRCLE, RECTANGLE AND SQUARE USING SWITCH-CASE METHOD. EX.21.C: ARITHMETIC OPERATIONS USING ‘SWITCH CASE’ METHOD. EX.21-OUTPUT: ARITHMETIC OPERATIONS USING ‘SWITCH CASE’ METHOD. EX.22. C: PROGRAM TO PRINT HELLO WORLD n TIMES USING ‘FOR LOOP’. EX.22. C. - OUTPUT: PROGRAM TO PRINT HELLO WORLD n TIMES USING ‘FOR LOOP’. EX.23. C: PROGRAM THAT PRINT YOUR NAME n TIMES USING ‘FOR LOOP’. EX.23. C. – OUTPUT- PROGRAM THAT PRINT YOUR NAME n TIMES USING ‘FOR LOOP’. EX.24. C: PROGRAM TO PRINT THE NATURAL NUMBERS IN ASCENDING/DESCENDING ORDER. EX.24. C. -OUTPUT: PROGRAM TO PRINT THE NATURAL NUMBERS IN ASCENDING/DESCENDING ORDER. EX.25. C: PROGRAM TO PRINT ODD/EVEN NUMBERS UP TO LIMIT. EX.25. C. – OUTPUT: PROGRAM TO PRINT ODD/EVEN NUMBERS UP TO LIMIT. EX.26.C: PROGRAM TO PRINT THE SUM OF NATURAL/ODD/EVEN NUMBERS UP TO LIMIT. EX.26. C. OUTPUT: PROGRAM TO PRINT THE SUM OF NATURAL/ODD/EVEN NUMBERS UP TO LIMIT. EX.27. C: FACTORIAL OF A NUMBER. EX.27. C. OUTPUT – FACTORIAL OF A NUMBER. EX.28. C: A PROGRAM TO PRINT WHETHER THE GIVEN NUMNER IS PRIME/NOT. EX.28. C. – OUTPUT: A PROGRAM TO PRINT WHETHER THE GIVEN NUMNER IS PRIME/NOT. EX.29. C: PROGRAM TO PRINT YOUR NAME n TIMES USING ‘WHILE LOOP’ UP TO LIMIT. EX.29. C. -OUTPUT: PROGRAM TO PRINT YPUR NAME n TIMES USING ‘WHILE LOOP’ UP TO LIMIT. EX.30.C: PROGRAM TO PRINT HELLO WORLD “n “TIMES UPTO LIMIT USING ‘WHILE LOOP’. EX.30.C- OUTPUT: PROGRAM TO PRINT HELLO WORLD n TIMES UPTO LIMIT USING ‘WHILE LOOP’. EX.31. C: PROGRAM TO PRINT THE NATURAL NUMBERS IN ASCENDING/DESCENDING ORDER UPTO LIMIT USING ‘WHILE LOOP’. EX.31. C. – OUTPUT: PROGRAM TO PRINT THE NATURAL NUMBERS IN ASCENDING/DESCENDING ORDER UPTO LIMIT USING ‘WHILE LOOP’. EX.32.C: PROGRAM TO PRINT ODD/EVEN UP TO THE LIMIT USING ‘WHILE LOOP’. EX.32. C. – OUTPUT: PROGRAM TO PRINT ODD/EVEN UP TO THE LIMIT USING ‘WHILE LOOP’. EX.33. C: PROGRAM TO PRINT THE SUM OF NATURAL/ODD/EVEN NUMBERS UP TO LIMIT USING ‘WHILE LOOP’. EX.33. C. OUTPUT: PROGRAM TO PRINT THE SUM OF NATURAL/ODD/EVEN NUMBERS UP TO LIMIT USING ‘WHILE LOOP’. EX.34. C: FACTORIAL OF A NUMBER USING ‘WHILE LOOP’. EX.34. C: OUTPUT: FACTORIAL OF A NUMBER USING ‘WHILE LOOP’. EX.35. C: PROGRAM TO CHECK WHETHER THE GIVEN IS PRIME/NOT USING ‘WHILE LOOP’. EX.35. C.-OUTPUT: PROGRAM TO CHECK WHETHER THE GIVEN IS PRIME/NOT USING ‘WHILE LOOP’. EX.36. C: PROGRAM TO PRINT HELLO WORLD n TIMES USING ‘WHILE LOOP’. EX.36.C: OUTPUT: PROGRAM TO PRINT HELLO WORLD n TIMES USING ‘WHILE LOOP’. EX.37. C: PROGRAM TO PRINT YOUR NAME n TIMES USING ‘DO-WHILE LOOP’. EX.37. C. – OUTPUT: PROGRAM TO PRINT YOUR NAME n TIMES USING ‘DO-WHILE LOOP’. EX.38. C: PROGRAM TO PRINT NATURAL NUMBERS IN ASCENDING/DESCENDING ORDER UPTO LIMIT USING ‘DO-WHILE LOOP’. EX.38. C. – OUTPUT: PROGRAM TO PRINT NATURAL NUMBERS IN ASCENDING/DESCENDING ORDER TO THE LIMIT USING ‘DO-WHILE LOOP’. EX.39. C: PROGRAM TO PRINT ODD/EVEN NUMBERS UP TO LIMIT USING ‘DO-WHILE LOOP’. EX39.C: OUTPUT: PROGRAM TO PRINT ODD/EVEN NUMBERS UP TO LIMIT USING ‘DO-WHILE LOOP’. EX.40.C: PROGRAM TO FIND THE SUM OF NATURAL/EVEN/ODD NUMBERS UP TO LIMIT USING DO-WHILE LOOP. Continued on next page… EX.40.C-OUTPUT: PROGRAM TO FIND THE SUM OF NATURAL/EVEN/ODD NUMBERS UP TO LIMIT USING DO- WHILE LOOP. EX.41. C: PROGRAM TO READ AND PRINT AN INTEGER ARRAY. EX.41. C. – OUTPUT: PROGRAM TO READ AND PRINT AN INTEGER ARRAY. u EX.42. C: PROGRAM TO PRINT SUM AND AVERAGE OF AN INTEGER ARRAY. EX.42. C. – OUTPUT: PROGRAM TO PRINT SUM AND AVERAGE OF AN INTEGER ARRAY. EX.43. C: PROGRAM TO PRINT AN INTEGER ARRAY IN REVERSED ORDER. EX.43. C.-OUTPUT: PROGRAM TO PRINT AN INTEGER ARRAY IN REVERSE ORDER. EX.44A. PROGRAM TO ADDING TWO NUMBERS USING FUNCTION (TYPE 1). EX.44A.C. OUTPUT: PROGRAM TO ADDING TWO NUMBERS USING FUNCTION (TYPE 1). EX.44B.C. – PROGRAM TO ADDING TWO NUMBERS USING FUNCTION (TYPE 2). EX.44B.C: OUTPUT– PROGRAM TO ADDING TWO NUMBERS USING FUNCTION (TYPE 2). EX.44C.C.: PROGRAM TO ADD TWO NUMBERS USING FUNCTIONS (TYPE 3). EX.44C.C. – OUTPUT: PROGRAM TO ADD TWO NUMBERS USING FUNCTIONS (TYPE 3). EX.44D.C: PROGRAM TO ADD TWO NUMBERS USING FUNCTIONS (TYPE 4). EX.44D.C. – OUTPUT: PROGRAM TO ADD TWO NUMBERS USING FUNCTIONS (TYPE 4). EX.45. C: PROGRAM USING FUNCTIONS TO PERFORM ODD/EVEN NUMBER CHECK. EX.45. C. – OUTPUT: PROGRAM USING FUNCTIONS TO PERFORM ODD/EVEN NUMBER CHECK. EX.C.46: PROGRAM PRINTS THE AREA OF A CIRCLE/RECTANGLE OR SQUARE USING FUNCTIONS. EX.C.46. – OUTPUT: PROGRAM PRINTS THE AREA OF A CIRCLE/RECTANGLE OR SQUARE USING FUNCTIONS. EX.47.C: PROGRAM TO PRINT THE GREATEST OF THREE NUMBERS USING FUNCTIONS. EX.47. C. OUTPUT: PROGRAM TO PRINT THE GREATEST OF THREE NUMBERS USING FUNCTIONS. EX.48. C: PROGRAM TO PRINT THE MEMORY ADDRESS OF A VARIABLE. EX.48. C. – OUTPUT: PROGRAM TO PRINT THE MEMORY ADDRESS OF A VARIABLE. EX.49.C: USING POINTERS TO STORE AND ACCESS A VARIABLE THROUGH ITS MEMORY ADDRESS EX.49. C. OUTPUT: USING POINTERS TO STORE AND ACCESS A VARIABLE THROUGH ITS MEMORY ADDRESS. EX.50. C: CREATING A TEXT FILE AND WRITING DATA INTO IT. EX.50. C. – OUTPUT: CREATING A TEXT FILE AND WRITING DATA INTO IT. A text file will be created in C:\ TC Folder under the name sample.txt. View the specified file in notepad. EX.51.C: READ LINES FROM THE TEXT FILE ALREADY CREATED IN EX.50. EX.51. C. OUTPUT: READ LINES FROM THE TEXT FILE ALREADY CREATED IN EX.50 EX.52. C: CREATING A STRUCTURE NAMED STUDENT AND STORE NAMES, ROLL NUMBERS AND MARKS. EX.52. C. – OUTPUT: CREATING A STRUCTURE NAMED STUDENT AND STORE NAMES, ROLL NUMBERS AND MARKS. EX.53.C: STRUCTURED ARRAY. EX.53. C. OUTPUT: STRUCTURED ARRAY. EX.54. C: SIZE OF THE UNION VARIABLE EX.54. C. OUTPUT: SIZE OF THE UNION VARIABLE. EX.55. C: DISADVANTAGES OF UNION OVER STRUCTURES. EX.55. C. -OUTPUT: DISADVANTAGES OF UNION OVER STRUCTURES. EX.56. C: CREATING A UNION VARIABLE TO STORE AN INTEGER, FLOAT AND STRING VARIABLE. EX.56. C. OUTPUT: CREATING A UNION VARIABLE TO STORE AN INTEGER, FLOAT AND STRING VARIABLE. EX.57. CPP: ADDING 2,3 NUMBERS USING FUNCTION OVERLOADING. EX.57. CPP. OUTPUT: ADDING 2,3 NUMBERS USING FUNCTION OVERLOADING. EX.58.CPP: GREATEST 2, 3 NUMBERS USING FUNCTION OVERLOADING. EX.58.CPP-OUTPUT: GREATEST 2,3 NUMBERS USING FUNCTION OVERLOADING. EX.59.CPP: AREA OF CIRCLE, RECTANGLE AND SQUARE USING THE FUNCTION OVERLOADING EX.59.CPP.OUTPUT: - AREA OF CIRCLE, RECTANGLE AND SQUARE USING THE FUNCTION OVERLOADING. EX60.CPP: CREATING A CLASS TO READ THE VALUE AND PRINT IT. EX60.CPP: CREATING A CLASS TO READ THE VALUE AND PRINT IT. EX61.CPP: CREATING A CLASS TO READ THE VALUE AND PRINT IT. EX.61.CPP-OUTPUT: CREATING A CLASS TO READ THE VALUE AND PRINT IT. EX62.CPP: CREATING A CLASS TO PRINT THE ARE OF A RECTANGLE. EX.62.CPP.OUTPUT: CREATING A CLASS TO PRINT THE AREA OF A RECTANGLE. EX63.CPP: IMPLEMENTING INHERITANCE USING A SUBCLASS EX.63.CPP.OUTPUT: - IMPLEMENTING INHERITANCE USING A SUBCLASS. EX64.CPP: IMPLEMENTING CONSTRUCTORS AND DESTRUCTORS EX.64.CPP.OUTPUT: - IMPLEMENTING CONSTRUCTORS AND DESTRUCTORS EX65.CPP: IMPLEMENTING FUNCTION OVERRIDING EX.65.CPP.OUTPUT: - IMPLEMENTING FUNCTION OVERRIDING THE END