In: Computer Science
Write a program in C++ that generates and displays the first N three digit odd numbers. Whereas the number N is provided by the user.
#include<iostream>
using namespace std;
int main()
{
// prompt user to enter the value of N
int N;
cout << "How many numbers you want to generate?
";
cin >> N;
// an array to store the numbers
int *arr = new int[N];
// generate first N 3 digit odd numbers (3 digit
means, the range is between 101 & 999)
// we can start the loop from 101 instead of 100 as we
know 100 is an even number
int i = 0, j = 101;
while (i <= N || (j >= 101 && j <=
999))
{
if (i == N || !(j >= 101
&& j <= 999))
break;
if (j % 2 != 0)
arr[i++] =
j;
j++;
}
// display the array
cout << "\nFirst " << N << " 3
-digit odd numbers: [";
for (int i = 0; i < N; i++)
{
if (i == N - 1)
cout <<
arr[i] << "]\n";
else
cout <<
arr[i] << ", ";
}
return 0;
}
******************************************************* SCREENSHOT *********************************************************
CODE SCREENSHOT :
CONSOLE OUTPUT :