Question

In: Computer Science

Write a program in C++ that generates and displays the first N three digit odd numbers....

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.

Solutions

Expert Solution

#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 :


Related Solutions

Write a C++ program that randomly generates N integer numbers (such that N is entered by...
Write a C++ program that randomly generates N integer numbers (such that N is entered by the user) and then stores them to a text file (myNumbers.txt) sorted in increasing (non-decreasing) order. Again, please notice that the size of the data (N) is known during the run time, not the compile-time (needs to be entered by the user after running the program).
Write a computer program for a logic bomb that continually generates 8-digit numbers randomly and increases...
Write a computer program for a logic bomb that continually generates 8-digit numbers randomly and increases a counter by one each time. If the random number meets the current date in a format mmddyyyy, it will display 6 times on screen the following message: Today is [date]! The count is: [nnnn] Hint: Since everyday is a different date, don’t hard code the date in your program. And the [nnnn] should be the number from your counter.
Write a program in C++ that computes the sum of odd numbers between 1 and 117....
Write a program in C++ that computes the sum of odd numbers between 1 and 117. Execute the program and submit a screen capture of the program and its results.
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by...
Write a C++ program that displays the numbers between 1000 and 9999, which are divisible by sum of the digits in them. For example, 2924 is divisible by (2+9+2+4 = 17). Your program should display all these possible numbers in the given range, and each number should be separated from the other by a space.
Write a C++ program that asks the user to enter a series of single-digit numbers with...
Write a C++ program that asks the user to enter a series of single-digit numbers with nothing separating them. Read the input as a C-string or a string object. The program should display the sum of all the single-digit numbers in the string. For example, if the user enters 2514, the program should display 12, which is the sum of 2, 5, 1, and 4. The program should also display the highest and lowest digits in the string. It is...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write a C++ program to read N numbers. Find sum, product, and average of N numbers
Write an application named OddNums that displays all the odd numbers from 1 through 99. All...
Write an application named OddNums that displays all the odd numbers from 1 through 99. All ODD numbers should show all together(Should not need to scroll down), do it in 10 rows. FOR C#
in java Write a program that reads in ten numbers and displays the number of distinct...
in java Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly one space (i.e., if a number appears multiple times, it is displayed only once). (Hint: Read a number and store it to an array if it is new. If the number is already in the array, ignore it.) After the input, the array contains the distinct numbers. Here is the sample run of the program: Enter...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into...
In Assembly Language Write a program that generates 10 random numbers (0~99). Save the numbers into arrayInt and calculate the sum. .data arrayInt Byte 10 DUP(?) Displays the array and the sum as follows: The random numbers are: xx xx xx xx xx xx …. The sum is   xxxx
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT