Вы находитесь на странице: 1из 28

ONE DIMENSIONAL ARRAY PROGRAMS

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 :-

CLASS NAME :- NUMBEROFDAYS

DATA MEMBERS :- Three integers named d, mon and year representing

date, month and year respectively.

MEMBER FUNCTIONS :- void accept( ) : to accept the date, month and year

from the user and to call the function setdata( ) and

send the user inputted date.

void setdata( ) : to calculate and display the total

number of days from the start of that year till the

user inputted date.

Write the main( ) function.


ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

1. ALGORITHM FOR THE PROGRAM NUMBER OF DAYS : -

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. :

2. SOURCE CODE FOR THE PROGRAM NUMBER OF DAYS: -


import java.io.*;
public class NUMBEROFDAYS
{ //declaring necessary variables
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int d, mon , tday , year , i , lip=0 ;
int dpm[]={31,28,31,30,31,30,31,31,30,31,30,31};
public void accept()throws IOException //start of method accept()
{
System.out.println("Enter Day,Month Number ,Year ::");
int d=Integer.parseInt(br.readLine());
int mon=Integer.parseInt(br.readLine());
int year=Integer.parseInt(br.readLine());
setdata(d,mon,year); //calling the method setdata()
} //end of method accept()
public void setdata(int d,int mon,int year) //start of method setdata()
{
tday=d;
for(i=0;i<mon-1;i++)
{
tday+=dpm[i];
}
for(i=0;i<2;i++)
{
if(year%10!=0)
break;
year= year/10;
}
if(i==2)
{
if(year%400==0)
lip=1;
else if(year%4==0)
lip=1;
}
if((lip==1)&&(mon>2))
tday=tday+1;
System.out.println("Total days from start of the year is :: "+tday);
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

} //end of method setdata()


public void main()throws IOException //start of main() method
{
NUMBEROFDAYS ob=new NUMBEROFDAYS(); //declaring object of class
ob.accept();
} //end of method main()
} //end of class

3. OUTPUT OF THE PROGRAM NUMBER OF DAYS : -


ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

4. VARIABLE CHART FOR THE PROGRAM NUMBER OF DAYS : -

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.

year int To store the year.


Used as a loop
i int variable.
To see if the user
lip int inputted year is a
leap year or not.
To store the user
d int inputted date.
To store the user
mon int iputted month.
To store number of
dpm[] int array days in each
month of that year.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

PROGRAM 2 CHRISTIAN
GOLDBACK S PROGRAM
Q. TO DESIGN A CLASS WITH THE FOLLOWING

SPECIFICATIONS :-

CLASS NAME :- Christian_goldback

DATA MEMBERS :- An even integer represented by N.

MEMBER FUNCTIONS :- void accept( ) : to accept an even number from the

user .

void check( ) : to check and display N as a sum of

two prime numbers.

Write the main( ) function.

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. :

1. ALGORITHM FOR THE PROGRAM CHRISTIAN GOLDBACK : -

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.

for i 2 to N Step value 1

isprime[i] true.

end for.

for i 2 to (i*i)<N Step value 1

if(isprime[i])

for j = i to (i*j)<N Step value 1

isprime[i*j] = false;

end for.

End if.

End for.

Declaring variable primes 0;

for i 2 to N Step value 1

if(isprime[i])

primes++;

end if

end for.

Printing Done Tabulating Primes.


ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

Declaring integer array named list[] of size primes.

Declaring n 0;

for i 0 to N Step value 1

if(isprime[i])

list[n++] i.

End if.

End for.

Declaring left 0.

Declaring right primes-1.

while(left < = right)

if(list[left]+list[right]==N)

break;

else if(list[left]+list[right]<N)

left++;

else

right--;

end if else if.

End while

if(list[left]+list[right]==N)

Print N is equal to the sum of list[left] and list[right].

else

Print N is not expressible as sum of two primes.

End of function void check()

STEP 7 : End of the function void main()


STEP 8 : End.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

2. SOURCE CODE FOR THE PROGRAM CHRISTIAN GOLDBACK: -

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. :

//check if N can be expressed as sum of two primes


int left=0,right=primes-1;
while(left<=right)
{
if(list[left]+list[right]==N)
break;
else if(list[left]+list[right]<N)
left++;
else
right--;
}
if(list[left]+list[right]==N)
System.out.println(N+" = " + list[left]+ " + "+list[right]);
else
System.out.println(N + " not expressible as sum of two primes ");
} //end of method check()
public void main() //start of method main()
{
christian_goldback ob=new christian_goldback(); //declaring object of class
ob.accept();
ob.check();
} //end of method main()
} //end of class
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

3. OUTPUT OF THE PROGRAM CHRISTIAN GOLDBACK : -


ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

4. VARIABLE CHART FOR THE PROGRAM CHRISTIAN GOLDBACK : -

USAGE OF THE
VARIABLE : DATA TYPE : VARIABLE :

ob Christian_goldback Encapsulation.
To store the even
N int number given by the
user.

i int Used as a loop


variable.

j int Used as a loop


variable.
To store true for the
prime numbers less
isprime[] boolean array than N and false for
non prime numbers
less than N.

primes int To store number of


primes less than N.

list[] int array To store the prime


numbers.
Used as a counter
n int
variable.
To find the small
prime in list[] which
left int
when added with a
larger prime gives N.
To find the larger
prime in list[] which
right int whem added with a
smaller prime gives
N.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

PROGRAM 3 PRIME
MAXIMUM GAP PROGRAM
Q. TO DESIGN A CLASS WITH THE FOLLOWING

SPECIFICATIONS :-

CLASS NAME :- Primegap

DATA MEMBERS :- To accept a limit in N.

MEMBER FUNCTIONS :- void accept( ) : to accept an even number from the user .

void check( ) : to check and display N as a sum of two

prime numbers.

Write the main( ) function.

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

Output There are no primes between the numbers 90 and 96.

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. :

1. ALGORITHM FOR THE PROGRAM PRIME GAP: -

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.

for i 2 to N Step value 1

isprime[i] true.

end for.

for i 2 to (i*i)<N Step value 1

if(isprime[i])

for j i to (i*j)<N Step value 1

isprime[i*j] = false.

end for.

End if.

End for.

Declaring variable gap 0.

Declaring variable bestgap 0.

Declaring variable right 0.

for i 2 to N Step value 1

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.

Declaring integer left right bestgap + 1

Print There are no prime numbers between left and right .

Print That is bestgap consecutive integers.

End of function void check()

STEP 7 : End of the function void main()


STEP 8 : End.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

2. SOURCE CODE FOR THE PROGRAM PRIME GAP: -

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. :

3. OUTPUT OF THE PROGRAM PRIME GAP : -


ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

4. VARIABLE CHART FOR THE PROGRAM PRIME GAP : -

USAGE OF THE
VARIABLE : DATA TYPE : VARIABLE :

ob Primegap Encapsulation.

N int To store the limit


given by the user.

i int Used as a loop


variable.

j int Used as a loop


variable.
To store true for the
prime numbers less
isprime[] boolean array than N and false for
non prime numbers
less than N.
To store the gap
gap int between two
successive prime
numbers less than N.
To store the maximum
bestgap int gap recorded between
two successive prime
numbers less than N.
To store the
immediate next prime
right int
after the bestgap
period.
To store the lower
left int limit of the bestgap
period.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

PROGRAM 4 PALINDROME
Q. TO DESIGN A CLASS WITH THE FOLLOWING

SPECIFICATIONS :-

CLASS NAME :- Palindrome

DATA MEMBERS :- An integer array and its limit.

MEMBER FUNCTIONS :- parameterized constructor palindrome(int a) : to give

initial values to all the variables.

void input( ) : to accept all array elements from the

user .

void check( ) : to check and display if a array element

is a palindrome or not.

Write the main( ) function.


ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

1. ALGORITHM FOR THE PROGRAM PALINDROME : -

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. :

Print one blank space


Print display the element
End of for loop or i loop
STEP 8 : Calling of method int rev(int ,int ) to pass the reversed value to the calling
method
Rev 0
Sum 0
d0
if (c==1)
While (a!=0)
d a%10
rev rev*10+d
a a/10
End of while loop
Return rev
End of if
Else
While (1>0)
d0
Sum 0
While (a!=0)
d a%10
sum sum+d
a a/10
End of inner while loop
if (sum >=10)
a sum
else
Break outer while loop
End of outer while loop
Return sum
End of else
Return 0
End of method int rev(int,int)
STEP 9 : End of method void check()
STEP 10 : End of method void main()
STEP 11 : End.
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

2. SOURCE CODE FOR THE PROGRAM PALINDROME : -

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. :

System.out.println(" "+ar[i]+" IS NOT A PALINDROME ");


n[i]=sum;
}
System.out.println("The sum of digits of the array elements is as follows:: ");
System.out.println(" ELEMENT SUM ");
for(int i=0;i<lim;i++)
{
System.out.print(" "+ar[i]);
if(ar[i]<10)
System.out.print(" ");
else if(ar[i]<100)
System.out.print(" ");
else if(ar[i]<1000)
System.out.print(" ");
System.out.println(" "+n[i]+" ");
}
} //end of method check()
int rev(int a,int c)
{ //method to generate the reverse
int rev=0,sum=0,d=0;
if(c==1)
{
while(a!=0)
{
d=a%10;
rev=rev*10+d;
a=a/10;
}
return rev;
}
else if(c==2)
{
while(1>0)
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

{ //while loop to calculate sum of digits


d=0;sum=0;
while(a!=0)
{
d=a%10;
sum=sum+d;
a=a/10;
}
if(sum>=10)
a=sum;
else break;
} //end of while
return sum;
}
return 0;
} //end of rev(int,int)
void input()throws IOException
{ //start of method input()
System.out.println(Enter the array elements ::");
for(int i=0;i<lim;i++)
ar[i]=Integer.parseInt(br.readLine());
} //end of input()
public static void main(String args[])throws IOException
{ //start of main() method
System.out.print(" Enter the number of elements ::);
int k=Integer.parseInt(br.readLine());
palindrome ob = new palindrome(k); //creating object of class
ob.input();
ob.check();
} //end of method main()
} //end of class
ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

3. OUTPUT OF THE PROGRAM PALINDROME : -


ONE DIMENSIONAL ARRAY PROGRAMS
Page No. :

4. VARIABLE CHART FOR THE PROGRAM PALINDROME : -

USAGE OF THE
VARIABLE : DATA TYPE : VARIABLE :

ob palindrome Encapsulation.

N int To store the limit


given by the user.

i int Used as a loop


variable.

j int Used as a loop


variable.
To store true for the
prime numbers less
isprime[] boolean array than N and false for
non prime numbers
less than N.
To store the gap
gap int between two
successive prime
numbers less than N.
To store the maximum
bestgap int gap recorded between
two successive prime
numbers less than N.
To store the
immediate next prime
right int
after the bestgap
period.
To store the lower
left int limit of the bestgap
period.

Вам также может понравиться