hi guys,

i have some questions here

Q1- i want to write programme to count the even and oss numbers

example: the user input is 12345
so: even= 2 , odd = 3

this is my code

int even[5]={0,2,4,6,8};
	int odd[5]={1,3,5,7,9};
	int n[10];
	int count_even = 0 ,count_odd = 0;
		
		for(int j=0; j < 10; j++)
		{
			cin>>n[j];
			
			for(int i=0; i < 5; i++)
			{
				
				if(n[j] == even[i]) count_even++;
				if(n[j] == odd[i])  count_odd++;
				
			}
						
		}
		cout << count_even << " " << count_odd;

can you show me what was my mistake?

****

Q2- i want to write a programme to ask the user to input the seconds, then convert it to hours,numutes,seconds

but i don't have an idea how to do it

can you show me the logic?

****

Q3- ask uesr to input a number then display this

example: input = 5

output: *****
****
***
**
*

this is my code

int n;
		cin >> n;
		for(int i = 0; i <= n; i++)
		{
			for(int j=0; i <=n; j++){
			cout << "*";
			n--;
			}
			
			
		}

****

Q4- ask user for ten numbers and then dsiplay the average

int numbers[10];
	int sum = 0;
	double average = 0;
	
	cout << "Enter 10 numbers one by one: " << endl;
	
	
	for(int i = 0; i > 10; i++){
	cin >> numbers[i];
	}
	int i = 0;
	while (i < 10)
	{
		sum += numbers[i];
		

		i++;
	}
	
	// Calculate the average of the 10 numbers.
	average = sum / 10.0;
	
	cout << "The average is " << average << endl;

i really don't know what is the wrong in my code? especially the to get ten numbers

i can do it by using while loop but i want to use for loop

waitng for you

Q1: That doesn't work because what you have to do is extract each of the digits from an integer. If you enter 1234 then you have to count each digit 1, 2, 3, and 4.

Use the % (mod) operator and / to do that. This isn't the complete code -- you will have to put it in a loop.

int x = 1234;
int odd = 0, even = 0;
int n;

n = x % 10;
if( n % 2 == 0)
   even++;
else
   odd++;
x /= 10;

Q2: You know that there are 60 seconds in a minute, and 60 minutes in an hour, and 24 hours in a day. If you have 1000 seconds then its simple 5th grade math how to convert it to days, hours, minutes and seconds. If you don't know how to do it then you need to take a remedial math course.

Make a function that inputs a number and returns a valid result.
Then Call that function in all "ask the user" logic

Make another one for displaying numbers to avoid duplicate codes.
Other like converting so-to-so are math issue

up

Edited 6 Years Ago by kuen: n/a

Comments
Bumping is antisocial and very rude.
string name[5];
	
	for(int i=0; i < 5; i++){
	cin >> name[i];
	}
	
	string search;
	cin >> search;
		for(int i=0; i < 5; i++)
		{
			if(search == name){
				cout << search;
			}else if(search != name){
				cout <<"no result";
			}
		}

how to output only for one time?

ny way to output by using array anf for loop is inclorrect

narue,

i post a replay but i edited because i found the solving

anyway, i really don't want you to help me understand how solve problems

Edited 6 Years Ago by kuen: n/a

This article has been dead for over six months. Start a new discussion instead.