#include<iostream>
using namespace std;
int main()
{
    int i=119;
    while(i>=3){
            cout<<" "<<i;
            i--;
}
  system("pause");
  return 0;
  }

cout of this is counting 119 decrement til 3 but output should this:
119 94 72 53 37 24 14 7 3 could any1 help? thanks a lot guys!:D

Recommended Answers

All 3 Replies

if I understand properly you want to decrease the subtrahend by 3 every time and subtract it from your current result. To do that add another variable called subtrahend and initialise by 25. Each round of the loop decrease the subtrahend after you subtracted it from i.

#include<iostream>
using namespace std;
int main()
{
int i=119;
int subtrahend = 25;
while(i>=3){
cout<<" "<<i;
i-=subtrahend;
subtrahend -=3;
}
system("pause");
return 0;
}

First off, please use code tags.

Second off, in your code here:

while(i>=3){
cout<<" "<<i;
[B]i--;[/B]
}

you are only decrementing by one. You seem to want to start yuor decrement at 25, and then subtract the decrement by 3 each time (i.e. subtract 25; subtract 22; subtract 19) as stated by the desired output you wish to recieve. try using this code here in place of your while loop:

int j = 25; //this is what you will be decrementing by.
    while(i >= 3)
    {
        cout << ' ' << i;
        i = i - j; //decrement by 25,22,19,etc...
        j = j - 3;//this is your other decrementnc counter, used to 
//decrement i.
    }

also, these lines here:

i = i - j;
        j = j - 3;

can also be written as such:

i -= j; //same as i = i - j
j -= 3; //same as j = j - 3

hope this helps. :)

EDIT: drkybelk got to this one before me ;)

guys thanks to all. thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.