Little bit confuse on how can I turn this if else statement to a do while statement.
Need some help..

#include<iostream>
using namespace std;
int main() 
{
    int div1,div2,rem,num;
    int count1=0,count2=0,sum1=0,sum2=0;
    system("cls");
    cout<<"Enter divisors : ";
    cin>>div1>>div2;
    cout<<"Enter remainder to search : ";
    cin>>rem;
    cout<<"Enter number 1 : ";
    cin>>num;
    if (num % div1==rem || num % div2== rem)
    {
       count1+=1;
       sum1+=num;
    }
    else
    {
       count2+=1;
       sum2+=num;
    }
    cout<<"Enter number 2 : ";
    cin>>num;
    if (num % div1==rem || num % div2== rem)
    {
       count1+=1;
       sum1+=num;
    }
    else
    {
       count2+=1;
       sum2+=num;
    }
    cout<<"Enter number 3 : ";
    cin>>num;
    if (num % div1==rem || num % div2== rem)
    {
       count1+=1;
       sum1+=num;
    }
    else
    {
       count2+=1;
       sum2+=num;
    }
    cout<<"Enter number 4 : ";
    cin>>num;
    if (num % div1==rem || num % div2== rem)
    {
       count1+=1;
       sum1+=num;
    }
    else
    {
       count2+=1;
       sum2+=num;
    }
    cout<<"Enter number 5 : ";
    cin>>num;
    if (num % div1==rem || num % div2== rem)
    {
       count1+=1;
       sum1+=num;
    }
    else
    {
       count2+=1;
       sum2+=num;
    }
    cout<<"Count of numbers which one/all remainder is equal to "<<rem<<" : "<<count1<<endl;
    cout<<"Sum of numbers which one/all remainder is equal to "<<rem<<" : "<<sum1<<endl;
    cout<<"Count of numbers which all remainder is not equal to "<<rem<<" : "<<count2<<endl;
    cout<<"Sum of numbers which all remainder is not equal to "<<rem<<" : "<<sum2<<endl;
    system("pause");
    return 0;
}

Need some help..

Recommended Answers

All 6 Replies

You can implement your code like this

do
{
    cout <<"\n Enter number ( 0 to exit ) ";
    cin >> num ;

    if (num % div1==rem || num % div2== rem)
    {
       count1+=1;
       sum1+=num;
    }
    else
    {
       count2+=1;
       sum2+=num;
    }

} while ( num != 0 ) ; 

Like this? got trouble because I cannot proceed to other#.. stock on "Enter Number 1:"

int div1,div2,rem, num;
    int hold=1;
    int count1=0,count2=0,sum1=0,sum2=0;
    system("cls");
    cout<<"Enter divisors : ";
    cin>>div1>>div2;
    cout<<"Enter remainder to search : ";
    cin>>rem;


    do{
       cout<<"Enter number " << hold << ":";
       cin>>num;
                if (num % div1==rem || num % div2== rem)
                {
                 count1+=1;
                 sum1+=num;
                 }
                  else
                  {
                   count2+=1;
                   sum2+=num;
              }
    } while ( hold <= 5 ) ;
            cout<<"Enter number" << hold;
            hold++;

You put the increment for 'hold' outside the loop, which means 'hold' will always equal 1 and the loop will never exit. I think you want something like this:

int div1,div2,rem, num;
int hold=1;
int count1=0,count2=0,sum1=0,sum2=0;
system("cls");
cout<<"Enter divisors : ";
cin>>div1>>div2;
cout<<"Enter remainder to search : ";
cin>>rem;
do{
    cout<<"Enter number " << hold << ":";
    cin>>num;
    if (num % div1==rem || num % div2== rem)
    {
        count1+=1;
        sum1+=num;
    }
    else
    {
        count2+=1;
        sum2+=num;
    }
    hold++; 
} while ( hold <= 5 ) ;

Thanks.. it works fine.. :D

how do i print numbers divisible by 5 and 3 only in the range from1 to 20 using do while in c++

@ atete

You dont resurrect posts like this. If this didnt solve your question. Raise a new discussion + with a question like that, you will get no answers.

Show what you have done and where the error is, you will get help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.