How can we print odd numbers up to 99 using nested for loops in C++?

Click For Summary

Discussion Overview

The discussion revolves around how to print odd numbers up to 99 using nested for loops in C++. Participants are exploring various coding approaches and conditions necessary to achieve the desired output.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant seeks help in completing a code segment to print odd numbers up to 99, expressing uncertainty about the conditions for the loops.
  • Another participant proposes a solution using a nested loop with the condition i < 2*n to print odd numbers, suggesting this approach will work effectively.
  • A later reply acknowledges the importance of the condition i < n for the inner loop, indicating a realization of its significance.
  • One participant shares an alternative code snippet but is questioned about its effectiveness, particularly regarding the inclusion of even numbers and the omission of 99.
  • Another participant suggests a different approach that increments i by 2, aiming to print only odd numbers, while questioning why 99 would not be printed in their previous code.
  • Further clarification is provided regarding the outer loop's role in determining the number of lines printed, with a mathematical explanation of why 99 would not appear in the output.

Areas of Agreement / Disagreement

Participants express differing views on the effectiveness of various code snippets, with some agreeing on certain conditions while others challenge the logic behind them. The discussion remains unresolved regarding the optimal approach to print odd numbers up to 99.

Contextual Notes

There are limitations in the proposed solutions, particularly regarding the conditions used in the loops, which may lead to incorrect outputs such as printing even numbers or failing to print 99. The discussion highlights the need for careful consideration of loop conditions.

ineedhelpnow
Messages
649
Reaction score
0
I have my final exam for C++ tomorrow. I was studying a previous exam and I came across a certain problem that I would like to know how to do.

Suppose that we want to print out the following on the screen, please complete the code segment below by filling in the blanks
1
1 3
1 3 5
...
...
1 3 5 7... 99
Code:
for(int n=1; ---1---;++n){
       for(int i=1; ---2---; ---3---)
       cout<<---4---;
     ---5---;
}

1 is n<=99
I don't know what 2,3,4 are. I think 2 is i<=99 but I'm not sure.
And 5 is cout<<endl;
 
Technology news on Phys.org
This is how I would code it:

Code:
for(int n = 1; n < 51; ++n){
    for(int i = 1; i < 2*n; i += 2)
        cout << i << ' ';
    cout << endl;
}
 
Thank You! It was the condition i<n that I couldn't seem to remember. :o
 
I edited the code I posted to include a space after each number on a line.

Just to be clear, we want [m]i < 2*n[/m]. :D
 
Code:
for(int n=1; n<=99 ;++n){
       for(int i=1; i<n ; ++i)
       cout<<i<<" ";
     cout<<endl;
}

Will ^ work as well?
 
ineedhelpnow said:
Code:
for(int n=1; n<=99 ;++n){
       for(int i=1; i<n ; ++i)
       cout<<i<<" ";
     cout<<endl;
}

Will ^ work as well?

No...you will wind up printing even numbers too. Also, you will never print the number 99.
 
Code:
for(int n=1; n<=99 ;++n){
       for(int i=1; i<n ; i+=2)
       cout<<i<<" ";
     cout<<endl;
}

How about ^? :D (Just want to see if it can be done in different ways also)

Why won't 99 ever be printed?
 
ineedhelpnow said:
Code:
for(int n=1; n<=99 ;++n){
       for(int i=1; i<n ; i+=2)
       cout<<i<<" ";
     cout<<endl;
}

How about ^? :D (Just want to see if it can be done in different ways also)

Why won't 99 ever be printed?

The outer loop determines how many lines you want to print...and this is 50:

$$N=\frac{99-1}{2}+1=50$$

The way you have it coded, there will be 99 lines printed. To see why 99 would never be printed, look at what happens the last time your outer loop is iterated...n is 99, but your condition statement on the inner loop is [m]i < n[/m]. :D
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
4
Views
3K