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

Dani AI

Generated

Brief, practical fixes for the numbered items in this thread. was right that digits must be handled individually, and 's suggestion to put repeated logic into functions is a good habit. Below are compact, correct examples and small notes to avoid the common mistakes seen in the earlier posts.

For counting even/odd digits: treat the input as text and iterate characters. This handles multi-digit input, leading zeros, and avoids indexing mistakes.

#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string s;
    if (!(std::cin >> s)) return 0;
    int even = 0, odd = 0;
    for (char ch : s) {
        if (!std::isdigit(static_cast<unsigned char>(ch))) continue;
        int d = ch - '0';
        if (d % 2 == 0) ++even; else ++odd;
    }
    std::cout << "even=" << even << " odd=" << odd << '\n';
}

To convert seconds into hours/minutes/seconds use integer division and remainder. Validate input (negative values) if needed.

#include <iostream>

int main() {
    long long total;
    if (!(std::cin >> total)) return 0;
    bool neg = total < 0;
    if (neg) total = -total;
    long long hours = total / 3600;
    long long minutes = (total % 3600) / 60;
    long long seconds = total % 60;
    if (neg) std::cout << '-';
    std::cout << hours << "h " << minutes << "m " << seconds << "s\n";
}

Common loop mistakes (Q3, Q4 and the name search): inner-loop conditions must use the inner index, for-loop bounds should be i < 10 to read ten items, and when searching an array print a single result by using a found flag and break. Example patterns:

/* decreasing star pattern */
for (int i = n; i >= 1; --i) {
    for (int j = 0; j < i; ++j) std::cout << '*';
    std::cout << '\n';
}

/* average of 10 numbers */
double sum = 0, x;
for (int i = 0; i < 10; ++i) { std::cin >> x; sum += x; }
std::cout << "Average: " << (sum / 10.0) << '\n';

/* name search */
bool found = false;
for (int i = 0; i < 5; ++i) {
    if (search == name[i]) { std::cout << search << '\n'; found = true; break; }
}
if (!found) std::cout << "no result\n";

Troubleshooting tips: test edge cases (zero, negatives, non-digit input), prefer small helper functions for input/validation, and run simple manual traces of your loops to find off-by-one or wrong-variable bugs.

Recommended Answers

All 7 Replies

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.

ok thanks, what about the other questions

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

commented: Bumping is antisocial and very rude. -4

>up
Bumping is rude. Now I'll likely ignore all of your subsequent threads.

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

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.