Please Help-Problem with program

Can someone please help me with the following program.

I got a question for homework and i tried to make a program but this give the wrong output.

The question is: Write a program to accept the age of employess and count the number of persons in the following age group.
(i)26 to 25 (ii)36 to 45 (iii)46 to 55

I run the program and input the ages but at the end the answer comes out to be wrong. For e.g.:

If i take the number of employees to be 3,and take the
age of employee 1:27
age of employee 2:37
age of employee 3:47

Then the output comes as:-
Number of employees between the age 26 & 35=3
Number of employees between the age 36 & 45=0
Number of employees between the age 46 & 55=0


The program i tried is:-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n,i,age[100],count=0,counta=0,countb=0;
cout<<"Enter the number of employees working for the company: ";
cin>>n;
	for(i=0;i<n;i++)
	{
	cout<<"Enter the age of employee "<<i+1<<": ";
	cin>>age[i];
	}
	for(i=0;i<n;i++)
	{
		if(age[i]<35||age[i]>26)
		{
		count=count+1;
		}
		else if(age[i]<45||age[i]>36)
		{
		counta=counta+1;
		}
		else if(age[i]<55||age[i]>46)
		{
		countb=countb+1;
		}
	}
cout<<"The number of employees between the age of 26 & 35 are: "<<count<<endl;
cout<<"The number of employees between the age of 36 & 45 are: "<<counta<<endl;
cout<<"The number of employees between the age of 46 & 55 are: "<<countb;
getch();
}

Please tell me where have i gone wrong.
Last edited on
[ code ] and [ /code ] are the code brackets we use. Apparently not knowing that, however, I applaud your commendable attempt to make the code easier to read.

Basically, everything is right except for one thing. You're testing with the logical OR || operator, which makes the first loop if statement ALWAYS true, because all numbers that exist are always either larger than 26 or smaller than 35!

In order to make this work, all you have to do is change to the logical AND && operator.
Last edited on
Also, ++count; does the same thing as count = count + 1;
Thank you so much. :)
I changed it and got it right.
Topic archived. No new replies allowed.