PF Programs Sem1
PF Programs Sem1
#include <iostream>
using namespace std;
int main()
{
int rows;
char ch = 'A';
for(int j = 0; j < row * 2 + 1; j++)
{
if (j >= row) cout << ch--;
else cout << ch++;
}
cout << endl;
}
return 0;
}
Sample Input:
Sample Output:
ABA
ABCBA
ABCDCBA
ABCDEDCBA
#include <iostream>
int main()
{
int d[5][5] = {{1,2,3,4,5},
{0,1,2,3,4},
{1,0,5,4,2},
{3,2,1,5,3},
{1,2,1,1,0}};
convolve(d, f, res);
return 0;
}
#include <iostream>
using namespace std;
bool isCyclic(int n)
{
int length = 0;
int temp = n;
while(temp > 0)
{
length++;
temp /= 10;
}
int currentPerm = n;
int modulus = 1;
return true;
}
int main()
{
int n;
if (isCyclic(n))
cout << "yay its cyclic u happy jerk";
else
cout << "ha! its not cyclic u retard";
return 0;
}
program to convert binary number to decimal
#include <iostream>
using namespace std;
int main()
{
int binaryDigits[100];
int size = 0;
do
{
cout << "Enter the binary digit " << size << "(-1 to stop
asking): ";
cin >> binaryDigits[size];
if (binaryDigits[size] == 0 || binaryDigits[size] == 1 ||
binaryDigits[size] == -1)
size++;
} while(binaryDigits[size - 1] != -1);
size--;
int number = 0;
number += digit;
}
cout << "The decimal number is " << number << endl;
return 0;
palindrome or not
#include <iostream>
using namespace std;
int main()
{
int number, temp, size = 0;
temp = number;
while(temp > 0)
{
temp /= 10;
size++;
}
int digits[100];
temp = number;
int isPalindrome = 1;
#include <iostream>
using namespace std;
int main()
{
int series[100];
int no = 0;
cout << "Enter the value of series (-9999 to stop asking): " <<
endl;
do
{
cin >> series[no];
no++;
no--;
int isFibonacci = 1;
#include <iostream>
using namespace std;
int main()
{
int number, toFound, size;
do
{
cout << "Enter the digit who's place value is to be found:
";
cin >> toFound;
size = 0;
int temp = toFound;
while(temp > 0)
{
temp /= 10;
size++;
}
size = 0;
int temp = number;
while(temp > 0)
{
size++;
temp /= 10;
}
int digits[20];
temp = number;
int isFound = 0;
int index = 0;
if (isFound)
{
switch (index)
{
case 0:
cout << "unit";
break;
case 1:
cout << "tens";
break;
case 2:
cout << "hundred";
break;
case 3:
cout << "thousands";
break;
case 4:
cout << "ten thousands";
break;
case 5:
cout << "hundred thousands";
break;
case 6:
cout << "millions";
break;
case 7:
cout << "ten millions";
break;
default:
cout << "out of my range meow";
break;
}
}
else
{
cout << "digit not found";
}
return 0;
rotation of an array
#include <iostream>
using namespace std;
int main()
{
int arr[15] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int newarr[15];
int rotation;
int size = 15;
do
{
cout << "How much rotation: ";
cin >> rotation;
} while(rotation < 0);
for(int i = 0; i < size; i++) cout << newarr[i] << " ";
cout << endl;
return 0;
}
int main()
{
int A1[10] = {0,2,4,8,10,12,14,16,18,20};
int A2[10] = {0,3,6,9,12,15,18,21,24,27};
int A3[10];
int i = 0, j = 0, k = 0;
pattern: diamond
#include <iostream>
using namespace std;
void spaces(int n)
{
for(int i = 0; i < n; i++) cout << " ";
return;
}
void pluses(int n)
{
for(int i = 0; i < n; i++) cout << "+";
return;
}
int main()
{
int rows;
do
{
cout << "Enter the number of rows: ";
cin >> rows;
int sp = rows - 1;
int p1 = 1;
int i = 1;
sp += 2;
p1 -= 4;
i = 1;
return 0;
}
#include <iostream>
using namespace std;
void spaces(int n)
{
for(int i = 0; i < n; i++) cout << " ";
return;
}
void pluses(int n)
{
for(int i = 0; i < n; i++) cout << "+";
return;
}
int main()
{
int n;
do
{
cout << "Enter the number of rows: ";
cin >> n;
} while(n < 0);
spaces(sp1);
pluses(1);
sp1--;
int sp2 = 1;
int i = 1;
while(i <= n - 1)
{
spaces(sp1);
pluses(1);
spaces(sp2);
pluses(1);
sp1--;
sp2 += 2;
i++;
sp1 += 2;
sp2 -= 4;
i = 1;
while(i <= n - 2)
{
spaces(sp1);
pluses(1);
spaces(sp2);
pluses(1);
sp1++;
sp2 -= 2;
i++;
spaces(n - 1);
pluses(1);
return 0;
}
pattern: numbers pyramid
#include <iostream>
using namespace std;
int main()
{
int rows;
do
{
cout << "Enter the number of rows: ";
cin >> rows;
} while(rows <= 0);
int counter = 1;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int rows;
int row = 0;
int neum = 1;
i = 1;
int j = 0;
int i = 1;
while(i <= j)
{
deno1 *= i;
i++;
}
i = 1;
j++;
}
return 0;
}
a password generator
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
srand(time(NULL));
string ch;
string password;
int size;
cout << "Enter the length of the password: ";
cin >> size;
for(int i = 0; i < size; i++)
{
ch = static_cast<char>(rand() % 94 + 33);
password.append(ch);
}
cout << "Password: " << password << endl;
return 0;
}
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char text[400] = "Cheating is the getting of a reward for ability
by dishonest means or finding an easy way out of an unpleasant
situation. It is generally used in situations to gain unfair advantage
in a competitive situation. This broad definition will necessarily
include acts of bribery, cronyism, sleaze, nepotism and any situation
where individuals are given preference using inappropriate criteria";
char word[20];
char rep[20];
return 0;
}
#include <iostream>
#include <cmath>
using namespace std;
return guess;
}
int main()
{
double num;
#include <iostream>
using namespace std;
int main()
{
double num;
cout << "Enter a number: ";
cin >> num;
cout << sqrt(num) << endl;
return 0;
}
transpose of a matrix
#include <iostream>
using namespace std;
int main()
{
int arr[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
int r = 3, c = 3;
PRINT("Before transposing");
print(arr, r, c);
transpose(arr, r, c);
PRINT("After transposing");
print(arr, r, c);
}
a program to merge two sorted arrays into a sorted
array
#include <iostream>
using namespace std;
// this will only run if all of the elements of array A have been
used and there are still some left in B
while(j < n)
{
C[k] = B[j];
j++;
k++;
}
}
int main()
{
int A[] = {0,4,5,5,7,9};
int B[] = {-1,0,3,6};
int m = 6, n = 4;
int C[10];
merge(A, m, B, n, C);
for(int i = 0; i < m + n; i++)
cout << C[i] << " ";
cout << endl;
return 0;
}
terminated strings, finds the largest string (longest in terms of length up to the null-
int largest(….) that returns the row/index number of the largest word. In case of
int shortest(….) that returns the row/index number of the shortest word. In case of
Example:
#include <iostream>
#include <string.h>
using namespace std;
return l_index;
}
return s_index;
}
do
{
cout << "Enter the number of rows: ";
cin >> R;
} while (R <= 0);
swapWord(matrix, R);
printArr(matrix, R);
system("pause");
return 0;
}
Example:
B = { “banana” };
return true;
#include <iostream>
#include <string.h>
using namespace std;
if (matches == strlen(rep))
found = true;
}
return found;
}
return found;
}
int main()
{
char A[20][20] = { "applepie", "abananafruit", "cherrycake",
"datebar", "elderberryjam" };
char B[] = "banana";
int R = 5;
do
{
cout << "Enter the number of rows: ";
cin >> R;
} while (R <= 0);
if (MatchSubMatrix(A, R, B))
{
cout << "Found it";
}
else
{
cout << "Not found";
}
system("pause");
return 0;
}
#include <iostream>
#include <string.h>
using namespace std;
return;
}
int main()
{
char A[5][15] = { "apple,oran", "ge,seas", "on,Catterpill",
"ar,ban", "ana" };
char B[5][15];
cout << "Before: " << endl;
print(A);
seperate(A, B);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int i = 0;
int main()
{
char matrix[5][5] = {{'A','B','C','D','E'},
{'F','G','H','I','J'},
{'K','L','M','N','O'},
{'P','Q','R','S','T'},
{'U','V','W','X','Y'}};
int rows = 5;
#include <iostream>
using namespace std;
int k;
if (matches == strlen(word))
{
startRow = i;
endRow = i;
startCol = j;
endCol = j + strlen(word) - 1;
found = true;
}
}
}
return found;
}
if (matches == strlen(word))
{
startRow = i;
endRow = i;
startCol = j;
endCol = j - strlen(word) + 1;
found = true;
}
}
}
return found;
}
if (matches == strlen(word))
{
startRow = i;
endRow = i + strlen(word) - 1;
startCol = j;
endCol = j;
found = true;
}
}
}
return found;
}
if (matches == strlen(word))
{
startRow = i;
endRow = i - strlen(word) + 1;
startCol = j;
endCol = j;
found = true;
}
}
}
return found;
}
bool searchWord(char matrix[][9], int rows, int cols, char word[], int&
startRow, int& startCol, int& endRow, int& endCol)
{
if (searchHorizontal(matrix, rows, cols, word, startRow, startCol,
endRow, endCol))
return true;
if (searchReverseHorizontal(matrix, rows, cols, word, startRow,
startCol, endRow, endCol))
return true;
if (searchVertical(matrix, rows, cols, word, startRow, startCol,
endRow, endCol))
return true;
if (searchReverseVertical(matrix, rows, cols, word, startRow,
startCol, endRow, endCol))
return true;
return false;
}
int i;
int k;
for(i = startRow, k = 0; i <= endRow && rep[k] != 0; i++, k++)
{
matrix[i][startCol] = rep[k];
}
int i;
int k;
for(i = startRow, k = 0; i >= endRow && rep[k] != 0; i--, k++)
{
matrix[i][startCol] = rep[k];
}
void replaceWord(char matrix[][9], int rows, int cols, char word[], char
rep[])
{
int startRow, startCol, endRow, endCol;
int main()
{
char matrix[9][9];
int rows = 3;
int cols = 4;
char word[5];
char rep[5];
int main()
{
int matrix[5][5], rows = 5;
do
{
cout << "Enter the number of rows: ";
cin >> rows;
} while(rows <= 0);
shiftRows(matrix, rows);
return 0;
}
#include <iostream>
using namespace std;
int count = 0;
if (count > 1)
return -1;
int length = 0;
double n = 0;
if (num[0] == '-')
decimalIndex++;
int mod = 1;
n /= mod;
return n;
}
int main()
{
char num[20];
double n = stonum(num);
if (n == -1)
cout << "Error!!!~!!!";
else
cout << "The number is " << n;
#include <iostream>
using namespace std;
return sum;
}
if (isNum(text[last]))
{
int sum = sumBetween(text, i, last);
if (sum > key)
{
reverse(text, i, last);
}
}
}
}
}
int main()
{
char message[200] = "Bi9llio6ns1 of messages contain Qco3de9 and
Myst9erious3 patterns, but only extr9a5 stuff M8encoded4 and Qdeep7 have
secrets.";
int key = 10;
char Triggers[6] = "BQM";
system("pause");
return 0;
}
#include <iostream>
using namespace std;
return count;
}
return -1;
}
int change = l / 2 + 1;
password[change]++;
int l = length(password);
if (l < minChars)
{
appendUsername(password, username, l, minChars);
mods++;
}
if (digitCount(password) == 0)
{
appendDigit(password);
mods++;
}
if (isPalindrome(password))
{
changeMiddle(password);
mods++;
}
return mods;
}
int main()
{
char username[20] = "Bob_K";
char password[20] = "abcba";
system("pause");
return 0;
}
Write a function that swaps the values of three variables in a circular manner (i.e., a gets b’s
value, b gets c’s value, and c gets a’s value). The user-defined function should not use 4th
variable for swapping and the main function should display the values after swapping.
Example Input:
Example Output:
b = b + c;
c = b - c;
b = b - c;
}
int main()
{
int a, b, c;
cout << "Before swap: a = " << a << ", b = " << b << ", c = " << c
<< endl;
superswap(a, b, c);
cout << "After swap: a = " << a << ", b = " << b << ", c = " << c
<< endl;
system("pause");
return 0;
}
Note: This code is the property of *****cyberpatcher. U can use it freely as u want. There can be some
mistakes. I’m a human after all.