Question

C++ language. please provide detailed comments (for beginners) and use easy to understand/trace code. Thank you...

C++ language. please provide detailed comments (for beginners) and use easy to understand/trace code.

Thank you

---------------

A positive integer n is triangular if it can be obtained by the product of three consecutive
positive integers. Given n > 0, determine whether n is triangular.
For example, 120 is triangular, since 4 · 5 · 6 = 120.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int main() {
    int n;
    cout << "Enter a number: "; // ask user for a number
    cin >> n;   // read a number from user
    for (int i = 1; i < n-2; ++i) { // loop i values from 1 to n-2
        if (i * (i+1) * (i+2) == n) {   // check if product of 3 consecutive numbers give a value of n
            cout << n << " is a triangular number" << endl; // if yes, print it's a triangular number
            return 0;   // and exit the number
        }
    }
    cout << n << " is not a triangular number" << endl; // print not a triangular number if it's not
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
C++ language. please provide detailed comments (for beginners) and use easy to understand/trace code. Thank you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ language. please provide detailed comments (for beginners) and use easy to understand/trace code. Thank you...

    C++ language. please provide detailed comments (for beginners) and use easy to understand/trace code. Thank you -------------------------------------------------- Given positive integers n, a, and b, print the first n positive integers that are a multiple of a, b, or both. For example: n = 6, a = 2 and b = 3, you should print: 2 3 4 6 8 9

  • Please provide comments for each line, to make it easier to understand. Thank you! Write the...

    Please provide comments for each line, to make it easier to understand. Thank you! Write the following code in ARM 32 *AND* 64 bit assembly. Be sure to follow C calling conventions. char *strcpy(char *dst, char *src) { char *dst_copy = dst; while (*src) { (*dst) = (*src) ++dst; ++src; (*dst) = (*src) return dst_copy;

  • C++ Programming help, please include comments to help me understand the code. Thank you for helping....

    C++ Programming help, please include comments to help me understand the code. Thank you for helping. Task C: Substitution and Hamming Distance For this task, we will explore mutations that occur by substitution. Your task is to write a program called hamming.cpp that calculates the Hamming distance between two strings. Given two strings of equal length, the Hamming distance is the number of positions at which the two strings differ. e. g.: Hamming("aactgc", "atcaga") would output 3. Notice that certain...

  • Please make the answer detailed and easy to read, thank you :) The table shown below...

    Please make the answer detailed and easy to read, thank you :) The table shown below lists the costy (in dollars) of purchasing cubic yards of red landscaping mulch. The variable x is the length (ft) of each side of a cubic yard. Construct a scatterplot and identify the mathematical model that best fits the given data 1 4. 5 y (dollars) 33 6.2 12 14.9 17.8 Choose the correct scatterplot below. A 30- 20 20 20 N 10- 10-...

  • USE matlab please show code and leave comments. Thank you bb)for for the following signal show...

    USE matlab please show code and leave comments. Thank you bb)for for the following signal show how the series approaches the signal by increasing no. of terms w(t) 3n 2t 2n n cvcn n1.5, 9,13, n=3, 7, 11, 15, 2 2 7/4 sin nt dt =0 Show the result for 1,3, 6,20, 50, 120 terms Do you see any unexpected behavior in the result, explain( in any)

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • Please provide clear, detailed, easy to follow answers. Thank you! My Notes Ask Your Teacher v...

    Please provide clear, detailed, easy to follow answers. Thank you! My Notes Ask Your Teacher v A single loop of aluminum wire, lying flat in a plane, has an area of 8.40 cm and a resistance of 2.40 . A uniform magnetic field points perpendicular to the plane of the loop. The field initially has a magnitude of 0.500 T, and the magnitude increases linearly to 3.00 T in a time of 1.06 5. What is the induced current in...

  • Language is in Java, please explain the answer using comments, Thank you. I always rate!!! PLEASE...

    Language is in Java, please explain the answer using comments, Thank you. I always rate!!! PLEASE DO NOT import any packages to solve the problem. PLEASE use classes and methods from java.lang.*, since they are automatically imported without import statement. build a method called; public static in[] multbygrp(int[] elems, int grpsize) This method splits arr into subgroups of (equal) size groupSize, and multiply the contents of each subgroup. It returns the individual product in a new list. If splitting can't...

  • Please code in MatLab or Octave Output should match Sample Output in the second picture Thank...

    Please code in MatLab or Octave Output should match Sample Output in the second picture Thank You 4. In this problem we will investigate using loops to calculate the product of two matrices. Given an mxn matrix A and an n x p matrix B, the matrix product C = AB is the m xp matrix with Cij = R=1 Qikbky. Write a program which calls a function get matrix.dimensions which should • print a description of the purpose of...

  • C++ preffered please, thank you. Write a code in a high-level language (C++, C# or Java...

    C++ preffered please, thank you. Write a code in a high-level language (C++, C# or Java or ??) for a Deterministic Pushdown automaton which will accept the string a^nb^n where sigma = a, b. Create an input file with the following strings aaabbb, ababab, aababb, abbaab and give your output showing what is accepted and what is not accepted.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT