Question

we do mot expect you tomoany the approach or incorporate any additional library methods The function printPattern(int num) pr
// You can print the values to stdout for debugging using namespace std; void printPattern ( int num){ int i, print-0; if (nu
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution for the given question are as follows -

Code :

#include <iostream>
using namespace std;
// printPattern - printPattern function defination
void printPattern(int num) {
// check num is even or odd
if (num % 2 == 0) {
// loop over num numbers and check each no is even or not and print i
for (int i = 0; i <= num; i++) {
if (i % 2 == 0) {
cout<< i << " ";
}
}
} else {
// loop over num numbers and check each no is odd or not and print i
for (int i = 0; i <= num; i++) {
if (i % 2 != 0) {
cout<< i << " ";
}
}
}
}
int main()
{
// local variable declaration
int n;
// get input from user
cout<<"Enter any number: ";
cin>> n;
// check for negative number
if (n < 0) {
cout<< "Invalid number";
}
// call to printPattern() function
printPattern(n);
return 0;
}

Code Screen Shot :

1 tinclude<iostream> 2 using names pace std; 3 // printPattern 4- void printPattern (int num) { printPattern function definat

Output :

Enter any number: 2 0 2 . .Program finished with exit code 0 Press ENTER to exit console.

Enter any number: 7 1 3 5 7 . .Program finished with exit code 0 Press ENTER to exit console.

Add a comment
Know the answer?
Add Answer to:
we do mot expect you tomoany the approach or incorporate any additional library methods The function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1 I You can print the values to stdout for debugging You are required to fix...

    1 I You can print the values to stdout for debugging You are required to fix all logical errors in the given code. You can click on Compile & Run anytime to check the compilation/execution status of the program. You can use System.outprintin to debug your code. The submitted code should be logically/syntactically correct and pass all testcases. Do not write the mainli function as it is not required. Code Approach: For this question, you will need to correct the...

  • please Code in c++ Create a new Library class. You will need both a header file...

    please Code in c++ Create a new Library class. You will need both a header file and a source file for this class. The class will contain two data members: an array of Book objects the current number of books in the array Since the book array is moving from the main.cc file, you will also move the constant array size definition (MAX_ARR_SIZE) into the Library header file. Write the following functions for the Library class: a constructor that initializes...

  • C++ problem where should I do overflow part? in this code do not write a new...

    C++ problem where should I do overflow part? in this code do not write a new code for me please /////////////////// // this program read two number from the user // and display the sum of the number #include <iostream> #include <string> using namespace std; const int MAX_DIGITS = 10; //10 digits void input_number(char num[MAX_DIGITS]); void output_number(char num[MAX_DIGITS]); void add(char num1[MAX_DIGITS], char num2[MAX_DIGITS], char result[MAX_DIGITS], int &base); int main() { // declare the array = {'0'} char num1[MAX_DIGITS] ={'0'}; char...

  • Can some help me with my code I'm not sure why its not working. Thanks In...

    Can some help me with my code I'm not sure why its not working. Thanks In this exercise, you are to modify the Classify Numbers programming example in this chapter. As written, the program inputs the data from the standard input device (keyboard) and outputs the results on the standard output device (screen). The program can process only 20 numbers. Rewrite the program to incorporate the following requirements: a. Data to the program is input from a file of an...

  • C++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • c++ /*This is the starter file for your final proficiency test This program has a function...

    c++ /*This is the starter file for your final proficiency test This program has a function that will generate a list of integers using the rand function. Your job is to fill the insertNum and oddCount functions.*/ #include <iostream> #include <ctime> using namespace std; //constants and function prototypes const int CAP = 100; int buildList(int[], int size); void printList(int[], int size); //your functions to implement /*This function finds even numbers in the list, and inserts a number before the even...

  • This lab is to give you more experience with CH Making Decisions and Looping Please do...

    This lab is to give you more experience with CH Making Decisions and Looping Please do the following Review Questions 1. Using the following chart, write an if else if statement that assigns. 10..15 or 20 to commission, depending on the value in sales. Make sure sales is valid (ie. >= 0) Sales Commission Rate Up to $10.000 10% $10,000 to $15,000 15% Over $15,000 20% 2. Convert the following if else if statement into a switch statement. double number...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand....

    In C++ Do not use a compiler like CodeBlocks or online. Trace the code by hand. Do not write "#include" and do not write whole "main()" function. Write only the minimal necessary code to accomplish the required task.                                                                                                           Save your answer file with the name: "FinalA.txt" 1) (2 pts) Given this loop                                                                                              int cnt = 1;     do {     cnt += 3;     } while (cnt < 25);     cout << cnt; It runs ________ times    ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT