C++ program to count tne sum of n numbers without using loops.

#include <iostream>
using namespace std;

int main()
{
    int num,sum=0;
    cin>>num;
    int init=0;
    if(num==1)
    {
        cout<<init+1;
    }
    else
    {
        a:
        init++;
        sum=sum+init;
        if(init<num)
        {
            goto a;
        }
        else
        {
            cout<<sum;
        }
    }
    return 0;
}

OUTPUT:

2
3


10
55

Comments

Popular posts from this blog

c++ program to print string along the diagonals of the matrix.

Kollywood Game

C program to print the following Pattern