1 LAB MANUAL FOR PRACTICAL COURSES BCA DIRECTORATE OF DISTANCE & CONTINUING EDUCATION MANONMANIAM SUNDARANAR UNIVERSITY TIRUNELVELI - 627 012 DECEMBER 2020 / MAY 2021 PRACTICAL EXAMINATION
1
LAB MANUAL FOR PRACTICAL COURSES
BCA
DIRECTORATE OF DISTANCE & CONTINUING EDUCATION MANONMANIAM SUNDARANAR UNIVERSITY
TIRUNELVELI - 627 012
DECEMBER 2020 / MAY 2021 PRACTICAL EXAMINATION
2
DIRECTORATE OF DISTANCE & CONTINUING EDUCATION MANONMANIAM SUNDARANAR UNIVERSITY
Reaccredited with ‘A’ Grade (CGPA 3.13 out of 4.0) by NAAC (3rd Cycle) TIRUNELVELI – 627 012, Tamilnadu, India Department of Computer Science
This is to certify that this is the bonafide record work of practical work done by Mr./Ms. ________________________ Reg.No._________________________ of II BCA., Programming in C++, at the Directorate of Distance & Continuing Education, Manonmaniam Sundaranar University, Tirunelveli during the academic year ________________.
Internal Examiner External Examiner
3
Programming in C++ (DJAP3 - Practical III)
CONTENTS
Sl. No.
Title of the Exercise Page No. 1. Fibonacci Series 04 2. Prime Number 05 3. Palindrome number 07 4. Sum of Digits 09 5. Matrix Multiplication 11 6.
Decimal to Binary Conversion 14
4
Exercise Number 1: Fibonacci Series Aim: To write a C++ Program to print Fibonacci series without using recursion. Algorithm:
Step 1:- Start Step 2:- Use namespace std Step 3:- Define and assign variables n1=0, n2=1, n3, i, number Step 4:- Read input from the user to the variable ‘number’ using cin statement Step 5:- Use a for loop that starts from i = 2 with an increment upto the value of the variable number. Step 6:- Print the values Step 7:- Stop
Program:- #include <iostream> using namespace std; int main() { int n1=0,n2=1,n3,i,number; cout<<"Enter the number of elements: "; cin>>number; cout<<n1<<" "<<n2<<" "; //printing 0 and 1 for(i=2;i<number;++i) //loop starts from 2 because 0 and 1 are already printed { n3=n1+n2; cout<<n3<<" "; n1=n2; n2=n3; } return 0; } Output Enter the number of elements: 10 0 1 1 2 3 5 8 13 21 34
Result:
Thus the program was executed and its output was verified.
5
Exercise Number 2: Prime Number Aim: To write a C++ Program to check whether the number is prime number or not. Algorithm:
Step 1:- Start Step 2:- Use namespace std Step 3:- Define and assign variables n, i, m=0, flag=0 Step 4:- Read input from the user to the variable ‘n’ using cin statement Step 5:- Use a for loop that starts from i = 2 with increment less than or equal to the value of the variable m. Step 6:- Use if statement: if(n % i = = 0) Step 7:- Print the result Step 8:- Stop
Program:- #include <iostream> using namespace std; int main() { int n, i, m=0, flag=0; cout << "Enter the Number to check Prime: "; cin >> n; m=n/2; for(i = 2; i <= m; i++) { if(n % i == 0) { cout<<"Number is not Prime."<<endl; flag=1; break; } } if (flag==0)
6
cout << "Number is Prime."<<endl; return 0; }
Output
Enter the Number to check Prime: 17
Number is Prime. Enter the Number to check Prime: 57 Number is not Prime. Result:
Thus the program was executed and its output was verified.
7
Exercise Number 3: Palindrome number
Aim: To write a C++ Program to check whether the number is Palindrome number or not. Algorithm:
Step 1:- Start Step 2:- Use namespace std Step 3:- Define and assign variables n, r, sum=0, temp Step 4:- Read input from the user to the temporary variable ‘n’ using cin statement Step 5:- Use a while loop to Reverse the number Step 6:- Compare the temporary number with reversed number Step 7:- If both numbers are same, print palindrome number Step 8:- Else print not palindrome number Step 9:- Stop
Program:- #include <iostream> using namespace std; int main() { int n,r,sum=0,temp; cout<<"Enter the Number="; cin>>n; temp=n; while(n>0) { r=n%10; sum=(sum*10)+r; n=n/10; } if(temp==sum)
8
cout<<"Number is Palindrome."; else cout<<"Number is not Palindrome."; return 0; } Output
Enter the Number=121
Number is Palindrome.
Enter the number=113 Number is not Palindrome. Result:
Thus the program was executed and its output was verified.
9
Exercise Number 4: Sum of Digits Aim: To write a C++ Program to get sum of each digit. Algorithm:
Step 1:- Start Step 2:- Define and assign variables n, sum=0, m Step 3:- Read input from the user Step 4:- Use a while loop Step 5:- Get the modulus/remainder of the number Step 6:- sum the remainder of the number Step 7:- Divide the number by 10 Step 8:- Repeat the step 5 while number is greater than 0. Step 9:- Stop
Program:- #include <iostream> using namespace std; int main() { int n,sum=0,m; cout<<"Enter a number: "; cin>>n; while(n>0) { m=n%10; sum=sum+m; n=n/10; } cout<<"Sum is= "<<sum<<endl; return 0; }
10
Output
Enter a Number: 121
Sum is= 4
Enter a number: 624 Sum is= 12 Result:
Thus the program was executed and its output was verified.
11
Exercise Number 5: Matrix Multiplication
Aim: To write a C++ Program to print multiplication of 2 matrices. Algorithm:
Step 1:- Start Step 2:- Define variables a, b, mul, r, c, i, j, k Step 3:- Read input from the user for the number of rows and columns Step 4:- Read input from the user for the elements of the matrix Step 5:- Use nested for loop to find the product of the 2 matrices Step 6:- Display the resultant matrix Step 7:- Stop
Program:- #include <iostream>
using namespace std; int main() { int a[10][10],b[10][10],mul[10][10],r,c,i,j,k; cout<<"enter the number of row="; cin>>r; cout<<"enter the number of column="; cin>>c; cout<<"enter the first matrix element=\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>a[i][j]; } }
12
cout<<"enter the second matrix element=\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { cin>>b[i][j]; } } cout<<"multiplication of the matrix=\n"; for(i=0;i<r;i++) { for(j=0;j<c;j++) { mul[i][j]=0; for(k=0;k<c;k++) { mul[i][j]+=a[i][k]*b[k][j]; } } } //for printing result for(i=0;i<r;i++) { for(j=0;j<c;j++) { cout<<mul[i][j]<<" "; } cout<<"\n"; } return 0; }
13
Output enter the number of row=3 enter the number of column=3 enter the first matrix element= 1 2 3 1 2 3 1 2 3 enter the second matrix element= 1 1 1 2 1 2 3 2 1 Multiplication of the matrix= 14 9 8 14 9 8 14 9 8 Result:
Thus the program was executed and its output was verified.
14
Exercise Number 6: Decimal to Binary Conversion Aim: To write a C++ Program to convert decimal number to binary. Algorithm:
Step 1:- Start Step 2:- Define and assign variables n, a, i Step 3:- Read input from the user for the number to convert Step 4:- Use a for loop Step 5:- Divide the number by 2 through % (modulus operator) and store the remainder in array Step 6:- Divide the number by 2 through / (division operator) Step 7:- Repeat the step 6 until the number is greater than zero Step 8:- Print the result Step 9:- Stop
Program:- #include <iostream>
using namespace std; int main() { int a[10], n, i; cout<<"Enter the number to convert: "; cin>>n; for(i=0; n>0; i++) { a[i]=n%2; n= n/2; } cout<<"Binary of the given number= "; for(i=i-1 ;i>=0 ;i--) { cout<<a[i];