Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

BCA I Programming Methodology practicals

The document outlines practical programming exercises for first-year BCA students focusing on programming methodologies and data structures. It includes tasks such as creating a pyramid structure, checking for prime numbers, generating even/odd series, and performing matrix multiplication, along with corresponding C++ code examples. Each exercise is designed to enhance students' understanding of fundamental programming concepts and techniques.

Uploaded by

pandyakeshav900
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

BCA I Programming Methodology practicals

The document outlines practical programming exercises for first-year BCA students focusing on programming methodologies and data structures. It includes tasks such as creating a pyramid structure, checking for prime numbers, generating even/odd series, and performing matrix multiplication, along with corresponding C++ code examples. Each exercise is designed to enhance students' understanding of fundamental programming concepts and techniques.

Uploaded by

pandyakeshav900
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

BCA- I YEAR

PROGRAMMING METHODOLOGIES & Data STRUCTURES

Subject Incharge: Submitted By:


Prof. Nirmala Dabi
RollNo:
INDEX

S.No. Practicals
1 Write a programme to create a pyramid structure.
2 Write a programme to check whether a given
number is prime or not.
3 Write a programme to generate even/odd series
from 1 tom 100 using conditional statements.
4 Write a programme for call by value and call by
reference.
5 Write a programme to print table of any number.
6 Write a programme to find factorial of a given
number.
7 Write a programme to print Fibonacci series.
8 Write a programme to check whether a given
number even or odd.
9 Write a programme to swap the contents of two
variables.
10 Write a programme to perform multiplication of
two matrices.
1.Write a programme to create a pyramid
structure.
#include<iostream.h>
include<conio.h>
int main()
{
int i, space, j;
for(i=1; i<=6; i++)
{
for(space=6; space>i; space--)
cout<<" ";
for(j=0; j<i; j++)
cout<<"* ";
cout<<endl;
}
cout<<endl;
return 0;
}

With this C++ code, the pyramid looks like this:


2.Write a programme to check whether a
given number is prime or not.
1. #include <iostream.h>
2. #include <conio.h>
3.
4. int main()
5. {
6. int n, i, m=0, flag=0;
7. cout << "Enter the Number to check Prime: ";
8. cin >> n;
9. m=n/2;
10. for(i = 2; i <= m; i++)
11. {
12. if(n % i == 0)
13. {
14. cout<<"Number is not Prime."<<endl;
15. flag=1;
16. break;
17. }
18. }
19. if (flag==0)
20. cout << "Number is Prime."<<endl;
21. return 0;
22. }

Output:

Enter the Number to check Prime: 17


Number is Prime.
Enter the Number to check Prime: 57
Number is not Prim
3.Write a programme to generate
even/odd series from 1 tom 100 using
conditional statements.

