Ads

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?

Ads

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

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

Ads

Related Tutorials/Questions & Answers: