Simple descending order series
Printing a descending order series – Numbers divisible by 3 from 30 to 1.
Learning Objectives
- Series printing in the descending order.
Source Code
|
|
Run Output
Code Understanding
int start=30,upto=1;
Variable start has been set at 30 and the variable upto has been set at 1. For descending order series the start will be more than the upto variable
for(int i=start;i>=upto;–i)
Here the initialisation will be from higher variable and condition check is with lower variable so >= operator has been used. In the third part of loop –i decrementing has been done so that the descend happens.
{
if(i%3==0) cout<<i<<” “;
This is divisibility test. Here any given instruction set to perform the given task can be performed.
}
Notes
- Most descending series or loops will have > or >= operator as condition check. However this is problem specific.
Common Errors
- Student commit frequent errors in >= operator and –i decrement use. Watch carefully and perform a dry run on these kind of loops with test conditions from higher to lower values.
Suggested Filename(s): ser-desc.cpp
Created: 10-Jan-2018 21:47 | Updated: 15-Sep-2018 13:25|
