Академический Документы
Профессиональный Документы
Культура Документы
Page No. :
ONE
DIMENSIONAL
ARRAY
PROGRAMS
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
PROGRAM 1 NUMBER OF
DAYS
Q. TO DESIGN A CLASS WITH THE FOLLOWING
SPECIFICATIONS :-
MEMBER FUNCTIONS :- void accept( ) : to accept the date, month and year
STEP 1 ; Start.
STEP 2 : Declaring class as NUMBEROFDAYS
STEP 3 : Declaring variables d, mon and year of integer type as data members.
STEP 4 : Creating main() and object ob of the class NUMBEROFDAYS to call
functions.
STEP 5 : Calling the function void accept() using the object ob
Print Enter Day , Month number and Year
Accepting the value of day in d.
Accepting the value of month in mon.
Accepting the value of year in year.
Calling the method setdata() and passing the variable d, mon and year.
End of function void accept().
STEP 6 : Start of the function void setdata(int d,int mon,int year)
tday d;
for i 0 to mon Step value 1
tday tday+dpm[i];
end for.
for i 0 to 2 Step value 1
if(year%10!=0)
break.
end if.
year year/10.
end for.
if(i==2)
if(year%400==0)
lip 1;
else if(year%4==0)
lip 1;
end if else if.
end if.
if((lip==1)&&(mon>2))
tday tday+1
end if.
Print " Total days from start of the year is :: " is tday.
End of function void setdata()
STEP 7 : End of the function void main()
STEP 8 : End.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
USAGE OF THE
VARIABLE : DATA TYPE : VARIABLE :
ob NUMBEROFDAYS Encapsulation.
To calculate and
store the total
number of days
tday int from the start of
that year till the
user inputted date.
PROGRAM 2 CHRISTIAN
GOLDBACK S PROGRAM
Q. TO DESIGN A CLASS WITH THE FOLLOWING
SPECIFICATIONS :-
user .
Every even number can be represented in the form of the sum of two prime odd numbers. This program
hence finds out that pair of odd prime numbers for the user given even number.
For example,
Input 98
Output is 93 +5
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
STEP 1 ; Start.
STEP 2 : Declaring class as Christian_goldback.
STEP 3 : Declaring variable N of integer type as data member.
STEP 4 : Creating main() and object ob of the class Christian_goldback to call
functions.
STEP 5 : Calling the function void accept()using the object ob
Print Enter an even number ::
Accepting the value in N.
End of function void accept().
STEP 6 : Calling the function void check() using the object ob
Declaring a boolean array named isprime[] of capacity N.
isprime[i] true.
end for.
if(isprime[i])
isprime[i*j] = false;
end for.
End if.
End for.
if(isprime[i])
primes++;
end if
end for.
Declaring n 0;
if(isprime[i])
list[n++] i.
End if.
End for.
Declaring left 0.
if(list[left]+list[right]==N)
break;
else if(list[left]+list[right]<N)
left++;
else
right--;
End while
if(list[left]+list[right]==N)
else
import java.util.Scanner;
public class christian_goldback
{
int N;
Scanner in = new Scanner(System.in);
public void accept() //start of method accept()
{
System.out.println("Enter an Even Number ::");
N=in.nextInt();
} //end of method accept()
public void check() //start of method check()
{
boolean isprime[] = new boolean[N];
for(int i=2;i<N;i++)
isprime[i]=true;
//determine primes < N using sieve of Eratosthenes
for(int i=2;(i*i)<N;i++)
{
if(isprime[i])
{
for(int j = i;(i*j)<N;j++)
isprime[i*j] = false;
}
}
//count primes
int primes = 0;
for(int i=2;i<N;i++)
if(isprime[i])
primes++;
System.out.println("Done Tabulating Primes.");
//store primes in the list
int list[] = new int[primes];
int n=0;
for(int i=0;i<N;i++)
if(isprime[i])
list[n++]=i;
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
USAGE OF THE
VARIABLE : DATA TYPE : VARIABLE :
ob Christian_goldback Encapsulation.
To store the even
N int number given by the
user.
PROGRAM 3 PRIME
MAXIMUM GAP PROGRAM
Q. TO DESIGN A CLASS WITH THE FOLLOWING
SPECIFICATIONS :-
MEMBER FUNCTIONS :- void accept( ) : to accept an even number from the user .
prime numbers.
This program works to find out the gap that is present between every two consequent prime numbers
within the user given limit. The program then prints the two primes for which the gap between them is
maximum within the user given limit along with the gap between them .
For example,
Input 100
Thus, the maximum gap between two primes in the range 0 to 100 includes 7 integers from 90 to 96.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
STEP 1 ; Start.
STEP 2 : Declaring class as Primegap.
STEP 3 : Declaring variable N of integer type as data member.
STEP 4 : Creating main() and object ob of the class Primegap to call functions.
STEP 5 : Calling the function void accept() using the object ob.
Print Enter N ::
Accepting the value in N.
End of function void accept().
STEP 6 : Calling the function void check() using the object ob.
Declaring a boolean array named isprime[] of capacity N.
isprime[i] true.
end for.
if(isprime[i])
isprime[i*j] = false.
end for.
End if.
End for.
if(isprime[i])
gap 0.
else
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
gap gap + 1.
end if else
if(gap>bestgap)
bestgap gap.
right i.
end if.
end for.
import java.util.Scanner;
public class Primegap
{
int N;
Scanner in = new Scanner(System.in);
public void accept() //start of method accept()
{
System.out.println("Enter N ::");
N = in.nextInt();
}
//end of method accept()
public void check() //start of method check()
{
boolean isprime[] = new boolean[N+1];
for(int i=2;i<=N;i++)
isprime[i]=true;
//determine primes < N using Sieve of Eratosthenes
for(int i=2;(i*i)<=N;i++)
{
if(isprime[i])
{
for(int j=i;(i*j)<=N;j++)
isprime[i*j] = false;
}
}
//find the longest consecutive sequence of integers with no primes
int gap = 0;
int bestgap = 0;
int right = 0;
for(int i=2;i<=N;i++)
{
if(isprime[i])
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
gap=0;
else
gap++;
if(gap>bestgap)
{
bestgap = gap; right = i;
}
}
int left = right - bestgap + 1;
System.out.println("There are no primes between " + left + " and " + right);
System.out.println("That is " + bestgap + " consecutive integers ");
} //end of method check()
public void main() //start of method main()
{
Primegap ob =new Primegap(); //declaring object of class
ob.accept();
ob.check();
} //end of method main()
} //end of class
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
USAGE OF THE
VARIABLE : DATA TYPE : VARIABLE :
ob Primegap Encapsulation.
PROGRAM 4 PALINDROME
Q. TO DESIGN A CLASS WITH THE FOLLOWING
SPECIFICATIONS :-
user .
is a palindrome or not.
STEP 1 : Start
STEP 2 : Declare a class palindrome.
STEP 3 : Declaring variable N of integer type as a data member.
STEP 4 : Creating object ob of class in main() to call relevant member functions
Print Enter the number of elements ::
Accepting the value in k
Passing value of k to parameterized constructor palindrome (int)
STEP 5 : Calling of parameterized constructor palindrome(int a)
lim a
for i 0 to lim step value is 1
ar[i] 0
n[i] 0
End of parameterized constructor palindrome(int)
STEP 6 : Calling method void input() to take the user inputs of the array elements
Print Enter the elements ::
for i 0 to lim step value is 1
ar[i] storing the array elements given as user input
End of method void input()
STEP 7 : Calling of method void check() to check whether the given number is a
Palindrome or not
t0
for i 0 to <lim step value 1
t ar[i]
reverse rev(t,1) passing and receiving value from method int rev(int,int)
sum rev(t,2) passing and receiving value from method int rev(int,int)
if (t==reverse)
Print message that the element is palindrome
Else
Print message that the element is not a palindrome
n[i] sum
End of for loop of i loop
Print displaying the elements along with the sum of digits
Print display
for i 0 to <lim step value is 1
Print displaying the elements
if ( ar[i]<10)
Print three blank spaces
Else if ( ar[i]<100)
Print two blank spaces
Else if (ar[i]<1000)
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
import java.io.*;
public class palindrome
{
//start of class palindrome
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int ar[]=new int [100];
int n[]=new int[100];
int lim;
palindrome(int a) //parameterized constructor
{
lim=a;
for(int i=0;i<lim;i++)
{
ar[i]=0;
n[i]=0;
}
}
void check()
{
//method check to check whether the number is a palindrome or not
int t=0,reverse,sum;
for(int i=0;i<lim;i++)
{
t=ar[i];
reverse=rev(t,1);
sum=rev(t,2);
if(t==reverse) //checking
System.out.println(" "+ar[i]+" IS A PALINDROME ");
//displaying
else
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :
USAGE OF THE
VARIABLE : DATA TYPE : VARIABLE :
ob palindrome Encapsulation.