Arrays Questions
Arrays Questions
Write a program to create an integer array of 10 elements and print all the prime
elements.
import java.util.*;
class Prime
{
public void print()
{
Scanner sc= new Scanner(System.in);
int a[]=new int[10];//Array declaration
for(int i=0;i<10;i++)
{
System.out.println("Enter a number:");
a[i]=sc.nextInt(); // storing element
}
System.out.println("Prime numbers are:");
int c=0;
for(int i=0;i<10;i++)
{
for(int j=1;j<=a[i];j++)
{
if(a[i]%j==0)
c++; //counting number of factors
}
if(c==2)//if two factors then prime , print it
{
System.out.println(a[i]);
}
c=0;//setting to 0 to count the factors of of the next number
}
}
}
Question 2:
Write a program to store N numbers in an array and display all the numbers ends with 6.
import java.util.*;
class Number
{
public void print()
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter number of elements:");
int N=sc.nextInt();//accepting size of the array
int a[]=new int[N];//Array declaration
for(int i=0;i<N;i++)
{
System.out.println("Enter a number:");
a[i]=sc.nextInt(); // storing element
}
System.out.println("Numbers ends with 6 are :");
int d=0;
for(int i=0;i<N;i++)
{
d=a[i]%10;//extracting last digit
if(d==6) //if the last digit is 6 then print the number
System.out.println(a[i]);
}
}
}
Question 3:
Write a program to store 20 numbers in an array and display the numbers having even
number of digits.
import java.util.*;
class EvenDigits
{
public void print()
{
Scanner sc= new Scanner(System.in);
int a[]=new int[20];//Array declaration
for(int i=0;i<20;i++)
{
System.out.println("Enter a number:");
a[i]=sc.nextInt(); // storing element
}
System.out.println("Numbers having Even number of digits:");
int d=0,n=0;
for(int i=0;i<20;i++)
{
n=a[i];//duplicate of the element
while(n>0)
{
d++;//counting number of digits
n=n/10;
}
if(d%2==0)//if number of digits even the print the number
System.out.println(a[i]);
d=0;//set d to 0 count the digits of the next number
}
}
}
Question 4:
Write a program to store 20 numbers in an array and display the reverse of each
numbers.
import java.util.*;
class Reverse
{
public void print()
{
Scanner sc= new Scanner(System.in);
int a[]=new int[20];//Array declaration
for(int i=0;i<20;i++)
{
System.out.println("Enter a number:");
a[i]=sc.nextInt(); // storing element
}
System.out.println("Number\t\tReverse");
int rev=0,d=0,n=0;
for(int i=0;i<20;i++)
{
n=a[i];//duplicate of the element
while(n>0)
{
d=n%10;//Extracting last digit
rev=rev*10+d;//calculating reverse of the number
n=n/10;
}
System.out.println(a[i]+"\t\t"+rev);
rev=0;//set rev to 0 to calculate the reverse of the next number
}
}
}
Question 4:
Write a program to store 10 numbers in an array and display the sum of digits of each
number.
import java.util.*;
class Sum
{
public void print()
{
Scanner sc= new Scanner(System.in);
int a[]=new int[10];//Array declaration
for(int i=0;i<10;i++)
{
System.out.println("Enter a number:");
a[i]=sc.nextInt(); // storing element
}
System.out.println("Number\t\tSum of digits");
int sum=0,d=0,n=0;
for(int i=0;i<10;i++)
{
n=a[i];//duplicate of the element
while(n>0)
{
d=n%10;//Extracting last digit
sum=sum+d;//calculating sum of the number
n=n/10;
}
System.out.println(a[i]+"\t\t"+sum);//printing the number and the sum of digits
sum=0;//set sum to 0 to calculate the sum of the next number
}
}
}
Question 5:
Write a program to store 15 numbers in a double array. Print only the integer part of all
numbers without decimal points.
import java.util.*;
class DoubleToInt
{
public void print()
{
Scanner sc= new Scanner(System.in);
double a[]=new double[15];//Array declaration
for(int i=0;i<15;i++)
{
System.out.println("Enter a number:");
a[i]=sc.nextDouble(); // storing element
}
System.out.println("Number\t\tInteger");
int n=0;
for(int i=0;i<15;i++)
{
n=(int)a[i];//converting double to int
System.out.println(a[i]+"\t\t"+n);
}
}
}