ACTIVITY
Name:-lalit Ajit Gholap
College
and :- dr.dy.patil
science arts commerce
college akurdi.
Class:- sy.bca(sci)
Roll.no:- 25
Subject:-c++
Q1.Write a C++ program to print Floyd‟s triangle.
1
23
456
7 8 9 10
Program:
#include<iostream>
using namespace std; int
main()
{
int i, j, num=1;
for(i=0; i<5; i++)
{
for(j=0; j<=i; j++)
{
cout<<num<<" "; num+
+;
}
cout<<endl;
}
return 0;
}
Output:
1
23
456
7 8 9 10
11 12 13 14 15
Q2.Write C++ program to check whether number is palindrome or
not?
program:
#include <iostream>
using namespace std;
int main()
{
int n, num, digit, rev = 0;
cout << "Enter a positive number: ";
cin >> num;
n = num;
do
{
digit = num % 10;
rev = (rev * 10) + digit;
num = num / 10;
} while (num != 0);
cout << " The reverse of the number is: " << rev << endl;
if (n == rev)
cout << " The number is a palindrome."; else
cout << " The number is not a palindrome.";
return 0;
}
output:
Enter a positive number: 12321 The
reverse of the number is: 12321 The
number is a palindrome.
Enter a positive number: 12331 The
reverse of the number is: 13321 The
number is not a palindrome.
Q3.Write a C++ program to display factors of a number.
program:
#include<iostream>
using namespace std; int
main() {
int num = 20, i;
cout << "The factors of " << num << " are : "; for(i=1;
i <= num; i++) {
if (num % i == 0)
cout << i << " ";
}
return 0;
Output:
The factors of 20 are : 1 2 4 5 10 20
Q4.Write a C++ program to Armstrong number .
Program:
#include <iostream>
using namespace std;
int main() {
int num, sum = 0, digit; cout<<"Enter
a positive integer: "; cin>>num;
for(int temp=num; temp!=0;)
{ digit = temp % 10;
sum = sum +(digit * digit * digit);
temp = temp/10;
}
if(sum == num)
cout<<num<<" is an Armstrong number.";
else
cout<<num<<" is not an Armstrong number.";
return 0;
}
Output:
Enter a positive integer: 370 370
is an Armstrong number.
Q5.Write a C++ program to fibonacci series.
Program:
#include <iostream>
using namespace std;
int fib(int x)
{ if((x==1)||(x==0)) {
return(x);
}else {
return(fib(x-1)+fib(x-2));
}
int main() { int
x , i=0;
cout << "Enter the number of terms of series : "; cin
>> x;
cout << "\nFibonnaci Series : ";
while(i < x) {
cout << " " << fib(i); i+
+;
}
return 0;
}
Output:
Enter the number of terms of series : 15
Fibonnaci Series : 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377
Q6.Write a C++ program to convert decimal number to hexadecimal.
Program:
#include<iostream> using
namespace std;
int DecToHexDec(int dec, int);
char hexaDecNum[50];
int main()
int decimalNum, i;
cout<<"Enter the Decimal Number: ";
cin>>decimalNum;
i = DecToHexDec(decimalNum, 0); cout<<"\
nEquivalent Hexadecimal Value: "; for(i=i-1;
i>=0; i--)
cout<<hexaDecNum[i];
cout<<endl;
return 0;
int DecToHexDec(int dec, int i)
int rem; while(dec!
=0)
{
rem = dec%16;
if(rem<10)
rem = rem+48; else
rem = rem+55;
hexaDecNum[i] = rem;
i++;
dec = dec/16;
return i;
Output:
Enter the Decimal Number: 316
Equivalent Hexadecimal Value: 13C
Q7.Write a C++ program to generate multiplication table.
Program:
#include <iostream>
using namespace std;
int main()
{ int n = 8, i;
cout<<"The multiplication table for "<< n <<" is as follows:"<< endl; for
(i = 1; i <= 10; i++)
cout << n << " * " << i << " = " << n * i << endl; return
0;
}
Output:
The multiplication table for 8 is as follows: 8
*1=8
8 * 2 = 16
8 * 3 = 24
8 * 4 = 32
8 * 5 = 40
8 * 6 = 48
8 * 7 = 56
8 * 8 = 64
8 * 9 = 72
8 * 10 = 80
Q8.Write a C++ program to matrix multiplication.
Program:
#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;
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;
}
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];
}
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];
}
for(i = 0; i < r1; ++i) for(j
= 0; j < c2; ++j)
{
mult[i][j]=0;
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];
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
Q9.Write a C++ program to matrix transpose.
Program:
#include<iostream>
using namespace std;
int main() {
int transpose[10][10], r=3, c=2, i, j; int
a[3][3] = { {1, 2} , {3, 4} , {5, 6} };
cout<<"The matrix is:"<<endl;
for(i=0; i<r; ++i)
{ for(j=0; j<c; ++j)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl; for(i=0;
i<r; ++i) for(j=0;
j<c; ++j) {
transpose[j][i] = a[i][j];
}
cout<<"The transpose of the matrix is:"<<endl;
for(i=0; i<c; ++i) {
for(j=0; j<r; ++j)
cout<<transpose[i][j]<<" ";
cout<<endl;
}
return 0;
}
Output:
The matrix is:
12
34
56
The transpose of the matrix is:
135
246