Click here to Skip to main content
15,885,170 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to print this sequence (+ - . + - . + - . + - . )by using for loop but I could not get the right output plz help!!

What I have tried:

I am trying to print + - . + - . + - . + - .
In C compiler but it doesn't plz help !
Posted
Updated 26-Mar-23 1:59am
v2

Quote:
Our teacher says that you have to run loop 12 times.

1. The requirement that 12 passes should be used was not originally mentioned.
2. The name of the include file is misspelled.
3. Your solution attempt should be under "What I have tried:" and not in a comment to a solution.
4. I would suggest a solution with switch-case:
C
switch (i % 3) {
  case 0:
     printf(". "); break;
  case 1:
     printf("+ "); break;
  default:
     printf("- "); break;
}
 
Share this answer
 
v2
Comments
nani jan 26-Mar-23 8:23am    
@merano99 your solution is correct but at last it does not show 1 dot
nani jan 26-Mar-23 8:27am    
@merano99 thanks aloz I got it. I just start loop from 0 and change switch conditions a bit. But thanks alottttt❤️❤️❤️
merano99 26-Mar-23 11:41am    
Glad to hear that it now works as desired and you have learned something from which you will benefit in the future. That's the way it should be! Thanks for accepted solution and the stars.
C
#include <stdio.h>

int main() {
    int i;
    int j = 4;
    for(i =0 ; i < j; i++){
        printf("+ - . ");
    }
}
 
Share this answer
 
Comments
Richard MacCutchan 26-Mar-23 7:41am    
You do not help people by doing their work for them.
nani jan 26-Mar-23 7:50am    
I am a student and I am struggling since 3 days regarding this question so if he helped me out why are you interfering?
Richard MacCutchan 26-Mar-23 8:09am    
The whole point of an assignment is to get you to think, and use the understanding of the things you have been taught in class. If you do not understand the assignment then talk to your teacher.
nani jan 26-Mar-23 7:45am    
Your program is absolutely correct but our teacher says that you have to run loop 12 times. So how can I got this sequence by using this code?
#include<studio.h>
int main ()
{
for(int i=1;i<12;i++)
{
if(i%3==0)
printf(".");
else if(i%2==0)
printf("+");
else
printf("-");
}
}
nani jan 26-Mar-23 7:46am    
I coded this my own and it have some logical errors :(
Look at the characters that need to bed printed. A sequence of four sets, each comprising "+ - ." separated by a space. What exactly have you tried and what is the problem?
 
Share this answer
 
Comments
nani jan 26-Mar-23 7:32am    
@RichardMacCutchan I am trying to print a series that contain +-.+-.+-.+-. By using for loop in C
Richard MacCutchan 26-Mar-23 7:41am    
So what is the problem? As I suggested above this is a simple task.
Assuming that you are just being vague on your homework question, and the requirement is to print a string N characters long, where the first is a '+', the second is a '-', the third is a '.', and the pattern repeats from there ...

This isn't difficult, you just need to think about it.
Start by setting up a for loop to print N characters - make it always print a '+'. Run it, test it, fix it if necessary so it works for any value of N you can think of!
Then add a counter that starts at zero outside the loop. Inside the loop, replace the '+' with printing the value of the counter and increment teh counter each time you print. Run it, test it ... you know the drill - you are looking for an output like "012345678910111213..."
Now modify the loop so that it also resets the counter when it gets to three. Check that it now prints "012012012012012012012..."
Now replace the print with either an if...else if...else or a switch block (depending on where your course has got to) to print a character based on the value of the counter: 0 prints '+', 1 prints '-', 2 prints '.'

Run it, blah, blah. Does it work? If so, that's your task done.

It's a "trick": breaking the code up into smaller bits you know how to do.
This may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
nani jan 26-Mar-23 7:55am    
Can you plz give this solution in written form?
OriginalGriff 26-Mar-23 10:20am    
No, because that would defeat the purpose of you being set homework.
The problem is that humans don't learn skills by looking, by reading - we learn by doing. You can watch as much of the Tour de France as you want, but the first time you get on a bicycle you are going to fall off! And the second, and the third - until you work out the myriad tiny adjustments you need to make each microsecond to keep yourself upright: and when you do that you still won't be able to tell your kids how to do it! :laugh:

Developing code is a skill and like all skills it only develops with practice - and that's what homework like this is for; to get you thinking as a developer, or at least starting to!

I give you code you can hand in, you pass today's homework. And next week the homework assumes you can do this on your own so it builds on that and you find it even harder to do. And before you know it the final exam is here and you have no idea how to write code and no access to people who do ...

This isn't a complex task, it's pretty trivial as a coding job - but it gets you thinking about how things work, what you can do with them and that's getign teh "skill ball" rolling. Give it a try, it's really easier than you think!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900