decreasing value of multiple of 3

closed account (Gv7fLyTq)
hello,

i'm learning c language program and want to make a program to decrease the value of multiple of 3 by 1 accumulatively, i.e. :
3 = 2
6 = 4
9 = 6....

i'm using if but it only decrease the value by 1 for 6 and the next.
any help is appreciated
Last edited on
Might need a little bit more information.

What value do you start with?
Why are you using an if statement?

Honestly, the information you've presented me with tells me I could do as little as:
1
2
for(i = 30; i >= 0; i -= 3)
    printf("%d", i);


This may be enough to get you started though
closed account (Gv7fLyTq)
okay so i want to make a formula that start with (n + 2) but every multiple of 3 the formula decreased by 1 for example :
if n = 0, then value is 2 formula is n+2
1, 3
2, 4
3, 4 formula become n+1
4, 5
5, 6
6, 6 formula become n
... 9, 8 formula become n-1 and so on
there's no real reason using if beside i only learn if and haven't learn other command yet
I think he wants
for(y = 1, x = 3; x < max; x+=3)
printf("%i\n", x-y++); //(3-1, 6-2, 9-3, 12-4, … ??)


or alternatively
for(y = 1; y < max; y++)
printf("%i\n", y*3-y); //1*3-1, 2*3-2, 3*3-3, 4*3-4, …


hmm your update made less sense. are you asking for this

1
2
3
4
5
6
7
8
9
10
#include <stdio.h>
int main()
{
int x,y,mx=30;
for(x = 0,y = 3; y < mx; y++)
{
if(y%3==0) x++;
printf("%i\n", y-x ); 
}
}


2
3
4
4
5
6
6
7
8
8
9
10
10
Last edited on
I think OP is looking for the formula n + 2 - (n / 3) = 2 ( n + 3 ) / 3.

1
2
3
4
5
6
#include <stdio.h>
int main(void)
{
  for (int i = 0; i <= 10; ++i) 
    printf("f(%2d) = %d\n", i, (2 * (i + 3)) / 3);
}
Last edited on
closed account (Gv7fLyTq)
what i mean is that i want a user to input an integer into the formula i made but the formula decrease/changed if the user input an integer within a multiple of 3

the starting formula is (n+2) therefore if the user input 0,1,2 the formula stay the same but if the user input 3 the formula changed to (n+1) for 3,4,5 and if 6 the formula changed to (n+0) for 6,7,8 and so on. The result printed is only 1 number not a list of numbers.

Simply enough - input/3. Store this value into an int to get rid of the decimal. This number will be the number you want to subtract from the 2 being added to n. So -

1
2
3
4
5
6
7
8
9
int userinput();
// Ask for it and assign it here

int subtract();
subtract = userinput/3;

int finalNumber();
finalNumber = 2 - subtract;


Made the code a bit longer so you can see everything happening. finalNumber will have the number to either add or subtract with N to use for your loop.
Last edited on
closed account (Gv7fLyTq)
thank you for all that took the time to reply
Topic archived. No new replies allowed.