sum of numbers formula code example
Example 1: 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
Example 2: sum of number
// Find Sum of Digits of a Number using for Loop
// ----codescracker.com----
#include<iostream>
using namespace std;
int main()
{
int num, rem, sum;
cout<<"Enter the Number: ";
cin>>num;
for(sum=0; num>0; num=num/10)
{
rem = num%10;
sum = sum+rem;
}
cout<<"\nSum of Digits = "<<sum;
cout<<endl;
return 0;
}