Write a C++ program that accepts three number

October 27, 2010 at 14:37:56
Specs: Windows XP
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: 25

Enter 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


See More: Write a C++ program that accepts three number

Report •


#1
October 28, 2010 at 00:12:05
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.)


Report •

#2
November 30, 2010 at 01:13:58
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,


Report •

#3
November 30, 2010 at 06:23:26
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.


Report •

Related Solutions

#4
November 30, 2010 at 09:40:58
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.



Report •

#5
November 30, 2010 at 09:49:45
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_


Report •

#6
November 30, 2010 at 10:52:52
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.


Report •

#7
December 3, 2010 at 20:32:58
#include <vcl.h>
#include "MT262io.h"
#pragma hdrstop
#pragma argsused

int 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;
}


Report •

#8
December 3, 2010 at 20:40:22
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.

Report •

#9
December 7, 2010 at 06:13:03
Thank you so much adnan.

Report •

#10
December 7, 2010 at 06:16:11
i understand a question well and i compile my code and its ok so if you need any help you can ask?

Report •

Ask Question