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 C++ program that asks the user to enter in three numbers and displays the...
Write a C++ program that asks the user to enter in three numbers and displays the numbers in ascending order. If the three numbers are all the same the program should tell the user that all the numbers are equal and exits the program. Be sure to think about all the possible cases of three numbers. Be sure to test all possible paths. Sample Runs: NOTE: not all possible runs are shown below. Sample Run 1 Welcome to the order...
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 that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending...
(Use the GenericStack class) Write a program that displays the first 100 prime numbers in descending order. Use a stack to store the prime numbers.
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT