Advertisement
Hiteshw11

Sum of Odd And Even Numbers in an Array

Jul 14th, 2020
1,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. public class trial {
  2.  
  3.     public static void main(String args[])
  4.     {
  5.         Scanner src=new Scanner(System.in);
  6.         System.out.println("Enter the size of array");
  7.         int n=src.nextInt();
  8.         int a[]=new int[n];
  9.         int esum=0;
  10.         int osum=0;
  11.         for(int i=0;i<n;i++)
  12.         {
  13.         System.out.println("Enter the number");    
  14.         a[i]=src.nextInt();
  15.         }
  16.         System.out.println("The entered array is");
  17.         for(int j=0;j<n;j++)
  18.         {
  19.             System.out.println(a[j]);
  20.         }
  21.         System.out.println("Even numbers in the array are");
  22.         for(int k=0;k<n;k++)
  23.         {
  24.             if (a[k]%2==0)
  25.             {
  26.                 System.out.println(a[k]);
  27.                 esum=esum+a[k];
  28.             }
  29.         }
  30.         System.out.println("Odd numbers in the array are");
  31.         for(int m=0;m<n;m++)
  32.         {
  33.             if (a[m]%2!=0)
  34.             {
  35.                 System.out.println(a[m]);
  36.                 osum=osum + a[m];  // osum Awesome hehehe
  37.             }
  38.         }
  39.         System.out.println("The sum of even numbers is "+esum);
  40.         System.out.println("The sum of odd numbers is "+osum);
  41.     }
  42.    
  43.    
  44.    
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement