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

In summary, The conversation is about a student preparing for their C++ final exam and coming across a question on printing a specific pattern. The student shares their solution and another person suggests a different approach. The conversation ends with a discussion about the possibility of printing the pattern using a different method.
  • #1
ineedhelpnow
651
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
  • #2
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;
}
 
  • #3
Thank You! It was the condition i<n that I couldn't seem to remember. :eek:
 
  • #4
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
 
  • #5
Code:
for(int n=1; n<=99 ;++n){
       for(int i=1; i<n ; ++i)
       cout<<i<<" ";
     cout<<endl;
}

Will ^ work as well?
 
  • #6
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.
 
  • #7
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?
 
  • #8
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:

\(\displaystyle 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
 

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

1. What is C++ programming?

C++ is a high-level, general-purpose programming language that was developed by Bjarne Stroustrup in 1983. It is an extension of the C programming language and is widely used for developing applications, games, operating systems, and more.

2. What are the main features of C++?

Some of the main features of C++ include object-oriented programming, low-level memory manipulation, high performance, and a large standard library. It also supports multiple paradigms such as procedural, functional, and generic programming.

3. How do I declare variables in C++?

Variables in C++ can be declared by specifying the data type followed by the variable name. For example, to declare an integer variable named 'num', you would write int num; Variables can also be assigned values at the time of declaration using the '=' operator, such as int num = 10;

4. How do I write a loop in C++?

C++ offers different types of loops such as for, while, and do-while. A for loop is commonly used and follows the syntax for(initialization; condition; increment/decrement){ // code to be executed } This loop will continue to execute until the condition is met.

5. How do I handle errors or exceptions in C++?

C++ provides a mechanism to handle errors and exceptions using the try-catch block. The code that may throw an exception is placed inside the try block and if an exception occurs, it is caught by the catch block. The catch block can also be used to handle specific exceptions based on their type.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
3
Replies
89
Views
4K
  • Programming and Computer Science
Replies
19
Views
993
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
32
Views
2K
Back
Top