Question

#include <iostream> using namespace std; int main() {} Write a program that produces a line comprised...

#include <iostream>
using namespace std;

int main() {}

Write a program that produces a line comprised of n alternating a's and b's, where n is user-specified.

For example, the console content following a run of the completed program with an input of "5" is as follows:

 
 
 

Enter a number: 5 A line of length 5: ababa

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

#include<iostream>
using namespace std;
int main()
{
   int n;//Declaring a variable
   printf("Enter a number:");
   scanf("%d",&n);//Taking input from the user
   printf("A line of length %d :",n);
   for(int i=0;i<n;i++)
   {
       if(i%2==0) printf("a");//Printing a in place of even positions
       else printf("b");//Printing b in place of odd positions
   }
   return 0;
}

Ouput:

Add a comment
Know the answer?
Add Answer to:
#include <iostream> using namespace std; int main() {} Write a program that produces a line comprised...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT