Sunday 27 January 2013

Write a program to insert, traverse, sum of all integer numbers, sum of all even numbers and sum of all odd numbers in array


#include <iostream.h>
#include<conio.h>
int main()
{
            int i,n, sum=0,even=0 ,odd=0;
           int a[20] ;
            cout<<"How many elemnt do you want:--"<<endl;
            cin>>n;

              for (i=0; i<n; i++)
            {
             cout<<"Enter the value in array:-"<<endl;
             cin>>a[i];
            }
          cout<<"Element is:--"<<endl;
            for (i=0; i<n ;i++)
            {
                        cout<<a[i]<<endl;
            }
            cout<<"sum of all integer numbers:-"<<endl ;

            for (i=0; i<n ;i++)
            {
            sum=sum+a[i];
            }
            cout<<sum<<endl;
            for (i=0; i<n ;i++)
            {
            if(a[i]%2==0)
            {
even=even+a[i];
            }
            else
            {
            odd=odd+a[i];
            }
        }
                        cout<<"sum of all even numbers :-"<<endl ;
                        cout<<even<<endl;
                        cout<<"sum of all odd numbers :-"<<endl ;
                        cout<<odd<<endl;
getch();  
}

No comments:

Post a Comment