C++ question awesome question challenging too

  • Comp Sci
  • Thread starter Vishalrox
  • Start date
  • Tags
    C++
In summary: What exactly do you need help with?In summary, the conversation is about a challenging C++ question that involves printing factors of a number in a pyramid pattern. The code provided by the user prints periods and spaces instead of the desired output. The individual is seeking help with writing the code to print the factors correctly.
  • #1
Vishalrox
20
0
C++ question...awesome question...challenging too !

How to write the c++ code for this sort of question...i couldn't get the logic even...

to print the factors of a number given by the user in a pyramid pattern...like if 4 is the number given by the user...its factors are 1,2,4...the we hv to print that in the format i hv provided in the attachment...

I hv attached a file to show how the output must look like...plez help !

Attempt to the solution :
To print stars in a pyramid format i could frame the code for printing 1 22 333..but couldn't for this question...

Code:
#include <iostream>
using namespace std;

int main() {

	// Loop through each row of the pyramid (1 - 9)
	for (int i = 1; i <= 9; i++) {

		// Create lead periods
		int totalperiods = 10 - i;
		for (int leadperiod = 1; leadperiod <= totalperiods; leadperiod++) {
			cout << ".";
		}

		// Now for each row, print that many numbers with a space in between
		// Spaces are needed in between due to the monospaced formatting. 
		// A space takes the same width as a number so to stagger you need placeholders.

		for (int j = 0; j < i; j++) {
			cout << " " << i;
		}

		// End pyramid line
		cout << endl;
	}

	return 0;
}
and by the way..is this correct...?...and ple help for the code i hv given...
 

Attachments

  • output.JPG
    output.JPG
    18.6 KB · Views: 352
Last edited by a moderator:
Physics news on Phys.org
  • #2


Please do not use textspeak when posting here at Physics Forums. Do not use "hv" for have, "please" for please, and so on.

I don't understand what you're trying to do. The attached file shows a pyramid with numbers in it, but your code is just printing periods and spaces.
 

1. What is C++?

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

2. What makes C++ an awesome language?

C++ is an awesome language because it is a powerful, efficient, and versatile programming language. It allows for low-level hardware manipulation, high-level abstractions, and supports various programming paradigms such as procedural, object-oriented, and generic programming.

3. What are some challenging aspects of C++?

C++ can be challenging for beginners because it has a steep learning curve and requires a strong understanding of computer science fundamentals such as memory management, pointers, and data structures. It also has a complex syntax and does not provide automatic memory management like other high-level languages.

4. How is C++ used in the real world?

C++ is used in a wide range of industries, including gaming, finance, telecommunications, and operating systems. It is commonly used for developing high-performance applications and systems that require efficient memory usage, such as game engines, operating systems, and scientific simulations.

5. What resources are available for learning C++?

There are many resources available for learning C++, including online tutorials, books, and coding communities. Some popular resources include "The C++ Programming Language" by Bjarne Stroustrup, "C++ Primer" by Stanley B. Lippman, and websites like Codecademy and Stack Overflow.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
896
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
838
Back
Top