Computer Science, asked by mkchesil, 5 hours ago

Write a C++ program to print out the first 20 ODD numbers and print their sum by

using DO-WHILE loop​

Answers

Answered by anindyaadhikari13
4

Solution.

Here comes the approach for the problem using C++

#include <iostream>

using namespace std;

int main() {

   cout<<"The first 20 odd numbers are - "<<endl;

   int sum=0,i=1;

   do{

       cout<<i<<" ";

       sum=sum+i;

       i=i+2;

   } while(i<=2*20-1);

   cout<<"\nSum of the numbers is: "<<sum;

   return 0;

}

Solution.

We know that,

→ nth odd number = 2n - 1

We have to display 20 odd numbers.

So, 20th odd number = 2 * 20 - 1 = 39

So, we have to iterate loop from i = 1 to 39 to display first 20 odd numbers.

At the same time, we will calculate the sum of the numbers.

After displaying the numbers, the sum is displayed.

See attachment for output.

Attachments:
Similar questions