In this, we are going to see do-while loop, the program is based on do-while loop which prints, sum of 1 to 10.
The code given below can be used for TURBO C++ Compiler:-
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n = 1;
int sum = 0;
do
{
sum = sum + n;
n++;
} while (n <= 10);
cout << "Sum of first 10 natural numbers is " << sum;
getch();
}
The code given below can be used for g++/gcc Compiler:-
#include<iostream>
using namespace std;
int main()
{
int n = 1;
int sum = 0;
do
{
sum = sum + n;
n++;
} while (n <= 10);
cout << "Sum of first 10 natural numbers is " << sum;
return 0;
}
#ENJOY CODING
Post a Comment
FOR ANY DOUBTS AND ERRORS FEEL FREE TO ASK. YOUR DOUBTS WILL BE ADDRESSED ASAP