#include <iostream>
using namespace std;
int main(){
int i;
cout << "Odd numbers between 1 to 100 are: " << endl;
for (i = 1; i <= 100; i++){
if (i % 2 != 0)
cout << i << " ";
}
cout << "Even numbers between 1 to 100 are: " << endl;

for(i = 1; i < = 100; i++){

/* If number is divisible by 2. */

if(i % 2 == 0) {

cout << i <<" ";


}

return 0;
}

Output

Odd numbers between 1 to 100 are:


1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53
55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99

Even numbers between 1 to 100 are:


2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40
42 44 46 48 50 52 54 56 58 60 62 64 66 68 70
72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
4.Write a programme for call by value and call by
reference.
concept of call by value in C++ language by the example given below:

1. #include <iostream>
2. using namespace std;
3. void change(int data);
4. int main()
5. {
6. int data = 3;
7. change(data);
8. cout << "Value of the data is: " << data<< endl;
9. return 0;
10. }
11. void change(int data)
12. {
13. data = 5;
14. }

Output:

Value of the data is: 3

concept of call by reference in C++ language by the example given below:

1. #include<iostream>
2. using namespace std;
3. void swap(int *x, int *y)
4. {
5. int swap;
6. swap=*x;
7. *x=*y;
8. *y=swap;
9. }
10. int main()
11. {
12. int x=500, y=100;
13. swap(&x, &y); // passing value to function
14. cout<<"Value of x is: "<<x<<endl;
15. cout<<"Value of y is: "<<y<<endl;
16. return 0;
17. }

Output:

Value of x is: 100


Value of y is: 500
5.Write a programme to print table of any number.
#include<iostream>

using namespace std;

int main()

int i,n;

cout<<"Enter any number:";

cin>>n;

for(i=1;i<=10;++i)

cout<<"\n"<<n<<" * "<<i<<" = "<<n*i;

return 0;

Output

Enter any number:6

6 * 1=6
6 * 2 = 12
6 * 3 = 18
6 * 4 = 24
6 * 5 = 30
6 * 6 = 36
6 * 7 = 42
6 * 8 = 48
6 * 9 = 54
6 * 10 = 60
6.Write a programme to find factorial of a given
number.

#include <iostream>
using namespace std;

int main() {
int n;
long factorial = 1.0;

cout << "Enter a positive integer: ";


cin >> n;

if (n < 0)
cout << "Error! Factorial of a negative number doesn't exist.";
else {
for(int i = 1; i <= n; ++i) {
factorial *= i;
}
cout << "Factorial of " << n << " = " << factorial;
}

return 0;
}
Run Code

Output

Enter a positive integer: 4


Factorial of 4 = 24
7.Write a programme to print Fibonacci series.

#include <iostream>
using namespace std;

int main() {
int t1 = 0, t2 = 1, nextTerm = 0, n;

cout << "Enter a positive number: ";


cin >> n;

// displays the first two terms which is always 0 and 1


cout << "Fibonacci Series: " << t1 << ", " << t2 << ", ";

nextTerm = t1 + t2;

while(nextTerm <= n) {
cout << nextTerm << ", ";
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}

Output

Enter a positive integer: 100


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
8 Write a programme to check whether a given number
even or odd.

#include <iostream>
using namespace std;

int main()
{
int num;
cout<<"Enter an integer number: ";
cin>>num;

if(num%2==0)
cout<<num<<" is an EVEN number."<<endl;
else
cout<<num<<" is an ODD number."<<endl;

return 0;
}

Output
First run:
Enter an integer number: 10
10 is an EVEN number.

Second run:
Enter an integer number: 11
11 s an ODD number.
9.Write a programme to swap the contents of two
variables.
1. #include <iostream>
2. using namespace std;
3. int main()
4. {
5. int a=5, b=10;
6. cout<<"Before swap a= "<<a<<" b= "<<b<<endl;
7. a=a*b; //a=50 (5*10)
8. b=a/b; //b=5 (50/10)
9. a=a/b; //a=10 (50/5)
10. cout<<"After swap a= "<<a<<" b= "<<b<<endl;
11. return 0;
12. }

Output:

Before swap a= 5 b= 10
After swap a= 10 b= 5
10 Write a programme to perform multiplication of two
matrices.

#include <iostream>
using namespace std;

int main()
{
int a[10][10], b[10][10], mult[10][10], r1, c1, r2, c2, i, j, k;

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;

// If column of first matrix in not equal to row of second matrix,


// ask the user to enter the size of matrix again.
while (c1!=r2)
{
cout << "Error! column of first matrix not equal to row of second.";

cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;

cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;
}

// Storing elements of first matrix.


cout << endl << "Enter elements of matrix 1:" << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c1; ++j)
{
cout << "Enter element a" << i + 1 << j + 1 << " : ";
cin >> a[i][j];
}

// Storing elements of second matrix.


cout << endl << "Enter elements of matrix 2:" << endl;
for(i = 0; i < r2; ++i)
for(j = 0; j < c2; ++j)
{
cout << "Enter element b" << i + 1 << j + 1 << " : ";
cin >> b[i][j];
}

// Initializing elements of matrix mult to 0.


for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
mult[i][j]=0;
}

// Multiplying matrix a and b and storing in array mult.


for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
for(k = 0; k < c1; ++k)
{
mult[i][j] += a[i][k] * b[k][j];
}

// Displaying the multiplication of two matrix.


cout << endl << "Output Matrix: " << endl;
for(i = 0; i < r1; ++i)
for(j = 0; j < c2; ++j)
{
cout << " " << mult[i][j];
if(j == c2-1)
cout << endl;
}

return 0;
}

Output

Enter rows and column for first matrix: 3


2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2


3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:


Enter elements a11: 3
Enter elements a12: -2
Enter elements a13: 5
Enter elements a21: 3
Enter elements a22: 0
Enter elements a23: 4

Enter elements of matrix 2:


Enter elements b11: 2
Enter elements b12: 3
Enter elements b21: -9
Enter elements b22: 0
Enter elements b31: 0
Enter elements b32: 4

Output Matrix:
24 29
6 25

You might also like