Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PRactice PROGRAMS

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

PROGRAMS:

1) Fibonacci series:
#include<iostream>
using namespace std;
int main()
{
int i=0,n,t1=0,t2=1,t3;
cout<<"Enter the number of terms:"<<endl;
cin>>n;

cout<<"\nFibonacci series is:"<<endl;


if(n==1)
cout<<t1;
else if(n==2)
cout<<t1<<" "<<t2<<" ";
else
{
cout<<t1<<" "<<t2<<" ";
while(i<n-2)
{
t3=t1+t2;
cout<<" "<<t3<<" ";
t1=t2;
t2=t3;
i++;
}
}
return 0;
}
2) Prime number:
#include<iostream>
using namespace std;
int main()
{
int n, c=1;
cout<<"Enter the number:"<<endl;
cin>>n;

if(n<=1)
{
c=0;
}

else
{
for(int i=2;i<n;i++)
{
if(n%i==0)
{
c=0;
}
break;
}
}

if(c==0)
{
cout<<n<<" is not a prime number"<<endl;
}
else if(c==1)
{
cout<<n<<" is a prime number"<<endl;
}
return 0;
}

3) Palindrome number:
#include<iostream>
using namespace std;
int main()
{
int n,og,rev=0,r;

cout<<"Enter a number:"<<endl;
cin>>n;

og=n;

while(n!=0)
{
r=n%10;
rev=rev*10+r;
n/=10;
}

if(og==rev)
{
cout<<og<<" is a palindrome"<<endl;
}

else
{
cout<<og<<" is not a palindrome"<<endl;
}
return 0;
}

4) Factorial:
#include<iostream>
using namespace std;
int main()
{
int fact=1,i=1,n;
cout<<"Enter a number:"<<endl;
cin>>n;

if(n==0)
cout<<"fact=1"<<endl;
else
{
while(i<=n)
{
fact*=i;
i++;
}
cout<<"fact = "<<fact<<endl;
}
return 0;
}

5) Armstrong number:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n,og,r,d=0,sum=0;
cout<<"Enter a number:"<<endl;
cin>>n;

og=n;

while(og!=0)
{
og/=10;
++d;
}

og=n;

while(og!=0)
{
r=og%10;
sum+=pow(r,d);
og/=10;
}

if(sum==n)
{
cout<<n<<" is an Armstrong number"<<endl;
}
else
{
cout<<n<<" is not an Armstrong number"<<endl;
}

return 0;
}
6) Sum of Digits:
#include<iostream>
using namespace std;
int main()
{
int num,sum=0,r,og;

cout<<"Enter the number:"<<endl;


cin>>num;

og=num;

while(num!=0)
{
r=num%10;
sum+=r;
num/=10;
}

cout<<"Sum of digits = "<<sum<<endl;

return 0;
}

7) Reverse Number:
#include<iostream>
using namespace std;

int rev(int num)


{
int reversed = 0;
while(num!=0)
{
int digit=num%10;
reversed=reversed*10+digit;
num/=10;
}
return reversed;
}

int main()
{
int number;
cout<<"Enter a number: ";
cin>>number;

int reversedNumber=rev(number);

cout<<"Reversed number: "<<reversedNumber<<endl;

return 0;
}

8) Swap two numbers without using third variable:


#include<iostream>
using namespace std;

int main()
{
int a, b;

// Input two numbers


cout << "Enter two numbers to swap: ";
cin >> a >> b;

// Swapping without using a third variable


a = a + b; // Add both numbers and store the result in 'a'
b = a - b; // Subtract 'b' from 'a' (which is now the sum), store the original
'a' in 'b'
a = a - b; // Subtract 'b' (which is the original 'a') from 'a' (which is now
the original 'b')

// Output the swapped numbers


cout << "After swapping, a = " << a << " and b = " << b << endl;

return 0;
}

9) Matrix Multiplication:
#include <iostream>
using namespace std;

const int MAX_SIZE = 100; // Maximum size for matrices, adjust as needed

// Function to multiply two matrices


