how to code this..
in 200 to 1
i need to decrease by 3 and displaying odd or even numbers..
can you help me..
ty :)
how to code this..
in 200 to 1
i need to decrease by 3 and displaying odd or even numbers..
can you help me..
ty :)
Jump to PostYou need two things to do this. Firstly, you need to make a
for
loop that runs over all the numbers. Secondly, you need to use the%
operator on each.%
gives you the "remainder" of an integer division. If you divide an integer by 2 …
As i understood from your question is that, you want numbers to be displayed even or oddthe number sequence will be from 200 to 1 by decreasing by 3
c=0;
for(int i=200;i>0;i=i-3)
{
if(c)
{
cout<<i+" is even";
c=1;
}
else
{
cout<<i+" is odd";
c=0;
}
}
Simple logic... as we know if u decrease it by 3 alternate numbers will be even, so i did like this.
You can also do by chceking if it is divisible by 2 or not..
Correct me if i am wrong.
Sorry small change in code...
c=0;
for(int i=200;i>0;i=i-3)
{
if(c)
{
cout<<i+" is odd";
c=0;
}
else
{
cout<<i+" is even";
c=1;
}
}
You need two things to do this. Firstly, you need to make a for
loop that runs over all the numbers. Secondly, you need to use the %
operator on each. %
gives you the "remainder" of an integer division. If you divide an integer by 2 and look at the remainder, it will be 0 for even numbers and 1 for odd numbers:
x % 2 == 0; // if x is even
y % 2 == 1; // if y is odd
Have a play with those ideas and see how you get on. Post your code if you have any further problems
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.