TechArena Community

TechArena Community (http://forums.techarena.in/)
-   Software Development (http://forums.techarena.in/software-development/)
-   -   Loop counting odd and even numbers (http://forums.techarena.in/software-development/1261963.htm)

Allan.d 23-10-2009 11:33 AM

Loop counting odd and even numbers
 
I want a way (a code in c or c ++) to display the result of given number,The best way to see if a given number is odd or even. Any recommendations and suggestions are appreciated.

Zecho 23-10-2009 11:36 AM

Re: Loop counting odd and even numbers
 
Code:

int main()
{
int in, evn=0, od=0;
do
{
cout << "Please enter a number:"<< ' ';
cin>> input;
if ( (evn%2 == 0) && (in >= 0) )
  even++;
else if (in >= 0)
  od++;
}
while (in>=0);
 
cout << "You have entered" << ' '<< od << ' ' <<"odd numbers." << endl;
cout << "And" << ' ' << evn << ' ' <<"even numbers." << endl;
system("PAUSE");
return 0;
}


Walby 23-10-2009 11:39 AM

Re: Loop counting odd and even numbers
 
Code:

long oddno =1;
 long maxno;
 unsigned long lproduct;
 cout<<"enter the odd number";
 cin >> maxno;
 while( oddno<= maxno )
 {
      long tempno = oddno + 2;
      lpro = tempno *oddno;
      cout<<"the product of two numbers first number"<<oddno<<"second number"<<tempno<<"equals"<<lpro<<"\n";
      oddno = tempno;
}


Aberto 23-10-2009 11:40 AM

Re: Loop counting odd and even numbers
 
Use the AND operation.
Code:

for(int i=0;i<=100;i++)
{
if(i&1)
{
printf("Odd\n");
}
else
{
printf("Even\n");
}
}



All times are GMT +5.5. The time now is 10:18 AM.