Sie sind auf Seite 1von 65

Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the

Universe trying to produce bigger and better idiots. So far, the Universe is winning.
Rick Cook, The Wizardry

Compiled

ACKNOWLEDGING THE HELPING HANDS IN MAKING ONES PROJECT REVEALS UNPROUDNESS OF ONES MIND . THEREFORE, I WANT TO THANK EVERYONE WHO HAS HELPED ME. I WANT TO SHOW MY SPECIAL GRATITUDE TO MY TEACHER , WITHOUT WHOSE EXPLANATION I WOULD NOT HAVE BEEN ABLE TO MAKE IT POSSIBLE.

I AM ALSO THANKFUL TO MY PARENTS AND FRIENDS WHO, THROUGH THEIR INNOVATIVE IDEAS, HELPED ME A LOT.

BY-:

S.No. 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.

Topic Circular Matrix. Calender + Pattern Matrix Transpose Factorial using Recursion Time in words Graduate Students Organizing a get together Base comparison Wondrous Square Arrangement of words Number to words

Page No. 7 9 11 13 16 18 20 25 29 31 34 36

13. 14. 15. 16.

Decimal to Octal number Roman number Pushing & Popping Subtracting days from a date Subtract two dates. Print Job Palindrome pattern Consecutive Alphabets

38 40 42 45

17. 18. 19. 20.

48 51 56 56

PROGRAM: 1
Program to print the circular matrix. ALGORITHM
STEP 1:Start Step2;Accept the size of the matrix I.e. number of rows and no. of columns n Step3: Initialize an integer type variable r=0,c=-1,l,f,t=1. Step4;Create a matrix a[][] i.e. int a[][]new =int [n][n] Step5- Set a while loop i.e. while (n>0) if true goto Step6 else goto 12 Step6- Inside a while loop set a for loop i.e. for(i=1;i<=n;i+=) inside this loop store the nos. from 1 to n to the row r i.e. a[r][++c] =t++ Step7- Set a for loop i.e. for(i=1;i<n;i++) which will store the numbers from t onwards in the column specified by c i.e. a[++r][c]=t++ Step8- Set a for loop i.e. for(i=1;i<n;i++) .In this loop no. from t onwards will be stored in a row specified by c i.e. a[r][--c]=t++ Step9- Set a for loop i.e. for (i=1;i<n-1;i++).in this loop no. from t onwards will be stored in a column specified by variable r i.e a[--r][c]=t++ Step10- Decrease the value of n i.e. the no. of columns or rows by 2

Step11- Goto step5 Step12- Print the matrix by setting two for loops Step13- End

PROGRAM
import java.io.*; class Cir_Matrix {public static void main(int n) {int i,r=0,c=-1,t=1,l=n,j; int a[][]=new int[n][n]; while(n>0) {for(i=1;i<=n;i++) a[r][++c]=t++; for(i=1;i<n;i++) a[++r][c]=t++; for(i=1;i<n;i++) a[r][--c]=t++; for(i=1;i<n-1;i++) a[--r][c]=t++; n=n-2; } System.out.println("Circular Matrix for input "+n+" is:-"); for(i=0;i<l;i++) {for(j=0;j<l;j++) {System.out.print(a[i][j]+" "); }System.out.println(); }}}

OUTPUT

PROGRAM - 2
To enter the total no. of days in month & the first day of month & print calendar of the month.

ALGORITHM
STEP 1:Start STEP 2:Declare an array or 6 rows & 7 columns. STEP 3:Make a void function input() to input the no of days in month & first day. STEP 4:Make funtion void calc() STEP 5:Make a loop from 0 to 5 in a. STEP 6:Make a loop from 0 to 6 in b. STEP 7:If a==0 and b==fd-1,c++. STEP 8:If c>0 and c<=nd,n[a][b]=c++. STEP 9:End both loops. STEP 10:Make the main() function. STEP 11:Print the names of days in one row. STEP 12:Print those elements of array that aare not 0. STEP 13:End

