What's new

C & C++ Help po nested loop

PHC-Eye

Eternal Poster
Joined
Jun 6, 2020
Posts
589
Reaction
159
Points
274
IMG_20211203_003235.jpg
 

Attachments

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

int main()
{
    int arr[10]={};
    bool isEven = false;
    bool isOdd = false;

    cout<<"Input 10 integers in array :"<<endl;
    for(int i=1; i<=10; i++)
    {
        cout<<"array["<<i<<"]: ";
        cin>>arr[i];
        if(arr[i]%2 == 0)
        {
            isEven = true;
        }
        else
        {
            isOdd = true;
        }
    }
    cout<<endl<<endl;

    if(isEven){
        cout<<"Even integers Found!"<<endl;
        cout<<"The Even integers are: "<<endl;
    }
    else{
        cout<<"No Even integers Found!"<<endl<<endl;
    }

    for(int i=1; i<=10; i++)
    {
        if(arr[i]%2 == 0)
        {
            cout<<arr[i]<<" ";
        }
    }

    cout<<endl<<endl;
    if(isOdd){
        cout<<"Odd integers Found!"<<endl;
        cout<<"The Odd integers are: "<<endl;
    }
    else{
        cout<<"No Odd integers Found!"<<endl<<endl;
    }

    for(int i=1; i<=10; i++)
    {
        if(arr[i]%2 == 1)
        {
            cout<<arr[i]<<" ";
        }
    }

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

int main()
{
    int arr[10]={};
    bool isEven = false;
    bool isOdd = false;

    cout<<"Input 10 integers in array :"<<endl;
    for(int i=1; i<=10; i++)
    {
        cout<<"array["<<i<<"]: ";
        cin>>arr[i];
        if(arr[i]%2 == 0)
        {
            isEven = true;
        }
        else
        {
            isOdd = true;
        }
    }
    cout<<endl<<endl;

    if(isEven){
        cout<<"Even integers Found!"<<endl;
        cout<<"The Even integers are: "<<endl;
    }
    else{
        cout<<"No Even integers Found!"<<endl<<endl;
    }

    for(int i=1; i<=10; i++)
    {
        if(arr[i]%2 == 0)
        {
            cout<<arr[i]<<" ";
        }
    }

    cout<<endl<<endl;
    if(isOdd){
        cout<<"Odd integers Found!"<<endl;
        cout<<"The Odd integers are: "<<endl;
    }
    else{
        cout<<"No Odd integers Found!"<<endl<<endl;
    }

    for(int i=1; i<=10; i++)
    {
        if(arr[i]%2 == 1)
        {
            cout<<arr[i]<<" ";
        }
    }

    return 0;
}
wala po ba yung hindi gumagamit ng bool?
 
C++:
#include <iostream>
using namespace std;

int main()
{
    int arr[10]={};
    string a = "";
    string b = "";

    cout<<"Input 10 integers in array :"<<endl;
    for(int i=1; i<=10; i++)
    {
        cout<<"array["<<i<<"]: ";
        cin>>arr[i];

    }
    cout<<endl<<endl;

    for(int i=1; i<=10; i++)
    {
        if(arr[i]%2==0)
        {
            a = "Even integers Found!";
        }
        else
        {
            b = "Odd integers Found";
        }
    }

    if(a.empty())
    {
        cout<<"No Even integers Found!"<<endl<<endl;
        if(b.empty())
        {
            cout<<"No Odd integers Found!"<<endl<<endl;
        }
        else
        {
            cout<<b<<endl;
            cout<<"The Odd integers are: "<<endl;
            for(int i=1; i<=10; i++)
            {
                if(arr[i]%2==1)
                {
                    cout<<arr[i]<<" ";
                }
            }
        }
    }
    else
    {
        cout<<a<<endl;
        cout<<"The Even integers are: "<<endl;
        for(int i=1; i<=10; i++)
        {
            if(arr[i]%2==0)
            {
                cout<<arr[i]<<" ";
            }
        }
        cout<<endl<<endl;
        if(b.empty())
        {
            cout<<"No Odd integers Found!"<<endl<<endl;
        }
        else
        {
            cout<<b<<endl;
            cout<<"The Odd integers are: "<<endl;
            for(int i=1; i<=10; i++)
            {
                if(arr[i]%2==1)
                {
                    cout<<arr[i]<<" ";
                }
            }
        }
    }
    cout<<endl<<endl;


    return 0;
}

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

int main(){
    int arr[10]={};
  
    cout<<"Input 10 integers in array: "<<endl;
    for(int i=1; i<=10; i++){
        cout<<"arr["<<i<<"]: ";
        cin>>arr[i];
    }
    cout<<endl;
  
    for(int i=1; i<=10; i++){
        if(arr[i]%2==0){
            for(int j=1; j<=10; j++){
                if(arr[j]%2==1){
                    goto both;
                }
            }
        }
    }

    for(int i=1; i<=10; i++){
        if(arr[i]%2==0){
            for(int j=1; j<=10; j++){
                if(arr[j]%2==0){
                    goto even;
                }
            }
        }
    }
  
    for(int i=1; i<=10; i++){
        if(arr[i]%2==1){
            for(int j=1; j<=10; j++){
                if(arr[j]%2==1){
                    goto odd;
                }
            }
        }
    }
  
    even:
    cout<<"Even integers Found!"<<endl;
    cout<<"The Even integers are: "<<endl;
    for(int i=1; i<=10; i++){
        if(arr[i]%2==0)
            cout<<arr[i]<<" ";
    }
    cout<<endl<<endl;
    cout<<"No Odd integers Found!";
    goto exit;
  
    odd:
    cout<<"No Even integers Found!"<<endl;
    cout<<endl;
    cout<<"Odd integers Found!"<<endl;
    cout<<"The integers are: "<<endl;
    for(int i=1; i<=10; i++){
        if(arr[i]%2==1)
            cout<<arr[i]<<" ";
    }
    goto exit;
  
    both:
    cout<<"Even integers Found!"<<endl;
    cout<<"The Even integers are: "<<endl;
    for(int i=1; i<=10; i++){
        if(arr[i]%2==0)
            cout<<arr[i]<<" ";
    }
    cout<<endl<<endl;
    cout<<"Odd integers Found!"<<endl;
    cout<<"The Odd integers are: "<<endl;
    for(int i=1; i<=10; i++){
        if(arr[i]%2==1)
            cout<<arr[i]<<" ";
    }
  
    exit:
    return 0;
}
 
Last edited:

Similar threads

Back
Top