Write a program which displays a given character, n number of times, using a function. When the n value is not provided, it should print the given character 80 times. When both the
character and n value is not provided, it should print ‘*’ character 80 times.
Write the above program in two ways: -
(i) By using function overloading
(ii) By using default arguments
(i)
#include <iostream>
using namespace std;
void print_char_n_times(char my_char, int n) {
cout << string(n, my_char) << endl;
}
int main() {
char my_char;
int n;
cout<<"Enter the number of times"<<endl;
cin>>n;
cout<<"Enter the char"<<endl;
cin>>my_char;
//displays a given character, n number of times, using a function.
if(n>0)
{
print_char_n_times(my_char, n);
}
//When the n value is not provided, it should print the given character 80 times
else if(n<0)
{
print_char_n_times(my_char, 80);
}
else//When both the character and n value is not provided, it should print ‘*’ character 80 times.
{
print_char_n_times('*', 80);
}
return 0;
}
(ii)
Comments
Thanks a lot !!
Leave a comment