C++ question awesome question challenging too

  • Context: Comp Sci 
  • Thread starter Thread starter Vishalrox
  • Start date Start date
  • Tags Tags
    C++
Click For Summary
SUMMARY

The discussion centers around a C++ programming challenge to print the factors of a user-defined number in a pyramid pattern. The user provided an initial code snippet that prints periods and spaces instead of the desired output. The correct approach involves calculating the factors of the number and formatting them into a pyramid structure. The conversation highlights the need for clarity in code logic and adherence to proper programming conventions.

PREREQUISITES
  • Understanding of C++ syntax and structure
  • Knowledge of loops and conditional statements in C++
  • Familiarity with basic input/output operations in C++
  • Ability to manipulate strings and formatting in console output
NEXT STEPS
  • Research how to calculate factors of a number in C++
  • Learn about formatting console output in C++ using manipulators
  • Explore advanced loop techniques for nested structures in C++
  • Study examples of pyramid patterns in programming challenges
USEFUL FOR

C++ developers, programming students, and anyone interested in solving algorithmic challenges related to number formatting and output.

Vishalrox
Messages
20
Reaction score
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: 416
Last edited by a moderator:
Physics news on Phys.org


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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K