hey guys i was solving a question but there is something which is not working properly.. I'll provide you guys with question statement and my code below.The problem is occuring after the loop like if the avg is more then 0 still it shows the output massege as "terminating". Can anyone please pin point the mistake and help me out with the problem. Urgent..!!
Your concern will highly be apreciated.. THANKYOU :)

QUESTION STATEMENT:
"Write a program that reads N numbers from the user and computes the average of only those numbers that are an even multiple of 3 and have 2 or 3 digits in them but are not divisible by 8. Also your program should terminate by displaying an error message if a negative number is entered."

MY CODE:

#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
    int limit;
    cout<<" Please enter total number of entries you want to be processed.."<<endl;
    cin>>limit;
    int num; 
    int sum = 0;
    double avg;
    for(int i = 0; i<limit; i++)
    {
        cout<<"\n Please enter the number.."<<endl;
        cin>>num;
        if(num<0){
            cout<<"Invalid input.. Please try again"<<endl;
            break;
        }else if((num % 3 == 0) && (num % 2 == 0) && (num % 8 != 0)){
            sum = sum + num;

        }

    }
    avg = sum / limit;
    if(avg<= 0){
        cout<<"terminating..!!"<<endl;
    }else if(avg <= 999){
        cout<<"The average of all the numbers is:\t"<<avg;
    }else{
        cout<<"The answer is bigger than the required condition"<<endl;
    }

    getch();
    return 0;
}

Recommended Answers

All 3 Replies

is not working properly...

Is it possible to mention WHAT is not working properly?

Your condition for accepting a number as part of your calculation is wrong. You are accepting it if:

it is a mulitple of three AND it is a multiple of two && it's not a multiple of eight

You've left out the check for having two or three digits, you've added a check for it being a multiple of two, and your check for it being a multiple of three is meant to check that it's an even multiple of three (i.e. it is two times three, or four times three, or six times three, and so on).

Your code also does not terminate straight away if the user enters a negative number.

Moschops what will be the exact condition then..?? can you please provide me with the proper condition and code..
Thanks and regards.:)

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.