Back To Java User Defined Methods Index
//program to create two functions that prints sum of even numbers and odd numbers between two numbers a and b (a nd b are passed to functions as integer arguments ) class sumevenodd { public void sumeven(int a,int b) { int sume=0; int i; for(i=a;i<=b;i++) { if(i%2==0) { sume=sume+i; } } System.out.println("sum of even numbers between a and b is " + sume); } public void sumodd(int a,int b) { int sumo=0; int i; for(i=a;i<=b;i++) { if(i%2!=0) { sumo=sumo+i; } } System.out.println("sum of odd numbers between a and b is " + sumo); } } public class sumevenoddatob { public static void main(String[] args) { sumevenodd ob=new sumevenodd(); ob.sumeven(5,15); ob.sumodd(5,15); } }