Question

In: Computer Science

A triangular number is the sum of the n natural numbers from 1 to n. For...

A triangular number is the sum of the n natural numbers from 1 to n.

For example:

  • The triangular number for 3 is 1 + 2 + 3 = 6
  • The triangular number for 7 is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28

Write a program segment (using a loop), that calculates and then prints the integer n and its triangular number.

Solutions

Expert Solution

Source Code:

Output:

Code in text format (See above images of code for indentation):

#include <iostream>
using namespace std;
/*main function*/
int main()
{
   /*varibles*/
   int n,i,tnum=0;
   /*read n value from user*/
   cout<<"Enter n value: ";
   cin>>n;
   /*using loop calculate triangular number*/
   for(i=1;i<=n;i++)
       tnum+=i;
   /*print number and triangular number*/
   cout<<"The number n is: "<<n<<" and its triangular number is "<<tnum;
return 0;
}


Related Solutions

The number 81 is written as a sum of three natural numbers 81 = a +...
The number 81 is written as a sum of three natural numbers 81 = a + b + c (the triple (a,b,c) is ordered; e.g., the decompositions 81 = 1 + 1 + 79 and 81 = 1 + 79 + 1 are different. Also, assume that all the decompositions have equal probability.) What is the probability that there exists a triangle with sides a, b, and c? (PLEASE EXPLAIN THE STEPS AND WHY YOU KNOW TO DO STEPS, THANKS)
Let the cardinal number of N, the set of all natural numbers, be א0. Prove that...
Let the cardinal number of N, the set of all natural numbers, be א0. Prove that the product set N × N = {(m,n);m ∈ N,n ∈ N} has the same cardinal number. Further prove that Q+, the set of all positive rational numbers, has the cardinal number N_0. Hint: You may use the formula 2^(m−1)(2n − 1) to define a function from N × N to N, see the third example on page 214 of the textbook.
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to...
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to n so that the first number is added, the second number is subtracted, the third number added, the fourth subtracted, and so on: 1-2+3-4+5-6+7… until you reach n. If n is 0 or less then return 0.
The number 73 is written as a sum of three natural numbers 73=a+b+c (the triple (a,b,c)...
The number 73 is written as a sum of three natural numbers 73=a+b+c (the triple (a,b,c) is ordered; e.g., the decompositions 73=19+20+34 and 73=20+34+19 are different. Also, assume that all the decompositions have equal probability.) Given that there exists a triangle with sides a, b, and c, what is the probability that this triangle is isosceles?
1. Prove that given n + 1 natural numbers, there are always two of them such...
1. Prove that given n + 1 natural numbers, there are always two of them such that their difference is a multiple of n. 2. Prove that there is a natural number composed with the digits 0 and 5 and divisible by 2018. both questions can be solved using pigeonhole principle.
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers...
Show that among all collections with 2n-1 natural numbers in them there are exactly n numbers whose sum is divisible by n.
Let m, n be natural numbers such that their greatest common divisor gcd(m, n) = 1....
Let m, n be natural numbers such that their greatest common divisor gcd(m, n) = 1. Prove that there is a natural number k such that n divides ((m^k) − 1).
Show that |N| = |Z|, where N is the set of natural numbers and Z is...
Show that |N| = |Z|, where N is the set of natural numbers and Z is the set of all integers.
Show by induction that for all n natural numbers 0+1+4+9+16+...+ n^2 = n(n+1)(2n+1)/6.
Show by induction that for all n natural numbers 0+1+4+9+16+...+ n^2 = n(n+1)(2n+1)/6.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT