Search
 
SCRIPT & CODE EXAMPLE
 

CPP

sum of all n integers

Sum of n integers 1 + 2 + 3 + ... + n = n * (n + 1) / 2
Comment

formula for sum of n numbers

Sum of the First n Natural Numbers. We prove the formula 1+ 2+ ... + n = n(n+1) / 2, for n a natural number
Comment

sum of n natural numbers

//Created by https://ashif.in

#include <bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	cin>>n; //Take Input
	
  	int sum = n*(n+1)/2;    //Calculate Sum
  	
	cout<<sum;		//Print Sum
  
	return 0;
}
Comment

n numbers sum

# Not used any loop:
# Time complexity = O(1)
# Space Complexity = O(1)

# n = 10
n = int(input())
print("N-th only even numbers sum =",(n//2)*((n//2)+1))
print("N-th only odd numbers sum =",(n//2)**2)
print("N-th all numbers sum =",n*(n+1)//2)

#output:
# N-th only even numbers sum = 30
# N-th only odd numbers sum = 25
# N-th all numbers sum = 55
Comment

sum of n natural numbers

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n;
	int sum=0;
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		sum+=i;
	}
		cout<<sum<<" ";
	cout<<endl;
	return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: why return 0 in int main 
Cpp :: c++ linked list 
Cpp :: free a pointer c++ 
Cpp :: c++ define function pointer 
Cpp :: how we can write code to remove a character in c++ 
Cpp :: definition of singly linkedlist 
Cpp :: valid parentheses in cpp 
Cpp :: heap allocated array in c ++ 
Cpp :: C++ Pointers to Structure 
Cpp :: max circular subarray sum gfg practice 
Cpp :: C++ switch..case Statement 
Cpp :: quicksort algorithm 
Cpp :: is there garbage collection in c++ 
Cpp :: visual studio code terminal keeps closing c++ 
Cpp :: Round 1 Confusion codechef solution in c++ 
Cpp :: read a file line by line c++ struct site:stackoverflow.com 
Cpp :: store arbitrarly large vector of doubles c++ 
Cpp :: CPP print executable name 
Cpp :: c++ online 
Cpp :: kadane algo 
Cpp :: triangle angle sum 
Cpp :: error when using base class members 
Cpp :: c++ dynamic array 
Cpp :: C++ Join thread 
Cpp :: c++ iterator shorthand 
Cpp :: Fill 2-dimensional array with value 
Cpp :: qt get wireless interface name 
Cpp :: c++ trim string 
Cpp :: static_cast 
Cpp :: c++ Unable to get CMake Tutorial example to compile 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =