What's new

Help Pa help po c++

Augustus69

Forum Veteran
Established
Joined
Oct 24, 2021
Posts
1,482
Reaction
326
Points
667
Problem Description: Susan was given a list of arbitrary numbers, she was tasked to display all the even numbers from the list.

Input Format: The line begins with an integer n indicating the number of integers m that follow which compromises a list.

Output Format: All even numbers from the list, each separated by a comma",".

2 <= n <= 100

-1000

Constraints: <= m <= 1000

Sample Input: 2 3 6 7
Output : 2, 6

Code ko(walang output ang problema ko huhu):

#include <iostream>
using namespace std;

int main() {

int n,m=0;

cout<< "Enter the number of number: ";
cin>>n;

if (2 <=n && n<= 100){

while (n>0){
if (1000 <=m && m<= 1000){
if (m%2==0){
cout<<m<<",";
}
}
m++;
n--;
}
}


return 0;
}
 
There are a few issues with your code that are preventing it from producing the desired output. Let's go through each issue and provide the corrected code.

1. The input is not being read correctly:
The line cin >> n; is only reading the first number in the input, but you need to read all the numbers in the list. One way to solve this is by using a loop to read each number one by one.

2. The condition if (1000 <= m && m <= 1000) is incorrect:
This condition checks if m is between 1000 and 1000, which will always be false. Instead, you should check if m is within the given constraints.

3. The comma "," is being printed after the last even number:
Your code is printing a comma after each even number, including the last one. To avoid this, you can check if the current number is the last even number and print it without the comma.

Here's the corrected code:

C++:
#include <iostream>
using namespace std;

int main() {
    int n;
    cout << "Enter the number of numbers: ";
    cin >> n;

    if (2 <= n && n <= 100) {
        int count = 0;
        cout << "Enter the numbers: ";

        while (count < n) {
            int m;
            cin >> m;

            if (-1000 <= m && m <= 1000 && m % 2 == 0) {
                cout << m;
                
                if (count < n - 1) {
                    cout << ", ";
                }
            }
            
            count++;
        }
    }

    return 0;
}

In this code, we use a count variable to keep track of the number of numbers read so far. The count variable is incremented after each number is read. We also check if the current number is the last even number before printing the comma.
 
Check mo dito https://www.häçkerearth.com/problem...pose=signup&source=problem-page&update=google lods (Submission tab pili ka lang kung anong languange), medyo same lang, konting edit lang sa code
 
hindi ko mahanap submission tab
ito po

1698314993796.png
 

Attachments

Similar threads

Back
Top