public class Main { public static void main(String args[]) { int x=1,n=5,sum=0; if(n%2==0) { do { if(x%2==0) { sum=sum+x; } x++; }while(x<=n); } else { do { if(x%2!=0) { sum=sum+x; } x++; }while(x<=n); } System.out.print(sum); } }
9
public class Main { public static void main(String args[]) { int x=1,n,sum=0; n=Integer.parseInt(args[0]); if(n%2==0) { do { if(x%2==0) { sum=sum+x; } x++; }while(x<=n); } else { do { if(x%2!=0) { sum=sum+x; } x++; }while(x<=n); } System.out.print(sum); } }
9
import java.util.*; public class Main { public static void main(String args[]) { int x=1,n,sum=0; Scanner s=new Scanner(System.in); System.out.println("Enter n value : "); n=s.nextInt(); if(n%2==0) { do { if(x%2==0) { sum=sum+x; } x++; }while(x<=n); } else { do { if(x%2!=0) { sum=sum+x; } x++; }while(x<=n); } System.out.print(sum); } }
Enter n value : 5
9