Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/*. An Evens number is an integer whose digits are all even. For example 2426 is an Evens number but 3224 is not.

Write a function named isEvens that returns 1 if its integer argument is 

an Evens number otherwise it returns 0.

The function signature is
   int isEvens (int n)
*/
import java.util.Scanner;
public class Evenoroddnumber {
	
	int status,n,k,evencount=0,oddcount=0,temp=0;
	
	int isEvens (int n)
	{System.out.println("number entered from main is"+n);
		if(n<0)
		{
			System.out.println("number is negative");
			return 0;
		}
		else if(n>0)
		{System.out.println("number is positive");
			temp=n%10;
			System.out.println("reminder is"+temp);
			if(temp%2==0)
			{
				System.out.println("reminder is even"+temp);
				evencount++;
				System.out.println("count even number"+evencount);
				n=n/10;
				System.out.println("number devided by 10 is"+n);
			}
			else 
			{
				System.out.println("reminder is odd"+temp);	
				oddcount++;
			}
		}
		if(oddcount!=0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}

	public static void main(String args[])
	{
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();		
		Evenoroddnumber e1=	new Evenoroddnumber();
		int x=e1.isEvens(n);
		
		
	}

}


What I have tried:

/*. An Evens number is an integer whose digits are all even. For example 2426 is an Evens number but 3224 is not.

Write a function named isEvens that returns 1 if its integer argument is 

an Evens number otherwise it returns 0.

The function signature is
   int isEvens (int n)
*/
import java.util.Scanner;
public class Evenoroddnumber {
	
	int status,n,k,evencount=0,oddcount=0,temp=0;
	
	int isEvens (int n)
	{System.out.println("number entered from main is"+n);
		if(n<0)
		{
			System.out.println("number is negative");
			return 0;
		}
		else if(n>0)
		{System.out.println("number is positive");
			temp=n%10;
			System.out.println("reminder is"+temp);
			if(temp%2==0)
			{
				System.out.println("reminder is even"+temp);
				evencount++;
				System.out.println("count even number"+evencount);
				n=n/10;
				System.out.println("number devided by 10 is"+n);
			}
			else 
			{
				System.out.println("reminder is odd"+temp);	
				oddcount++;
			}
		}
		if(oddcount!=0)
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}

	public static void main(String args[])
	{
		Scanner s=new Scanner(System.in);
		int n=s.nextInt();		
		Evenoroddnumber e1=	new Evenoroddnumber();
		int x=e1.isEvens(n);
		
		
	}

}
Posted
Updated 21-Feb-18 4:52am

You say that the loop is executing only once, but you don't have a loop.
In your main method try putting a loop around the statements where you read the next number and test if it is even. The loop should end when you run out of input.
 
Share this answer
 
Comments
Member 13689256 21-Feb-18 10:43am    
i am new in programming. would you mind to write a program for mentioned question?
phil.o 21-Feb-18 10:45am    
I don't think anyone will be ok to do your homework.
Member 13689256 21-Feb-18 10:48am    
i am asking for help.
BillW33 21-Feb-18 14:44pm    
@Member 13689256: We provide help for you to figure out how to do your work. We are not here to do your work for you.
Member 13689256 21-Feb-18 10:45am    
if i entered 246, first it have to check for 6, then for 4, then for 2, if all are even, pddcount is zero and number is iseven? but this program it is checking for 6 only
As BillW33 has said: you don't have a loop.
And you need on inside your Evenoroddnumber function as well as in your main - the main loop allows you to process multiple numbers, and the one you need inside Evenoroddnumber needs to be there so as to process every digit in the user input.
Basically, you need to say:
1) While number > 0
1.1) Get least significant digit into a variable called digit
1.2) If digit is odd, return false.
1.3) Otherwise, divide number by ten and go round the loop again
2) Return true
 
Share this answer
 
Comments
Maciej Los 21-Feb-18 11:25am    
5ed!
To construct an algorithm, best way is write it down on a paper first; that is, do it by hand before trying to make a computer do it.

A pseudo-code for your task could be

while n is greater than zero
   if n is odd, return 0
   divide n by 10
return 1

You do not need to count the number of even numbers. Neither do you need all these global variables.
Now try to translate that pseudo code to java, using your course and asking your teacher if there is something you do not understand.
 
Share this answer
 
Comments
Maciej Los 21-Feb-18 11:25am    
5ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900