C++ Programs for Pattern Printing and String Manipulation, Assignments of Object Oriented Programming

Three C++ programs for printing different patterns using recursion and for counting the number of vowels in a given string. The first two programs, PrintPatternA and PrintPatternB, print star patterns of given size using recursion. The third program, countvowels, counts the number of vowels in a given string using recursion. The SumOfSquares program, which is not included in the document, calculates the sum of squares of numbers up to a given number.

Typology: Assignments

2020/2021

Uploaded on 06/21/2021

muhammad-faizan-mughal
muhammad-faizan-mughal 🇵🇰

5 documents

1 / 9

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Code 1
Pattern A:
#include<iostream>
using namespace std;
void PrintPatternA(int num)
{
if(num < 0)
{
cout << endl << "Please enter a non negative number." << endl;
}
else
{
if (num == 0)
{
return;
}
else
{
for (int q = 1; q <= num; q++)
{
cout << "*”;
}
cout << endl;
PrintPatternA(num - 1);
for (int q = 1; q <= num; q++)
{
cout << "*";
}
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download C++ Programs for Pattern Printing and String Manipulation and more Assignments Object Oriented Programming in PDF only on Docsity!

Code 1

Pattern A:

#include using namespace std; void PrintPatternA(int num) { if(num < 0) { cout << endl << "Please enter a non negative number." << endl; } else { if (num == 0) { return; } else { for (int q = 1; q <= num; q++) { cout << "”; } cout << endl; PrintPatternA(num - 1); for (int q = 1; q <= num; q++) { cout << "";

cout << endl; } } } int main() { int num; cout << "Enter the number of lines to print this pattern "<<endl; cin >> num; PrintPatternA(num); return 0;

int main() { int num; cout<<"enter limit to print staric pattern"<<endl; cin>>num; PrintPatternB(1, num); return 0;

Pattern C:

#include using namespace std; void Secondhalf(int j,int i,int z) { if(j==i) { return; } cout<<" "; Secondhalf(j+z,i,z); } void thirdhalf(int k,int i,int z) { if(k==i) { return; } cout<<"* "; thirdhalf(k+z,i,z); } void firsthalf(int row,int i) { if(i>row) { return; } Secondhalf(row,i,-1);

Code

#include #include using namespace std; void countvowels(int len,string s, int count) { if(len<0) { cout<<"number of vowels in string are "<<count<<endl; return; } else { if(s[len-1]=='a' || s[len-1]=='A' || s[len-1]=='e' || s[len-1]=='E' || s[len-1]=='i' || s[len-1]=='I' || s[len-1]=='o' || s[len-1]=='O' || s[len-1]=='u' || s[len-1]=='U') { count++;

len--; countvowels(len,s,count); } } int main() { string str; cout<<"enter astring"<<endl; cin>>str; int len=str.length(); countvowels(len,str,0); //cout<<"number of vowels in the string are "<<count<<endl; return 0; }

code 3

#include #include <math.h> using namespace std; void SumOfSquares(int n,int sum) { if(n==0) { cout<<"sum of squares from 0 to the given number is "<<sum<<endl; return; } else