Table of Contents
In this tutorial, we will discuss do-while loop C++ programming language.
The do while loop is functioning similar while loop but there is a small difference. The body of the do while loop is executed at least once before the test expression is evaluated.
Syntax
do{ //codes inside the body of loop }while(testExpression);
Flow diagram
Program 1
This program displays natural numbers from 1 to n using do while loop
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int i=1;
while(i<=10){
cout << i << endl;
i++;
}
getch();
return 0;
}
When the above code is executed it produces the following the result
1 2 3 4 5 6 7 8 9 10
Program 2
This program allows the user to enter some numbers then it uses to find the sum of given numbers until the user enters zero
#include <iostream>
using namespace std;
int main() {
float number, sum = 0.0;
do {
cout<<"Enter a number: ";
cin>>number;
sum += number;
}
while(number != 0.0);
cout<<"Total sum = "<<sum;
return 0;
} When the above code is executed it produces the following the result
Program 3
This program allows the user to enter the number then it uses to find the factorial of given numbers
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num,fact=1,i=1;
cout << "Enter the number" << endl;
cin>>num;
do{
fact=fact*i;
i++;
}
while(i<=num);
cout<<"The factorial of "<<num<<" : "<<fact;
getch();
return 0;
}
When the above code is executed it produces the following the result
Enter the number 5 The factorial of 5: 120
Multiply two numbers in Java using scanner| 5 different ways In this article, we will…
5 Different ways to Divide two numbers in Java using scanner In this article, we…
Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…
10 ways to subtract two numbers in Java In this article, we will discuss the…
Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…
How to Divide two numbers in Java| 5 different ways In this article, we will…
This website uses cookies.