Write a C++ program that accepts three numbers from the user (All are integers): Such as:

Enter the first mark: 3

Enter the second mark: 25Enter the third mark: 23

The program should find and print the average of the odd numbers greater than 5 and less than 100.

this is my code :

#include <vcl.h>

#include "MT262io.h"

#pragma hdrstop//---------------------------------------------------------------------------

#pragma argsused

int main(int argc, char* argv[])

{

int firstNum;

int secondNum;

int thirdNum;

int average;

firstNum=ReadIntPr(" enter the first number");

secondNum=ReadIntPr(" enter the second number");

thirdNum=ReadIntPr(" enter the third number");

average=(firstNum+secondNum+thirdNum/3);while((average >5) && (average <100)) {

if(firstNum % 2 != 0)&&(secondNum % 2 != 0)&& (thirdNum % 2 != 0) {

WriteIntPr("the average of the odd numbers is"+average);

}

getchar();return 0;

}

//---------------------------------------------------------------------------thank you

Sounds like you've misunderstood the question. I think the test of > 5 and < 100 applies to the entered numbers (any numbers not satisfying these conditions do not get used in the calculation of the average.) By the way, your program is Borland compiler-specific and not portable C++ (which may or may not be an issue.)

In fact i agree with you . But i have question?

the condition: odd numbers

how can we make it in codes?and i have another question" I'm sorry :$"

I didn't understand this phrase:

your program is Borland compiler-specific and not portable C++ (which may or may not be an issue.)what you mean by it?

regards,

An odd number gives a remainder of 1 when you divide it by 2. Since C++ has a "remainder" operator, it could be useful here. The following are not standard C++ :

#include <vcl.h> - no such header.

#pragma hdrstop - not recognised.

WriteIntPr - no such function (unless it's one you've written yourself.)So this application will only run if you compile it with the Borland compiler. If you want to write an application that can be compiled by any standard C++ compiler, then you need to remove the above and replace them with standard equivalents.

aha , now i get it

it is Ok.so in this program we will use "for loop" for the rule of odd numbers and inside it we have to use "if condition" to put the condition of limited numbers( <100 and >5) , right?

thank you so much for your help.

Perhaps this link will be helpful.......

http://www.users.totalise.co.uk/~ri...i guess you're not linking to library. Perhaps you are missing this line "USELIB("MT262io.lib");"

.....................................

_Adnan_

Now I'm beginning to doubt myself. I'm not sure I've understood the question. Does the question mean calculate the average of ALL the odd numbers greater than 5 and less than 100? I.e. 7,9,11,13,...,95,97,99 ? (The answer is 2491)

Or does it mean calculate the average of any of the three entered numbers that satisfy the condition of being odd and >5 and < 100. For example:

If the user enters 7, 13, 25 the answer is (7 + 13 + 25) / 3 == 15.

If the user enters 5, 13, 25 the answer is (13+25) / 2 == 19 (don't count the 5).

If the user enters 7, 14, 98 the answer is 7 (the others are even so don't count.)

Then what if the user enters 6,8,10? None of these are eligible and you can't calculate the average of an empty set.

#include <vcl.h>

#include "MT262io.h"

#pragma hdrstop

#pragma argsusedint main(int argc, char* argv[])

{

int firstNum;

int secondNum;

int thirdNum;

double average;

int tempSum =0;

int count=0;

firstNum=ReadIntPr(" enter the first number");

secondNum=ReadIntPr(" enter the second number");

thirdNum=ReadIntPr(" enter the third number");

if((firstNum % 2 != 0) && (firstNum > 5) && (firstNum <100))

{

tempSum += firstNum;

count++;

}

if((secondNum % 2 != 0) && (secondNum > 5) && (secondNum <100))

{

tempSum += secondNum;

count++;

}

if((thirdNum % 2 != 0) && (thirdNum > 5) && (thirdNum <100))

{

tempSum += thirdNum;

count++;

}

if(count != 0)

{

average = tempSum / count;

WriteIntPr("the average of the odd numbers is"+average);

}

else

{

WriteIntPr("There were no odd integers between 5 and 100");

}

getchar();return 0;

}

Im not familiar with "writeintpr" so I cant help you with that, but I changed the average variable to be a double, which will allow decimals rather than integral numbers. Also, I added a protection against "divide by zero", because if none of your entered values are between 5 and 100, and none of them are odd, you will be dividing by zero, which will crash your program.

Thank you so much adnan.

i understand a question well and i compile my code and its ok so if you need any help you can ask?

Ask Your Question

Weekly Poll

Would you ride in a self-driving car from Tesla?

Discuss in The Lounge

Poll History