Answer to Question #226440 in C++ for gikovi

Question #226440

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


1
Expert's answer
2021-08-16T01:04:11-0400
(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)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

gikovi
16.08.21, 20:46

Thanks a lot !!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS