Using while loops, write programs that calculates the sum of the first n counting numbers: 1 + 2 + 3 + .. + n
If user input 4, output=10
If user input 3, output=6
#include <iostream>
using namespace std;
int main()
{
int n,z=1,sum=0,y=1;
cout<<"n= ";
cin>>n;
while ( z<=n)
{
It looks like a random program with some loops thrown in and a pinch of hope it will work?
Make it clear in your mind how you would do it with pen and paper and then convert it to code.
You only need one loop. Within the loop you add z to sum and increment z while z < n.