PROGRAM
import java.util.*; class Calender {Scanner ob=new Scanner(System.in); int fd,nd,n[][]=new int[6][7]; void input()throws Exception {System.out.println("Enter no.of days in the month:"); nd=ob.nextInt();System.out.println("Enter I day's no.if 1 is for monday:"); fd=ob.nextInt(); }void calc() {int a,b,c=0; for(a=0;a<6;a++) {for(b=0;b<7;b++) {if(a==0&&b==fd-1) c++;

if(c>0&&c<=nd) n[a][b]=c++; }}} void main()throws Exception {String s[]={"MON","TUE","WED","THU","FRI","SAT","SUN"}; input();calc(); for(int y=0;y<7;y++) System.out.print(s[y]+"\t"); System.out.println(); for(int h=0;h<6;h++) {for(int t=0;t<7;t++) {if(n[h][t]!=0) System.out.print(n[h][t]); System.out.print("\t"); }System.out.println();}}

OUTPUT

PROGRAM - 3
To form the + pattern from entered string. ALGORITHM
STEP-1: Start STEP-2:Enter the string. STEP-3:Take the loop upto the length of string through a. STEP-4:Take the loop for entering spaces until a is not equal to 2. STEP5:Print the characters of String. STEP-6: Stop.

PROGRAM
class form5 { public static void main(String s) { int l=s.length(); int k=l/2; for(int a=0;a<l;a++) { if(a!=k) { for(int b=0;b<k;b++) System.out.print(" ");

System.out.println(s.charAt(a)); } else { for(int b=0;b<l;b++) System.out.print(s.charAt(b)+" "); System.out.println(); } } } }

OUTPUT

PROGRAM: 4
To print the Transpose of entered matrix & the sum of each column, row & diagonal. ALGORITHM
STEP-1:START. STEP-2:Define a 4*4 matrix . STEP-3:Input the elements of the matrix from the user row-wise. STEP-4:Loop through the matrix and change the columns to rows and rows to columns- a[i][j]=a[j][i]. STEP-5:Take another array a[] of 4 and place the sum of each row in it . STEP-6:Print each row of the changed matrix followed by the sum of that row. STEP-7:Take another array b[] of 4 and store the sum of the columns. STEP-8:Print the array b[]. STEP-9:Now print the sum of the primary diagonal of the changed matrix after the printing of the elements of the array b[]. STEP-10:STOP.

PROGRAM

import java.util.*; class Trr {Scanner in = new Scanner(System.in); protected int arr[][] = new int[4][4]; public void main() {Trr a = new Trr(); a.input(); Trr b = new Trr(); b.transpose(a); b.sum(); } private void input() // function to input the matrix row-wise {System.out.println("ENTER THE MATRIX"); for(int i=0;i<4;i++) {System.out.println("Enter next row"); for(int j=0;j<4;j++) arr[i][j] = in.nextInt(); }} private void transpose(Trr M) // function to transpose the matrix of the given object {for(int i=0;i<4;i++) {for(int j=0;j<4;j++) arr[i][j]= M.arr[j][i] ; } } private void sum() // function to display the sum of each row and each column nad the sum of the diagonal { int s=0,spd=0; for(int i=0;i<4;i++)

{for(int j=0;j<4;j++) { if(i==j) spd = spd +arr[i][j]; s = s+arr[i][j]; }for(int j=0;j<4;j++)

// printing the sum of the rows in front of each row

System.out.print(arr[i][j]+"\t"); System.out.println(s); s=0; }System.out.println(); for(int i=0;i<4;i++) // printing the sum of each column at the bottom of the matrix {for(int j=0;j<4;j++) s= s+arr[j][i]; System.out.print(s+"\t"); s=0; } System.out.print(spd); } } // printing the sum of the diagonal

OUTPUT
ENTER THE MATRIX Enter next row 1 2 3 4 Enter next row 6 5 4 3 Enter next row 8

7 6 7 Enter next row 3 5 9 8 1 6 8 2 5 7 3 4 6 4 3 7 10 18 28

3 5 9 8 25

18 19 22 22 20

PROGRAM: 5
To find the factorial of any number using recursion. ALGORITHM
STEP 1:Start. STEP 2:Enter number whose factorial to be calculated. STEP 3:Check whether is it equal to 0 or 1 ;if it is so return 1. STEP 4:If it is not as above case calculate factorial using the method STEP 5:Stop

PROGRAM

import java.io.*; class factorial { public int fact(int n) { if(n==1 || n==0) return(1); else return(n*fact(n-1)); } public void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the number : "); int n=Integer.parseInt(br.readLine()); System.out.println("The Factorial Of "+n+" is "+fact(n)); } }

OUTPUT

PROGRAM:6
To print entered time in words . ALGORITHM

STEP-1:Start STEP-2: Enter the hours & minutes STEP-3: Declare an array hrN[] to store names STEP-4: Check all possible cases of hours & minutes. STEP-5: Print the finally converted time. STEP-6: Stop.

PROGRAM
import java.io.*; public class time { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the time :"); System.out.print("Enter the hours :"); int hr=Integer.parseInt(br.readLine()); System.out.print("Enter the minutes :"); int min=Integer.parseInt(br.readLine()); String time=new String(); String hrN[]={"twelve"," one "," two "," three "," four "," five "," six "," seven "," eight "," nine "," ten "," eleven ","twelve"};String minu=ConMin(min); if( min==45) {if(hr<11) time="quarter to "+hrN[hr+1]; else time="quarter to "+hrN[12-hr+1]; }else if(min==0) time=hrN[hr]+"o'clock"; else if(min==30) time="half past "+hrN[hr];

else if(min==15) time="quarter past "+hrN[hr]; else time=minu+" minutes past "+hrN[hr]; System.out.println(hr+":"+min+"="+time); }public static String ConMin(int n) {String t[]={""," one "," two "," three "," four "," five "," six "," seven "," eight "," nine "," ten "," eleven "," twelve "," thirteen "," fourteen "," quarter "," sixteen "," seventeen "," eighteen "," nineteen "}; String h[]={"","","twenty","thirty","forty","fifty"};String tim=new String(); if(n>0&& n<=19) tim=t[n]; else tim=h[n/10]+t[n%10]; return tim; }}

OUTPUT

PROGRAM: 7
To enter the details of the ten students & print the details of the graduate having highest percentage marks using inheritence. ALGORITHM
STEP-1:START STEP-2:Input the year from the user STEP-3:Enter the details like name, age, roll number, percentage marks, employment status, and subject of the ten students. STEP-4:Print the number of employed and non-employed graduates. STEP-5:Find the highest percentage marks from among the entered graduates. Assuming 0 percent marks as highest and then checking in the loop for marks greater than that. If any then the number is overwritten on the previous one. STEP-6:Print the details of the graduate having highest percentage marks. STEP-7:STOP.

PROGRAM
import java.util.Scanner; class GraduateStudentmn { Scanner in = new Scanner(System.in); public void main()throws Exception { int nw=0,nnw=0,yy; System.out.println("ENTER THE WORKING YEAR"); yy= in.nextInt(); GraduateStudent obj[] = new GraduateStudent[10]; int c=0,ft=0; for(int i=0;i<10;i++) // inputing the details of the ten students

{GraduateStudent obj1 = new GraduateStudent(); obj1.input();obj[i] = obj1; } for(int i=0;i<10;i++) // checking for the top marks and the employement status {if(obj[i].employed==true) nw++; else if(obj[i].employed==false) nnw++; if(obj[i].percentage_marks>obj[c].percentage_marks) c=i; if(obj[i].percentage_marks>75) ft++; }System.out.println("YEAR-----------"+yy); // printing the details

System.out.println("NO. OF WORKING GRADUATES: "+ nw); System.out.println("NO. OF NON WORKING GRADUATES: "+ nnw); System.out.println("DETAILS OF THE TOP MOST SCORER"); System.out.println("NAME: "+obj[c].name); System.out.println("AGE: "+obj[c].age); System.out.println("SUBJECT: "+obj[c].subject); System.out.println("AVERAGE MARKS: "+obj[c].percentage_marks); System.out.println("75% AND ABOVE ARE Ist DIVISIONERS. PERCENTAGE OF Ist DIVISIONERS IS "+(ft*100)/10); }} class Person {Scanner in = new Scanner(System.in); protected String name;protected int age,rollno protected double percentage_marks;protected void input()throws Exception {System.out.println("ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS");

name = in.next(); age = in.nextInt(); rollno = in.nextInt(); percentage_marks = in.nextDouble(); }}class GraduateStudent extends Person {Scanner in = new Scanner(System.in); protected String subject; protected Boolean employed; public void input()throws Exception { super.input(); System.out.println("ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED"); subject = in.next(); employed = in.nextBoolean(); }} // inputing the details

OUTPUT ENTER THE WORKING YEAR 1993 ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Saumya 20 1 89 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Science false ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Prashant 20 2 90

ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Commerce true ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Rahul 20 3 91 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Science true ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Kartikeya 20 4 92 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Science true ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Tushar 20 5 93 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Commerce false ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Shreya 20 6 94 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Commerce false ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Anam

20 7 95 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Science true ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Tanya 20 8 96 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Science true ENTER THE NAME , AGE , ROLLNO , PERCENTAGE MARKS Michael 20 9 97 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Commerce false ENTER THE NAME, AGE, ROLLNO, PERCENTAGE MARKS Bruce 89 10 89 ENTER THE SUBJECT OF THE STUDENT AND ENTER TRUE IF EMPLOYED Commerce true YEAR-----------1993 NO. OF WORKING GRADUATES: 6 NO. OF NON WORKING GRADUATES: 4 DETAILS OF THE TOP MOST SCORER NAME: Michael AGE: 20 SUBJECT: Commerce

AVERAGE MARKS: 97.0 75% AND ABOVE ARE Ist DIVISIONERS. PERCENTAGE OF Ist DIVISIONERS IS 100

PROGRAM: 8
To enter number of friends &relatives & print the possible days of organizing a get together . ALGORITHM
STEP-1:Start STEP-2: Enter the number of friends & relatives n. STEP-3: Declare an array A[][] of size n*31. STEP-4: Enter days when they are free. STEP-5: Compare the entries. STEP-6: Print the entries common in all relatives. STEP-7: Stop.

PROGRAM
import java.io.*; import java.lang.*; public class party { static int n; static int A[][]=new int[100][31]; public static int compair(int m) {

int d=A[0][m]; int flag=0,x=1,y=0;

for(x=1;x<n;x++) {flag=0; for(y=0;y<31;y++) { if(A[x][y]==d) { flag=1; } } if(flag==0) { return 0; }

} return 1; } public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter number of relatives & friends :"); n=Integer.parseInt(br.readLine()); int i,j=0; for(int x=0;x<n;x++) { j=0; System.out.print("For "+(x+1)+" relative ,Enter dates :"); do

{ i=Integer.parseInt(br.readLine()); A[x][j]=i; j++; } while (i!=0); }

int point=0; int S[]=new int [31]; for(int x=0;x<n;x++) { if(A[0][x]==0) { break; } else { S[x]=compair(x); if(S[x]!=0) point++; } } if(point==0) { System.out.print("No suitable dates "); }

else { System.out.print(" Suitable dates "); for(i=0;i<31;i++) { if(S[i]==1) { System.out.print(A[0][i]+", "); } } } } }

