First Blog! My most viewed Quora Post
Originally answered in Quora!. My most viewed answer. (Almost 57k+ views!!) Click to view post.
Algorithm⌗
-
Start
-
Declare a variable of type int i = 0 and sum = 0
-
Iteration, iterate variable i in a loop from 1 to 50. On each iteration increment i by 1 and add i
for i = 1 and i <= 50 { sum = sum + i increment i = i + 1 }
-
Print Sum
-
Stop
C++ Syntax⌗
#include<iostream.h>
using namespace std
int main()
{
int i,sum=0;
for(i=1;i<=50;++i) // for loop to iterate upto 50
{
sum += i; //sum = sum + i (C++ Shorthands)
}
cout<<"Sum of first 50 numbers : "<<sum;
return 0;
}
Cheers!
Pic Credits: Photo by Aperture Vintage
Read other posts