#include<iostream.h>
void main()
{
int n,i=1,sum=0;
cout<<"Enter n value:";
cin>>n;
do
{
sum+=i;
i++;
}while(i<=n);
cout<<"Sum of first"<<n<<"Numbers:"<<sum;
}
output:
Enter n value: 10
sum of first 10 numbers: 55
using loop is do..while loop:
The do..while loop is another repetitive loop. whenever one is certain about a test condition, then may use the do..while loop as it enters into the loop atleast once and then checks whether the given condition is true or false
The general form of do..while is,
do
{
statements;
}while(expression);
void main()
{
int n,i=1,sum=0;
cout<<"Enter n value:";
cin>>n;
do
{
sum+=i;
i++;
}while(i<=n);
cout<<"Sum of first"<<n<<"Numbers:"<<sum;
}
output:
Enter n value: 10
sum of first 10 numbers: 55
using loop is do..while loop:
The do..while loop is another repetitive loop. whenever one is certain about a test condition, then may use the do..while loop as it enters into the loop atleast once and then checks whether the given condition is true or false
The general form of do..while is,
do
{
statements;
}while(expression);
No comments:
Post a Comment