Triangular number?

This is a discussion on Triangular number? within the C++ Programming forums, part of the General Programming Boards category; Triangular number: The triangular numbers are those resulting from the sum of the first n whole numbers. So the first ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    20

    Triangular number?

    Triangular number: The triangular numbers are those resulting from the sum of the first n whole numbers. So the first four are: 1; 3 (= 1 + 2); 6 (= 1 + 2 + 3) and 10 (= 1 + 2 + 3 + 4). Write the program to print out k first triangular.

  2. #2
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Have you done anything so far? Ask the user what k should be. After that you will probably want 2 loops to complete your task. One outer loop to go through each series up to k, and inside each loop you will want to print out the sum of 1 to whatever iteration of k you are on. Before you increment k be sure to print out the sum!

    So roughly:

    Code:
    //...
    int nK, sum;
    
    std::cin >> nK;
    
    for (int i = 0; i < nK; i++)
    {
       sum = 0;
       for (int j = 1; j <= i + 1; j++)
          sum += j;
       std::cout << "Triangle Number " << i + 1<< " is: " << sum << std::endl;
    }
    //...
    Last edited by valaris; 09-21-2009 at 02:28 AM.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    7
    You only require one loop since you are computing all the other j - 1 triangular numbers for each inner loop.

    That is all I will say since this is clearly a homework task.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 09:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 10:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 01:42 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21