C++ question awesome question challenging too

  • Topic: Comp Sci 
  • Thread starter Vishalrox
  • Start date
  • Tags Tags
    C++
1 reply · 2K views · last reply
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
  • Text instructions reading: Say if the given number by the user is 4, the output must be like: Its factors in pyramid format is: then a centred pyramid with 1 on the top row, 1 and 2 on the middle row, and 1, 2 and 4 on the bottom row.
    output.JPG
    18.6 KB · Views: 439
Last edited by a moderator:


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.