Thread: Triangular number?

  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 01: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, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12: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