OUTPUT

PROGRAM: 9
To compare two numbers & print the bases in which they are equal . ALGORITHM
STEP-1:Start STEP-2: Enter 2 numbers x&y. STEP-3: Find & store the ith base of numbers & store them in array xb[] & yb[] respectively. STEP-4: Using linear search technique, compare the bases. STEP-5: If 2 bases are found equal print them STEP-6: If none is found equal then print x is not equal to y in any base . STEP-7: Stop.

PROGRAM
import java.io.*; import java.lang.*; public class base { static String xb[]=new String[21]; static String yb[]=new String[21]; static char Digit[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J'}; public static String findbase(int n, int base) { if(n<0) n=-n;String s=""; while(n!=0) { s+=Digit[n%base];n=n/base; } return s; }

public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a number :"); int x=Integer.parseInt(br.readLine()); System.out.print("Enter another number :"); int y=Integer.parseInt(br.readLine()); for(int i=1;i<21;i++) {yb[i]=findbase(x,i);}int pos=0; for(int i=1;i<21;i++) { xb[i]=findbase(x,i);pos=search(i); if(pos!=0) {System.out.println(x+"( base "+i+" ) = "+y+"( base "+pos+")"); break; }} if(pos==0) { System.out.println(x+" is not equal to "+y+" in any base "); } } public static int search(int i) {for(int x=1;x<21;x++) { if(xb[i].equals(yb[x])) return x; } return 0; }}

OUTPUT

PROGRAM: 10
To check whether an entered array is Wondrous Square & print prime no. & their location. ALGORITHM
STEP-1:Start STEP-2: Enter an array of size n x n. STEP-3: Check whether it is wondrous or not. STEP-4: Check each element whether it is prime or not. STEP-5: If prime , print the number &its location. STEP-6: Stop

PROGRAM
import java.io.*; public class wondrous { static int n; static int A[][]= new int [100] [100]; public static boolean check()

{ int x=0, y=0; int S[][]= new int [2][n]; for (x=0; x<n; x++) { for (y=0; y<n; y++) { S[0][x]+= A[x][y]; S[1][x]+= A[y][x]; }} double p=0.5*n*((n*n)+1); if( S[0][0]==(int)(p)) {x=S[0][0]; for (y=0;y<n;y++) { if (S[0][y]!=x) return false; if (S[0][y]!=x) return false; } return true; } else return false; }public static boolean prime(int m) { int flag=0; for (int x=2; x<m; x++) { if (m%x ==0) {flag= 1;} } if (flag ==0) return true;

else return false; } public static void main(String args[])throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter the limit :"); n=Integer.parseInt(br.readLine()); for(int x=0;x<n;x++) { System.out.print("For the "+(x+1)+"row,"); for(int y=0;y<n;y++) { System.out.print("Enter a no. :"); A[x][y]= Integer.parseInt(br.readLine()); } } boolean wonder= check(); if (wonder==true) System.out.println("Yes it represents a wondrous square"); else System.out.println("No, it doesn't represents a wondrous square"); System.out.println("Prime Row Index Column Index"); for (int x=0; x<n; x++) { for (int y=0;y<n;y++) { boolean primeN= prime (A[x] [y]); if (primeN==true&& A[x][y]!=1) {System.out.println( A[x] [y]+" } } } }} "+x+" "+y);

OUTPUT

PROGRAM: 11
To arrange a string in order of length of word .

ALGORITHM
STEP-1:Start STEP-2: Enter a String, terminated by '.'. STEP-3: Extract each word from the String & store them in array ss[]. STEP-4: Using sorting , arrange the words in order of their lengths. STEP-5: Prints the words in the new order. STEP-6: Stop.

PROGRAM
import java.io.*; public class word_order { public static void main(String[] args) throws IOException {BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a sentence ,terminated by '.':"); String s=br.readLine(); int in=0; System.out.println("Transformed Sentence : "); String ss[]=new String[s.length()]; int s2=0;s=s.toLowerCase(); s=s.substring(0, s.length()-1)+" "; for(int n=0;n<s.length();n++) {if(s.charAt(n)==32) {ss[s2]=s.substring(in, n);ss[s2]=ss[s2].trim(); in=n;s2++; } } String a=" ";int i;int s1=0;

for(int n=0;n<s2;n++) {for(int y=0;y<n;y++) {if(ss[n].length()<ss[y].length()) {a=ss[n]; ss[n]=ss[y]; ss[y]=a; }}} if(ss[0].charAt(0)>96 && ss[0].charAt(0)<=122) {s1=ss[0].charAt(0);s1=s1-32; System.out.print((char)s1); for(i=1;i<ss[0].length();i++) {System.out.print(ss[0].charAt(i)); }System.out.print(" "); }for(i=1;i<s2;i++) {System.out.print(ss[i]+" "); }System.out.print('.'); }}

OUTPUT

PROGRAM: 12
Write a program to enter a number & convert it into words. ALGORITHM
STEP-1:Start STEP-2: Declare an array word[] storing digit names STEP-3: Enter a number less than 10000 STEP-4: Store all digits of number in array a[] in proper order. STEP-5: Run a loop to print word of the digits stored in a[]. STEP-6: Stop.

PROGRAM
import java.io.*; class num_word {public static void main(String args[])throws IOException {BufferedReader dd=new BufferedReader(new InputStreamReader(System.in)); int i,n,n1;int a[]=new int[4]; Stringword[]={"One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen","Fou rteen","Fifteen","Sixteen","Seventeen","Eighteen","Ninteen","Twenty","Thirty","Forty","Fifty","Sixty","Seventy","Eighty", "Ninty","Hundred","Thousand"}; System.out.print("Enter a natural number less than 10000 :"); n=Integer.parseInt(dd.readLine()); n1=n; i=4; System.out.print(n+"= "); while(n1>0) {a[--i]=n1%10;n1=n1/10;}

while(i!=0) a[--i]=0; if(a[0]!=0) System.out.print(word[a[0]-1]+" "+word[28]+" "); if(a[1]!=0) System.out.print(word[a[1]-1]+" "+word[27]+" "); if(a[2]!=0) { if(a[2]>=2) System.out.print(word[a[2]+17]+" "); if(a[3]!=0) System.out.print(word[a[3]-1]+" "); } if(a[2]==1) System.out.print(word[a[3]+9]); } else if(a[2]==0&&a[3]!=0) System.out.print(word[a[3]-1]); } }

OUTPUT

PROGRAM: 13
Define a class dec_to_oct with following data members
int n number to be converted to its octal equivalent

int s to store octal equivalent int i to store the incremental power

and following functions : dec_to_oct( ) void input( ) constructor to input number in n.

void convert(int) to convert any number to its octal equivalent using recursion. void display() also write main( ). to print n and its octal equivalent s.

ALGORITHM
Step 1:START. Step 2:Input the number to converted from the user. Step 3:Take a variable s to store the converted number. Step 4:Take another variable i = 10. Step 5:If number is not equal to zero then go to Step 6 else go to Step 9. Step 6:Update s equal to number module 8 plus s multiplied by i. Step 7:Update number equal to number divided by 10. Step 8:Go to step 5. Step 9:Print the value of s. Step 10:STOP.

PROGRAM
import java.util.*; class dec_to_oct { private int n,s,i; public void main()throws Exception { input(); convert(n); display();s=0; } public dec_to_oct() { n=0; s=0; i=10; } { private void input()throws Exception Scanner in =new Scanner(System.in); System.out.println("ENTER THE NUMBER"); n = in.nextInt(); } private void convert(int n) { if(n!=0) { int r = n%8; n=n/8; convert(n);

s = r+ s*i; }} private void display() {

System.out.println("THE NUMBER IS"+"\t"+n); System.out.println("IT'S OCTAL EQUIVALENT IS:"+"\t"+s); } }

OUTPUT

PROGRAM: 14
Write a program to enter a Roman Number & give its equivalent integer. ALGORITHM
Step1:Start Step 2:Int I,sum

Step 3:String sym= "IVXLCDM"; int value[]={1,5,10,50,100,500,1000}; for(i=0;i<s.length();i++) if(i<(s.length()-1)&&sym.indexOf(s.charAt(i))<sym.indexOf(s.charAt(i+1))) sum-=value[sym.indexOf(s.charAt(i))]; else sum+=value[sym.indexOf(s.charAt(i))]; return sum; Step 4:Stop

PROGRAM
import java.util.*; class roman2no { public static int compute(String s) { int i,sum=0 String sym= "IVXLCDM"; int value[]={1,5,10,50,100,500,1000}; for(i=0;i<s.length();i++) //LOOP TO CALCULATE PARTIAL SUM & CHECKING if(i<(s.length()-1)&&sym.indexOf(s.charAt(i))<sym.indexOf(s.charAt(i+1))) sum-=value[sym.indexOf(s.charAt(i))]; else sum+=value[sym.indexOf(s.charAt(i))]; return sum; }

public static void main() { Scanner obj=new Scanner(System.in);

int sum=0; String s=new String(""),s1=new String(""),s2=new String(""); System.out.println("Enter no. in roman"); s=obj.nextLine(); //INPUTTING THE ROMAN NUMBER

if(s.charAt(0)=='(') { s1=s.substring(1,s.indexOf(')')); s2=s.substring(s.indexOf(')')+1); } else s2=s; sum=(compute(s1)*1000)+compute(s2); System.out.println(sum) } }

OUTPUT

PROGRAM: 15
Pushing & Popping(print appropriate message when there is a underflow or overflow) ALGORITHM
Step-1:Start Step-2:Declare an array ele[] Step-3:Enter the values of elements in constructor Step-4:In function push item, enter the value to be pushed checking whether array is not overflow & print appropriate message. Step-5:In function pop item, enter the value to be poped checking whether array is not underflow & print appropriate message. Step-6:Stop

PROGRAM
import java.util.*; class strange { int ele[],capacity,top; Scanner obj=new Scanner(System.in); strange(int cap) int a=0; capacity=cap; ele=new int[capacity]; top=-1; for(a=0;a<capacity;a++) //INPUTTING THE NUMBERS IN STACK { {

System.out.println("ENTER AN ELEMENT AT "+a+"th POSITION"); ele[a]=obj.nextInt(); top=a; } System.out.println("ENTERED ARRAY IS --"); for( a=0;a<capacity;a++) System.out.print(ele[a]+" "); } public void push item(int value) //METHOD TO PUSH AN ITEM IN ARRAY { int a=0,d=0; if(ele[capacity-1]==0) { for(a=capacity-1;a>0;a--) //PUSHING EACH ELEMENT ON RIGHT ele[a]=ele[a-1]; ele[0]=value; System.out.println("THE NEW ARRAY IS --"); //PRINTING THE PUSHED ARRAY for( a=0;a<capacity;a++) System.out.print(ele[a]+" "); } else System.out.println("STRANGE IS FULL"); } public void popitem() //METHOD TO POP AN ITEM { int a=0,pop=ele[0]; if(ele[a]!=0) {

for(a=0;a<capacity-1;a++) //LOOP TO SHIFT ELEMENTS LEFT { ele[a]=ele[a+1]; } ele[top]=0; //ELEMENTS TO GIVE LAST POSITION 0 top--; System.out.println("THE NEW ARRAY IS--); //PRINTING THE NEW ARRAY for(a=0;a<capacity;a++) System.out.print(ele[a]+" "); System.out.println("POPPED ITEM "+pop); } else System.out.println("STACK IS EMPTY"); } }

OUTPUT

PROGRAM: 16
Write a program to subtract n days from a given date.

ALGORITHM

Step-1 : Start Step-2: Initialize array int mo[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; Step-3:create method : boolean isvalid(int a[]) a-i-check whether((a[3]%100==0&&a[3]%400==0)||(a[3]%100!=0&&a[3]%4==0)) ii- thenmo[2]=29; iii-else mo[2]=28; b-icheck whwther (a[3]>0&&a[2]>0&&a[1]>0&&a[2]<=12&&a[1]<=mo[a[2]]) ii-if satisfied ;return true; iii-else return false; Step-4:create method : static void pre(int a[]) i-a[1]--; ii-check if(a[1]==0)

iii-if true ;then > a[2]--; >if(a[2]==0) >a[2]=12;a[3]--; >a[1]=mo[a[2]]; Step-5:End

PROGRAM
import java.ioclass SUBTDATE {static int mo[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; static boolean isvalid(int a[]) //CONDITION CHECK FOR LEAP YEAR CHECK { if((a[3]%100==0&&a[3]%400==0)||(a[3]%100!=0&&a[3]%4==0)) mo[2]=29; else mo[2]=28; if(a[3]>0&&a[2]>0&&a[1]>0&&a[2]<=12&&a[1]<=mo[a[2]]) return true; else return false; } static void pre(int a[]) { a[1]--; if(a[1]==0) { a[2]--; if(a[2]==0)

{a[2]=12;a[3]--;} a[1]=mo[a[2]]; } } public static void main()throws Exception { DataInputStream v=new DataInputStream(System.in); int a[],n;a=new int[4]; System.out.println("enter the day"); //INPUTTING THE DATE a[1]=Integer.parseInt(v.readLine()); System.out.println("enter the month"); //INPUTTING THE MONTH a[2]=Integer.parseInt(v.readLine()); System.out.println("enter the year"); //INPUTTING THE YEAR a[3]=Integer.parseInt(v.readLine()); System.out.println("enter no. of days to substract"); n=Integer.parseInt(v.readLine()); if(isvalid(a)) { for(int ss=0;ss<n;ss++) pre(a); System.out.println(a[1]+"-"+a[2]+"-"+a[3]); } else System.out.println("wrong entry!!"); //PRINTING APPROPRIATE MESSAGE } }

OUTPUT

PROGRAM: 17
Write a program to input two dates & subtract them.

ALGORITHM
Step 1:Start Step 2:Create data member static int mo[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; Step 3:Enter the 2 dates. Step 4 :check whether 2 dates are valid or not. Step 5:If dates are valid, subtract the dates Step 6: Stop

PROGRAM
import java.io.*; class date_reduce { static int mo[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; static boolean isvalid(int a[]) //METHOD THAT RETURNS IF DATE IS VALID OR NOT { if((a[3]%100==0&&a[3]%400==0)||(a[3]%100!=0&&a[3]%4==0)) mo[2]=29; else mo[2]=28; if(a[3]>0&&a[2]>0&&a[1]>0&&a[2]<=12&&a[1]<=mo[a[2]]) return true; else return false; } public static void main()throws Exception //MAIN METHOD { BufferedReader v =new BufferedReader(new InputStreamReader(System.in)); int a[],n,b[],c[];

a=new int[4];b=new int[4];c=new int[4]; System.out.print("enter the day:"); //INPUTTING FIRST DAY a[1]=Integer.parseInt(v.readLine()); System.out.print("enter the month:"); //INPUTTING FIRST MONTH a[2]=Integer.parseInt(v.readLine()); System.out.print("enter the year:");//INPUTTING FIRST YEAR a[3]=Integer.parseInt(v.readLine()); System.out.print("enter the day:"); //INPUTTING SECOND DAY b[1]=Integer.parseInt(v.readLine()); System.out.print("enter the month:"); //INPUTTING SECOND MONTH b[2]=Integer.parseInt(v.readLine()); System.out.print("enter the year:"); //INPUTTING SECOND YEAR b[3]=Integer.parseInt(v.readLine()); if(isvalid(b)&&isvalid(a)) { if(a[1]<b[1]) {c[1]=a[1]+mo[a[2]]-b[1]; a[2]--; if(a[2]==0) a[2]=12; } else c[1]=a[1]-b[1]; //CONDITION CHECK

if(a[2]<b[2]) {

c[2]=a[2]+12-b[2]; a[3]--; } else c[2]=a[2]-b[2]; c[3]=a[3]-b[3]; System.out.println("Difference between the dates \n Days:"+ c[1]+"\n Months :"+c[2]+"\n Years:"+c[3]); //PRINTING } else System.out.println("wrong entry!!"); //PRINTING APPROPRIATE MESSAGE }}

OUTPUT

PROGRAM: 18
NIC institutes resource manager has decided to network the computer resources like printer, storage media etc, so that minimum resources and maximum sharing could be availed. Accordingly printers are linked to a centralized system and the printing jobs are done on a first cum first served basis only. This is like the first persons printing job will get done first and the next persons job will be done as the next job in the list and so on. In order to avoid collision, the restriction is that no more than 20 printing jobs can be added.

Define the class PrintJob with the following detail: Class name : PrintJob

Data members/instance variables job[ ] Newjob Capacity Front Rear Member Functions: PrintJobs ( ) : Constructor to initialize the data member Capacity = 20, Front = Rear=-1 and call the function createJob ( ) To create an array to hold the printing jobs. Adds the new printing job to the end of the last : : : : : array to integers to hold the printing jobs To add a new printing job into the array The maximum capacity of the integer array To point to the index of the front To point to the index of the last

void createJob ( ) void addJob ( ) printing

: :

job, if possible, otherwise displays the message Printjob is full, cannot add any more void removeJob ( ) printing job : Removes the printing job from the front if the

is not empty, otherwise displays the messge Printjob is empty also write main() for above class.

ALGORITHM
STEP-1:Start STEP-2: Display the choices to user. STEP-3: 1-to add job,2-to remove job,3-to print,4-to quit. STEP-4: Enter the choice from user. STEP-5: If 1 is entered , then add a job to the existing array by adding 1 to Rear if space is there in the array. If array is full, then message is displayed PRINTJOB IS FULL. CANNOT ADD ANY MORE. STEP-6: If 2 is entered , then remove element from the array by subtracting 1 from Front. If not possible then display the message PRINTJOB IS EMPTY. STEP-7: If 3 is entered then print all the elements of the array. STEP-8: If 4 is entered then break out of the program. STEP-7: STOP.

PROGRAM
import java.util.*; import java.io.*;

class PrintJob { protected int job[], Newjob,Capacity,Front,Rear; public void main()throws Exception { BufferedReader in =new BufferedReader(new InputStreamReader(System.in)); System.out.println("1- to add new job.2- to remove a job.3-to print.4-to quit"); while(true) { System.out.println("ENETER YOUR CHOICE"); int n =Integer.parseInt(in.readLine()); if(n==1) addJob(); else if( n==2) removeJob(); else if(n==3) for(int i=Front;i<=Rear;i++) System.out.println(job[i]); else if(n==4) break; }

} PrintJob() { Capacity = 20; Front = -1; Rear = -1; createJob(); } private void createJob() { job= new int[Capacity]; } private void addJob()throws Exception {Scanner in =new Scanner(System.in); System.out.println("ENTER THE JOB"); Newjob = in.nextInt(); if(Rear==19) System.out.println("Printjob is full. cannot add any more"); else if(Rear<19) { Rear++; job[Rear]= Newjob; if(Front==-1)

Front++; } } private void removeJob() { if(Front==-1&&Rear==-1||Front >Rear) { Front =-1; Rear = -1; System.out.println("Printing job is empty"); } else Front++; } }

OUTPUT

PROGRAM - 19
To enter a string & if its Palindrome,print the patternMADAM A A D D A A MADA M

ALGORITHM
Step-1 Start Step-2 Enter any string Step-3 Take the reverse of the string into any variable Step-4 Initialize c=length of string -2 Step-5 If string is not equal to its reverse, then print that it is not palindrome & exit. Step-6 Else print the string Step-7 Create loop from 1 to length of string -1(Step 8 to 10) Step-8 Print the character at that index Step-9 Print c spaces Step-10 Print the character at that index Step-11 Print the string Step-12 Exit

PROGRAM
import java.io.*; class palinpat { public static void main(String s) { String t=""; int a,b,c=s.length()-2;

for(a=0;a<s.length();a++) t=s.charAt(a)+t; if(t.equals(s)==false) { System.out.println(s+" is not palindrome."); System.exit(0); } System.out.println(s); for(a=1;a<s.length()-1;a++) { System.out.print(s.charAt(a)); for(b=0;b<c;b++) System.out.print(" "); System.out.println(s.charAt(a)); } System.out.println(s); } }

OUTPUT

PROGRAM: 20
To print the String having consecutive alphabets . ALGORITHM
STEP-1:Start STEP-2: Enter the number of Strings STEP-3: Enter the Strings STEP-4: Convert all the Strings to Lower case

STEP-5: Check the strings & store the String having consecutive alphabets & store them in s. STEP-6: Print the stored String. STEP-7: End.

PROGRAM
import java.io.*; import java.lang.*; class ConsecutiveAlphabets { public static void main(int p)throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); String a[]=new String[p]; for(int x=0;x<p;x++) { System.out.print("Enter the name : "); a[x]=br.readLine(); } String s="",n; for(int x=0;x<p;x++) { s=a[x]; int f=0; n=s.toUpperCase(); for(int b=0;b<n.length()-1;b++) {

if((int)(n.charAt(b))+1==(int)(n.charAt(b+1))) f=1; } if(f==1) System.out.println(" Name with consequtive letters :"+s); } } }

OUTPUT

Das könnte Ihnen auch gefallen