void multiplyMatrices(int mat1[MAX_SIZE][MAX_SIZE], int mat2[MAX_SIZE]
[MAX_SIZE], int result[MAX_SIZE][MAX_SIZE], int rows1, int cols1, int cols2)
{
// Initialize result matrix with zeros
for(int i = 0; i < rows1; ++i)
{
for(int j = 0; j < cols2; ++j)
{
result[i][j] = 0;
}
}

// Perform multiplication
for(int i = 0; i < rows1; ++i)
{
for(int j = 0; j < cols2; ++j)
{
for(int k = 0; k < cols1; ++k)
{
result[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

// Function to print a matrix


void printMatrix(int matrix[MAX_SIZE][MAX_SIZE], int rows, int cols)
{
for(int i = 0; i < rows; ++i)
{
for(int j = 0; j < cols; ++j)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
}

int main()
{
int mat1[MAX_SIZE][MAX_SIZE], mat2[MAX_SIZE][MAX_SIZE];
int result[MAX_SIZE][MAX_SIZE];
int rows1, cols1, rows2, cols2;

// Input dimensions of matrix 1


cout << "Enter number of rows and columns of matrix 1: ";
cin >> rows1 >> cols1;

// Input elements of matrix 1


cout << "Enter elements of matrix 1:" << endl;
for(int i = 0; i < rows1; ++i)
{
for(int j = 0; j < cols1; ++j)
{
cin >> mat1[i][j];
}
}

// Input dimensions of matrix 2


cout << "Enter number of rows and columns of matrix 2: ";
cin >> rows2 >> cols2;

// Input elements of matrix 2


cout << "Enter elements of matrix 2:" << endl;
for(int i = 0; i < rows2; ++i)
{
for(int j = 0; j < cols2; ++j)
{
cin >> mat2[i][j];
}
}

// Check if multiplication is possible


if(cols1 != rows2)
{
cout << "Matrix multiplication not possible. Number of columns in
matrix 1 should be equal to number of rows in matrix 2." << endl;
return 1;
}

// Perform matrix multiplication


multiplyMatrices(mat1, mat2, result, rows1, cols1, cols2);

// Output the result matrix


cout << "Resultant matrix after multiplication:" << endl;
printMatrix(result, rows1, cols2);
return 0;
}

10) Decimal to binary:


#include <iostream>
using namespace std;

// Function to convert decimal to binary


void decimalToBinary(int n)
{
// Array to store binary number
int binaryNum[32];

// Counter for binary array


int i = 0;
while(n > 0)
{
// Store remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}

// Print binary array in reverse order


cout << "Binary representation: ";
for(int j = i - 1; j >= 0; j--)
cout << binaryNum[j];

cout << endl;


}

int main()
{
int decimalNumber;

// Input decimal number


cout << "Enter a decimal number: ";
cin >> decimalNumber;

// Call function to convert and print binary equivalent


decimalToBinary(decimalNumber);

return 0;
}

11) Alphabet Triangle


#include <iostream>
using namespace std;

int main()
{
int n;
char current_char = 'A';

cout << "Enter the number of rows (1 to 26): ";


cin >> n;

if (n < 1 || n > 26)


{
cout << "Invalid input! Number of rows should be between 1 and 26."
<< endl;
return 1;
}

// Loop through each row


for (int i = 0; i < n; ++i)
{
// Print spaces before the characters
for (int j = 0; j < n - i - 1; ++j)
{
cout << " ";
}

// Print characters in increasing order


for (int j = 0; j <= i; ++j)
{
cout << current_char++;
}

// Print characters in decreasing order


for (int j = i - 1; j >= 0; --j)
{
cout << --current_char;
}

cout << endl;


current_char = 'A'; // Reset current_char for next row
}

return 0;
}

12) Number Triangle:


#include <iostream>
using namespace std;

int main()
{
int n;
cout << "Enter the number of rows (1 to 9): ";
cin >> n;

if (n < 1 || n > 9)
{
cout << "Invalid input! Number of rows should be between 1 and 9." <<
endl;
return 1;
}

// Loop through each row


for (int i = 1; i <= n; ++i)
{
// Print spaces before the digits
for (int j = 0; j < n - i - 1; ++j)
{
cout << " ";
}

// Print numbers in increasing order


for (int j = 1; j <= i; ++j)
{
cout << j;
}

// Print numbers in decreasing order


for (int j = i - 1; j >= 1; --j)
{
cout << j;
}

cout << endl;


}

return 0;
}

13) Fibonacci Triangle:


#include <iostream>
using namespace std;

// Function to calculate Fibonacci numbers


int fibonacci(int n)
{
if (n <= 1)
return n;
else
return fibonacci(n-1) + fibonacci(n-2);
}

int main()
{
int n;

cout << "Enter the number of rows for Fibonacci triangle: ";
cin >> n;

// Loop through each row


for (int i = 0; i < n; ++i)
{
// Print Fibonacci numbers for the current row
for (int j = 0; j <= i; ++j)
{
cout << fibonacci(j) << " ";
}
cout << endl;
}

return 0;
}

14) Number in Characters:


#include <iostream>
#include <string>
using namespace std;

// Function to convert a digit to its word representation


string digitToWord(int digit)
{
string words[] = {"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
return words[digit];
}

int main()
{
long long num;

cout << "Enter a number (up to 18 digits): ";


cin >> num;

if (num < 0 || num > 999999999999999999)


{
cout << "Number out of range. Please enter a number between 0 and
999,999,999,999,999,999." << endl;
return 1;
}

// Convert number to string to iterate over each digit


string numStr = to_string(num);

// Print each digit as words


cout << "Digits in words: ";
for (char digitChar : numStr)
{
int digit = digitChar - '0'; // Convert char to int
cout << digitToWord(digit) << " ";
}
cout << endl;

return 0;
}

You might also like