2. Program to display number and their sum using while loop.

Chapter 6: 

Looping Structures

Programming Exercise

Problem # 2:

Write a program to display the following format using while loop:
    -----------------
    num        sum

    -----------------

    1        1
    2        3
    3        6
    4        10
    5        15
   -----------------

Solution:

#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int num=1,sum=1;
    cout<<"num\tsum"<<endl;
    while(num<=5)
    {
        cout<<num<<"\t"<<sum<<endl;
        num++;
        sum=sum+num;
    }
    return 0;
}

Let me know in the comment section if you have any question.

Previous Post:
1. Program to display number in incrementing and decrementing order using while loop.

Comments