Program to print Triangular Number series 1 3 6 10 15 ...N

Problem statement:- Program to print Triangular Number series 1 3 6 10 15 ...N

Data requirement:-

   Input Data:- n

  Output Data:-res

  Additional Data:-i

Program in C
  
Here is the source code of the C Program to print Triangular Number series 1 3 6 10 15 ...N.

Code:

#include<stdio.h>
int main()
{
    int n,i=1;
    printf("Enter the range of number(Limit):");
    scanf("%d",&n);
    while(i<=n)
    {
     int res=(i*(i+1))/2;
     printf("%d ",res);
     i++;
    }
}

Input/Output:
Enter the range of number(Limit):5
1 3 6 10 15

Program in C++
  
Here is the source code of the C++ Program to print Triangular Number series 1 3 6 10 15 ...N.

Code:

#include<iostream>
using namespace std;
int main()
{
    int n,i=1;
    cout<<"Enter the range of number(Limit):";
    cin>>n;
    while(i<=n)
    {
     cout<<int((i*(i+1))/2)<<" ";
     i++;
    }
}


Input/Output:
Enter the range of number(Limit):4
1 3 6 10

Program in Java
  
Here is the source code of the Java Program to print Triangular Number series 1 3 6 10 15 ...N.

Code:

import java.util.Scanner;
public class p10 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in); 
    int n,i=1;
    System.out.println("Enter the range of number(Limit):");
    n=cs.nextInt();
    while(i<=n)
    {
     System.out.print((int)((i*(i+1))/2)+" ");
     i++;
     }
     cs.close();
}
}

Input/Output:
Enter the range of number(Limit):
7
1 3 6 10 15 21 28 

Program in Python
  
Here is the source code of the Python Program to print Triangular Number series 1 3 6 10 15 ...N.

Code:

print("Enter the range of number(Limit):")
n=int(input())
i=1
while(i<=n):
    print((int)((i*(i+1))/2),end=" ")
    i+=1

Input/Output:
Enter the range of number(Limit):
5
1 3 6 10 15 


Post a Comment

0 Comments