about 'loop'. help me please..

hi guys ı have a problem that ı have to solve but ı cant.now ı will enter a[0]=5.after that ı need odd and even number checker.if the number is odd(first number is 5) i will multiply by 3 and add 1 all the time so 5*3+1 =16. i find 16.now we have an even number if we have even number divided by 2 all the time so 16:2=8. and again even 8:2=4 result is even again 4:2=2 even again 2:2=1 and stop.when ı find 1 program will stop.at every result program will check even or odd . thanks...
Last edited on
You want a while loop:

1
2
3
4
while( /* some condition here */ )
{
  /* the code in this block will repeat until the above condition is false */
}


So if you want to keep doing something until you have 1, what do you think the condition will be?


EDIT: no need to post your question multiple times.
Last edited on
condition is ı will begin with 5 .number will check even or odd.if number iş odd multiply by 3 add 1 so 5*3+1=16 now number even diveded by 2 until 1 when you find 1 stop.at every result it will check even or odd...
in general your main will look somthing like this :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void main()
{
  int current;
  int index=0;
  cin>>current;
  checkInput(current);  //a function to check if the input is a legit number.
  while (current!=1)
  {
    array[index]=current;
    if (current%2==0)
      current = even_func();  //a function to be called if the number is even
    else
      current = odd_func();  //a function to be called if the number is odd
    index++;
  }
  array[index]=1;
}
]

you sould decide how to define the array, and how to implement the functions.
make sure u understand the logic of the while loop and the functions inside it,
so that the numbers are written in the correct index's, and the loop iterates a correct number of times.

hope this helps.

actually, if you say the first value is 5, then theres no need for the cin, or the functino to check the input.

i thought 5 was just an example of an input :)

so you are left with a pretty simple while loop.
Last edited on
@btucho:

>_> We're giving out full solutions now?

Don't write the code for him. He won't learn that way.

@tayf
tayf wrote:
condition is ı will begin with 5


No I mean the condition on which you want to keep looping.

You said you want to keep doing something with the number until it's 1, right? So then the condition for the loop would be while(number != 1). That way the loop will keep running until number==1.
Last edited on
understood but it will not be enough . you know ı m beginner thats way something is too difficult
values are true first value will be 5 my friend :) .how do you solve? never mind loop.firs value is 5 and after checked even or odd it will go on...... and finally will be 1 and stopç what do u think?
is there anyone to solve it? ı need immediatelly guys.
This isn't a homework service. Show us what you came up with so far and what you're having trouble with, and we can help you the rest of the way.

But we're not going to post a full solution (even though btucho came pretty close)
sorry all, i just thought that since it is the basics, maybe it will be best if hell see the general from of possible solution, and try to learn from it.
okay thank you friends
#include<stdio.h>
void main()
{
int a=5,z;
while (a!=1)
{
if (a%2==0)
{
printf ("The entered number is EVEN.\n");
}
else{printf ("The entered number is ODD.\n");
}
if (a%2==0)
z=a/2;
else if
z=a*3+1;
}
}

ı did this but it is not completed.there is one error (syntax error : identifier 'z').
1
2
else if
z=a*3+1;


get rid of the "if" here and your problem should go away. When you write "else if", the compiler is expecting a parentheses and another conditional expression, but you just want "else". This is probably why you're getting that error.

Also, for future reference, put code blocks around your code so it's easier to read. put [ CODE] at the top and [ /CODE] at the bottom (without the spaces between the [ and first character, of course).
Last edited on
Also note, you probably should be changing 'a' and not 'z'. Your program will loop infinitely because a is always 5.
finally i did guys thanks for your help :)


#include<stdio.h>
const nmax=100;
int a[nmax];
void main()
{
int z,i;
z=13;a[0]=z;i=0;
while (z!=1)
{
if (z%2==0)
{
printf ("The entered number is EVEN.%d\n",z);
z=z/2;
i++;
a[i]=z;
}
else
{printf ("The entered number is EVEN.%d\n",z);
z=z*3+1;
i++;
a[i]=z;
}
}
}
Topic archived. No new replies allowed.