0% found this document useful (0 votes)
24 views

Arra Programming

The document contains 10 Java programs that perform various operations on arrays such as storing and printing array values, calculating the sum of array elements, copying arrays, finding sums of even/odd numbers, prime numbers, palindrome numbers, and counting positive, negative and zero values in an array. Each program imports Java utilities, declares and initializes an integer array, gets user input to populate the array using a scanner, then uses control flow statements like for loops to manipulate and output results from the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Arra Programming

The document contains 10 Java programs that perform various operations on arrays such as storing and printing array values, calculating the sum of array elements, copying arrays, finding sums of even/odd numbers, prime numbers, palindrome numbers, and counting positive, negative and zero values in an array. Each program imports Java utilities, declares and initializes an integer array, gets user input to populate the array using a scanner, then uses control flow statements like for loops to manipulate and output results from the array.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

// store the value in array & print them

import java.util.*;
class ex_1
{
public static void main(String args[])
{
int a[]=new int[5],i;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
System.out.println("Printing the array....");
for(i=0;i<5;i++)
{
System.out.print(a[i]+" ");
}
}
}

// sum of the array elements

import java.util.*;
class ex_2
{
public static void main(String args[])
{
int a[]=new int[5],i,s=0;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
for(i=0;i<5;i++)
{
s=s+a[i];
}
System.out.println("The sum result is "+s);
}
}

// copy the array elements to another array

import java.util.*;
class ex_3
{
public static void main(String args[])
{
int a[]=new int[5],i;
int b[]=new int[5];
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
System.out.println("Printing the duplicate array..");
for(i=0;i<5;i++)
{
b[i]=a[i];
System.out.print(b[i]+" ");
}
}
}

// copy the array elements in reverse form to another array

import java.util.*;
class ex_4
{
public static void main(String args[])
{
int a[]=new int[5],i,j=0;
int b[]=new int[5];
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
System.out.println("Printing the reverse duplicate array..");
for(i=4;i>=0;i--)
{
b[j]=a[i];
}
for(i=0;i<5;i++)
{
System.out.print(b[i]+" ");
}
}
}

// find the sum of even numbers & odd numbers in an array

import java.util.*;
class ex_5
{
public static void main(String args[])
{
int a[]=new int[5],i,es=0,os=0;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
for(i=0;i<5;i++)
{
if(a[i]%2==0)
es=es+a[i];
else
os=os+a[i];
}
System.out.println("Sum of even numbers "+es);
System.out.println("Sum of odd numbers "+os);
}
}

// find the sum of three digits & two digits numbers separately

import java.util.*;
class ex_6
{
public static void main(String args[])
{
int a[]=new int[5],i,s1=0,s2=0;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
for(i=0;i<5;i++)
{
if(a[i]>=10 && a[i]<=99)
s1=s1+a[i];
if(a[i]>=100 && a[i]<=999)
s2=s2+a[i];
}
System.out.println("Sum of two digit numbers "+s1);
System.out.println("Sum of three digit numbers "+s2);
}
}

// find the sum of all prime numbers in an array

import java.util.*;
class ex_7
{
public static void main(String args[])
{
int a[]=new int[5],i,s=0,c=0,r,j;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
for(i=0;i<5;i++)
{
c=0;
for(j=2;j<a[i];j++)
{
r=a[i]%j;
if(r==0)
c++;
}
if(c==0)
s=s+a[i];
}
System.out.println("Sum of prime numbers "+s);
}
}

// find the sum of all pallindrome numbers in an array

import java.util.*;
class ex_8
{
public static void main(String args[])
{
int a[]=new int[5],i,s=0,c=0,r,j,ps=0;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
for(i=0;i<5;i++)
{
c=a[i];
while(c>0)
{
j=c%10;
s=s*10+j;
c=c/10;
}
if(a[i]==s)
ps=ps+a[i];
}
System.out.println("Sum of pallindrome numbers "+ps);
}
}

// find the sum of all armstrong numbers in an array

import java.util.*;
class ex_9
{
public static void main(String args[])
{
int a[]=new int[5],i,s=0,c=0,r,j,as=0;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
for(i=0;i<5;i++)
{
c=a[i];
while(c>0)
{
j=c%10;
s=s+(j*j*j);
c=c/10;
}
if(a[i]==s)
as=as+a[i];
}
System.out.println("Sum of pallindrome numbers "+as);
}
}

// find total positive negetive & zeros in an array

import java.util.*;
class ex_10
{
public static void main(String args[])
{
int a[]=new int[5],i,p=0,n=0,z=0;
Scanner sc=new Scanner(System.in);
for(i=0;i<5;i++)
{
System.out.print("Enter a number : ");
a[i]=sc.nextInt();
}
for(i=0;i<5;i++)
{
if(a[i]>0)
p++;
if(a[i]<0)
n++;
if(a[i]==0)
z++;
}
System.out.println("Total positive numbers "+p);
System.out.println("Total negetive numbers "+n);
System.out.println("Total zeros numbers "+z);
}
}

You might also like