📜  程序打印三角数列直到n

📅  最后修改于: 2021-05-05 00:11:01             🧑  作者: Mango

三角数或三角形数对排列成等边三角形的对象进行计数,如右图所示。第n个三角形数是在一个侧面上具有n个点的三角形组成的点数,并且等于从1到n的n个自然数之和。

例子 :

Input : 5
Output : 1 3 6 10 15

Input : 10 
Output : 1 3 6 10 15 21 28 36 45 55

Explanation :
For k = 1 and j = 1 -> print k ( i.e. 1);
increase j by 1 and add into k then print k ( i.e  3 ) update k
increase j by 1 and add into k then print k ( i.e  6 ) update k
increase j by 1 and add into k then print k ( i.e 10 ) update k 
increase j by 1 and add into k then print k ( i.e 15 ) update k
increase j by 1 and add into k then print k ( i.e 21 ) update k
.
.
and so on.

使用的方法非常简单。循环循环直到给定n的值,并且每次迭代将j增加1并将其添加到k中,这将简单地打印三角数列直到n。
以下是实现上述方法的程序:

C
// C Program to find Triangular Number Series
#include 
 
// Function to find triangular number
void triangular_series(int n)
{
    int i, j = 1, k = 1;
 
    // For each iteration increase j by 1
    // and add it into k
    for (i = 1; i <= n; i++) {
        printf(" %d ", k);
        j = j + 1; // Increasing j by 1
        k = k + j; // Add value of j into k and update k
    }
}
// Driven Function
int main()
{
    int n = 5;
    triangular_series(n);
    return 0;
}


Java
// Java Program to print triangular number series till n
import java.util.*;
 
class GFG {
     
    // Function to find triangular number
    static void triangular_series(int n)
    {
        int i, j = 1, k = 1;
      
        // For each iteration increase j by 1
        // and add it into k
        for (i = 1; i <= n; i++) {
 
            System.out.printf("%d ", k);
            j = j + 1; // Increasing j by 1
            k = k + j; // Add value of j into k and update k
        }
    }
     
    // Driver function
    public static void main(String[] args)
    {
            int n = 5;
            triangular_series(n);
    }
}
         
// This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 code to find Triangular
# Number Series
 
# Function to find triangular number
def triangular_series( n ):
    j = 1
    k = 1
     
    # For each iteration increase j
    # by 1 and add it into k
    for i in range(1, n + 1):
        print(k, end = ' ')
        j = j + 1 # Increasing j by 1
         
        # Add value of j into k and update k
        k = k + j
         
# Driven Code
n = 5
triangular_series(n)
 
# This code is contributed by "Sharad_Bhardwaj"


C#
// C# Program to print triangular
// number series till n
using System;
 
class GFG {
     
    // Function to find triangular number
    static void triangular_series(int n)
    {
        int i, j = 1, k = 1;
     
        // For each iteration increase j by 1
        // and add it into k
        for (i = 1; i <= n; i++) {
 
            Console.Write(k +" ");
            j += 1; // Increasing j by 1
            k += j; // Add value of j into k and update k
        }
    }
     
    // Driver Code
    public static void Main()
    {
            int n = 5;
            triangular_series(n);
    }
}
         
// This code is contributed by vt_m.


PHP


Javascript


C
// C Program to find Triangular Number Series
#include 
 
// Function to find triangular number
void triangular_series(int n)
{
    for (int i = 1; i <= n; i++)
        printf(" %d ", i*(i+1)/2);
}
 
// Driven Function
int main()
{
    int n = 5;
    triangular_series(n);
    return 0;
}


Java
//Java program to print triangular number series till n
import java.util.*;
 
class GFG {
     
    // Function to find triangular number
    static void triangular_series(int n)
    {
        for (int i = 1; i <= n; i++)
            System.out.printf("%d ";, i*(i+1)/2);
    }
     
    // Driver function
    public static void main(String[] args)
    {
            int n = 5;
            triangular_series(n);
    }
}
         
//This code is contributed by Arnav Kr. Mandal.


Python3
# Python3 code to find Triangular
# Number Series
  
def triangular_series(n):
  
     for i in range(1, n + 1):
         print( i*(i+1)//2,end=' ')
  
# Driver code
n = 5
triangular_series(n)
# This code is contributed by ihritik


C#
// C# program to print triangular
// number series till n
using System;
 
class GFG {
     
    // Function to find triangular number
    static void triangular_series(int n)
    {
        for (int i = 1; i <= n; i++)
            Console.Write(i * (i + 1) / 2 + " ");
    }
     
    // Driver Code
    public static void Main()
    {
            int n = 5;
            triangular_series(n);
    }
}
         
// This code is contributed by vt_m.


PHP


Javascript


输出 :

1 3 6 10 15

替代解决方案:
该解决方案基于以下事实:第i个三角数是第i个自然数的和,即i *(i + 1)/ 2

C

// C Program to find Triangular Number Series
#include 
 
// Function to find triangular number
void triangular_series(int n)
{
    for (int i = 1; i <= n; i++)
        printf(" %d ", i*(i+1)/2);
}
 
// Driven Function
int main()
{
    int n = 5;
    triangular_series(n);
    return 0;
}

Java

//Java program to print triangular number series till n
import java.util.*;
 
class GFG {
     
    // Function to find triangular number
    static void triangular_series(int n)
    {
        for (int i = 1; i <= n; i++)
            System.out.printf("%d ";, i*(i+1)/2);
    }
     
    // Driver function
    public static void main(String[] args)
    {
            int n = 5;
            triangular_series(n);
    }
}
         
//This code is contributed by Arnav Kr. Mandal.

Python3

# Python3 code to find Triangular
# Number Series
  
def triangular_series(n):
  
     for i in range(1, n + 1):
         print( i*(i+1)//2,end=' ')
  
# Driver code
n = 5
triangular_series(n)
# This code is contributed by ihritik

C#

// C# program to print triangular
// number series till n
using System;
 
class GFG {
     
    // Function to find triangular number
    static void triangular_series(int n)
    {
        for (int i = 1; i <= n; i++)
            Console.Write(i * (i + 1) / 2 + " ");
    }
     
    // Driver Code
    public static void Main()
    {
            int n = 5;
            triangular_series(n);
    }
}
         
// This code is contributed by vt_m.

的PHP


Java脚本


输出 :

1 3 6 10 15