---
title: "Write a program to find out the sum of 1 to n number which take minimum time it?"  
description: "Write a program to find out the sum of 1 to n number which take minimum time it?"  
author: "Ashutosh Patel"  
published: 2023-01-27  
canonical: https://answers.mindstick.com/qa/99051/write-a-program-to-find-out-the-sum-of-1-to-n-number-which-take-minimum-time-it  
category: "programming"  
tags: ["java programming", "script writing", "programming language", "c#", "pythons"]  
reading_time: 1 minute  

---

# Write a program to find out the sum of 1 to n number which take minimum time it?

I am [writing](https://www.mindstick.com/articles/23188/why-writing-skills-are-essential-to-succeed-in-business-and-technology) a [procedure](https://www.mindstick.com/articles/157262/why-to-go-with-the-gmail-signup-procedure) for writing [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) [sum](https://yourviews.mindstick.com/story/2587/what-is-lump-sum) of 1 to n numbers. but It takes more seconds to solve it. like

`var a = 1` , `var n = 12345678965487512` ,

`var sum = 12345678965487513` ,

## Answers

### Answer by Ravi Vishwakarma

I write some code.

## Program 1

```cs
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        long n = 1234567896584256, sum = 0;
        for(int i = 1; i <= n; i++)
        {
            sum += i;
        }
        Console.WriteLine("Sum "+ sum);
    }
}
```

In this example, this problem has been solved. but it takes more and more time to run it. It is not the right way to solve it because in this program time complexity and space complexity are high.

I write code for this question again.

## Program 2

```cs
using System;

public class HelloWorld
{
    public static void Main(string[] args)
    {
        long n = 10, sum = 0;
        sum = ( n * (n + 1)) / 2;
        Console.WriteLine("Sum "+ sum);
    }
}
```

In these answers, all program outputs are the same.


---

Original Source: https://answers.mindstick.com/qa/99051/write-a-program-to-find-out-the-sum-of-1-to-n-number-which-take-minimum-time-it

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
