Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners How to program an application for?

Have Programming Question? Ask it here!
 
 
 


Jasmine
How to program an application for?
1 Answer(s)      4 years and 2 months ago
Posted in : Java Beginners


How do you write an application to count the number of both odd and even integers of several positive integerss? Then the final display is the total of how many odd numbers were entered and the total of how many even numbers were entered?

Advertisement
View Answers

May 27, 2010 at 10:57 AM


Hi Friend,

Try the following code:

import java.util.*;

class CountEvenAndOdd{
public static void main(String[] args){
Scanner input=new Scanner(System.in);
System.out.println("Enter 15 numbers");
int even=0,odd=0;
for(int i=1;i<=15;i++){
int num=input.nextInt();
if(num%2==0){
even++;
}
else{
odd++;
}
}
System.out.println("Number of Odds= "+odd);
System.out.println("Number of evens= "+even);
}
}

Thanks


Related Tutorials/Questions & Answers:
Advertisements
Advertisements
 

 

 

DMCA.com