|
|
| Author | Message |
|---|
WinRrule Active member


 | Subject: Java Programs Fri Mar 05, 2010 8:25 am | |
| | Hey Guys all the java programs that we need to know for ISC 2010 will be posted here.. some theory notes too will be posted..! |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Fri Mar 05, 2010 8:27 am | |
| Question Write a Java Program to find the sum of all odd numbers divisible by 7 between -50 and 50.
Program public class prac2 { public static void main(String[] args) { int i; System.out.println("Odd numbers between -50 and 50 which are divisible by 7: "); for(i=-49; i<50; i++) if((i%2) != 0) if((i%7) == 0) System.out.println(i); } }
Last edited by WinRrule on Fri Mar 05, 2010 8:31 am; edited 1 time in total |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Fri Mar 05, 2010 8:27 am | |
| Question Write a Java Program to accept any three numbers and find biggest among them after confirming that they are not equal to each other. Check whether these three numbers form lengths of sides of a triangle. Program import java.io.*; public class prac3 { public static void main(String[] args) throws IOException { InputStreamReader inp = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(inp); System.out.println("Enter 3 numbers: "); int a = Integer.parseInt(br.readLine()); int b = Integer.parseInt(br.readLine()); int c = Integer.parseInt(br.readLine()); //Check if they are equal if(a == b && a == c) System.out.println("All three values are equal."); //Find the biggest number int max = a; if(b > max) max = b; if(c > max) max = c; System.out.println("The biggest among "+a+", "+b+", "+c+" is "+max); //Check if they form the sides of a triangle if(((a+b) > c) && ((a+c) > b) && ((c+b) > a)) System.out.print("These three values can form a triangle."); else System.out.println("These values do not form a triangle."); } }
Last edited by WinRrule on Fri Mar 05, 2010 8:32 am; edited 1 time in total |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Fri Mar 05, 2010 8:27 am | |
| Question Write a Java Program to accept a number and perform the following: i. Check if it is three-digit number. ii. If it is a three-digit number, check if it is an Armstrong number. Program import java.io.*; public class prac4 { public static void main(String[] args) throws IOException { int i, m, n = 0; InputStreamReader in = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(in); System.out.println("Enter a number: "); i = Integer.parseInt(br.readLine()); m = i; while(m > 0){ n++; m /= 10; } if(n == 3){ //For Armstrong number if(i == sumd(i)) System.out.println(i+" is an Armstrong number."); } } static int power(int n, int p){ if(p == 0) return 1; else return (n * power(n, p-1)); } static int sumd(int n){ if(n==0) { return 0; } else return(power((n%10),3) + sumd(n/10)); } }
Last edited by WinRrule on Fri Mar 05, 2010 8:33 am; edited 1 time in total |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Fri Mar 05, 2010 8:29 am | |
| Question Write a Java Program to find whether the given number is a palindrome or not.
Program import java.io.*; public class prac7 { public static void main(String[] args) throws IOException { int n, m, d, s = 0; InputStreamReader inp = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(inp); System.out.println("Enter a number: "); n = Integer.parseInt(br.readLine()); m = n; while(m > 0){ d = m % 10; s += d; m /= 10; } if(n == s) System.out.println("It is a Palindrome number."); else System.out.println("It is not a Palindrome number."); } } |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Fri Mar 05, 2010 8:37 am | |
| Question Write a program to accept a number and check if it is a prime number.
Program import java.io.*; public class prac8 { public static void main(String[] args) throws IOException { int n, i, fl = 0; InputStreamReader inp = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(inp); System.out.println("Enter a number: "); n = Integer.parseInt(br.readLine()); for (i = 2; i <= (n/2); ++i){ if((n%i) == 0){ fl = 1; break; } } if(fl == 0) System.out.println(n+" is a prime number."); else System.out.println(n+" is not a prime number."); } } |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Fri Mar 05, 2010 8:38 am | |
| Question Write a Java Program to print numbers from 26 to 50 in form of square matrix of order 5x5 using nested for loops.
Program public class prac13 { public static void main(String args[]){ int i, j, k; for(i = 0, k = 26; i < 5; i++, k++){ for(j = 0; j < 5; j++,k++){ System.out.print(k+" "); } k--; System.out.println(); } } } |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Tue Mar 16, 2010 7:26 pm | |
| Question Write a Java program to print numbers in circular matrix.
Program import java.io.*; public class circular { public static void main(String args[])throws IOException { int i,j,c=1; InputStreamReader x=new InputStreamReader(System.in); BufferedReader y=new BufferedReader(x); System.out.println("Enter value of N: "); int n; n=Integer.parseInt(y.readLine()); int a[][]=new int[n][n]; int r=0,s=n-1; for(i=0;i<n;i ) { for(j=r;j<s;j ) { a[i][j]=c; c ; } for(j=r;j<=s-1;j ) { a[j][s]=c; c ; } for(j=s;j>=r;j--) { a[s][j]=c; c ; } for(j=(s-1);j>r;j--) { a[j][i]=c; c ; } r ; s--; } for(i=0;i<n;i ) { for(j=0;j<n;j ) { System.out.print(a[i][j]); System.out.print('\t'); } System.out.println(); } } }
Last edited by WinRrule on Tue Mar 16, 2010 7:53 pm; edited 2 times in total |
|
 | |
G-7 Active member


 | Subject: Re: Java Programs Tue Mar 16, 2010 7:35 pm | |
| WinRule..! What is the last question..?  |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Tue Mar 16, 2010 7:50 pm | |
| | its for printing numbers in circular fashion.. i think comment didn' print..! |
|
 | |
RC Active member


 | Subject: Re: Java Programs Wed Mar 17, 2010 9:28 am | |
| class Spiral { void main(int n) { int a[][]=new int[n][n];int c=1; for (int i = n-1, j = 0; i > 0; i--, j++) { for (int k = j; k < i; k++) a[j][k]=c++; for (int k = j; k < i; k++) a[k][i]=c++; for (int k = i; k > j; k--) a[i][k]=c++; for (int k = i; k > j; k--) a[k][j]=c++; } //special case for middle element if N is odd if (n % 2 == 1) a[(n-1)/2][(n-1)/2]=c++; for(int i=0;i<n;i++){ for(int j=0;j<n;j++) System.out.print(a[i][j]+" "); System.out.println();} }
} |
|
 | |
G-7 Active member


 | Subject: Re: Java Programs Wed Mar 17, 2010 9:33 am | |
| | 0k...now whts this RC,..? |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Wed Mar 17, 2010 10:13 am | |
| Queue
A queue is a FIFO structure and physically it can be implemented as an array or as a linked list. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that whenever an element is added, all elements that were added before have to be removed before the new element can be invoked. A queue is an example of a linear data structure.Insertions take place at the rear end and deletions take place at the front end.
Queue Implementation Theoretically, one characteristic of a queue is that it does not have a specific capacity. Regardless of how many elements are already contained, a new element can always be added. It can also be empty, at which point removing an element will be impossible until a new element has been added again. When a queue is created as an array, no. of elements is declared before processing. The beginning of the array becomes its front end and the end of the array becomes its rear end. The terms 'front' and 'rear' are used in describing a linear list only when it is implemented as a queue. Front stores the no. of first element in the queue and the rear stores the number of last element in the queue. The no. of elements in a queue at any given time can be calculated from the values of the front and rear. If front=0 then no.of elements = 0. else no. of elements = front - rear + 1 Linked Queues Linked queues are the queues having links among its elements. Variation in Queues Queues can be used in various forms and ways, depending on program requirements. Two popular variations are: 1) Circular queues 2) Dequeues
Circular Queues are the queues implemented in circle form rather than a straight line. They overcome the problem of unutilised space in linear queues implemented as arrays.
Dequeues (Double-ended queues) are the refined queues in which elements can be added or removed at either end but no in the middle. |
|
 | |
G-7 Active member


 | Subject: Re: Java Programs Wed Mar 17, 2010 10:31 am | |
| thanks WinRule..! This is good...!
Dont we have application of dequeue and circular queue..? Aren't there any changes to the Insertion and Deletion code when it comes to dequeue or circular queue..?
Is my question valid..? |
|
 | |
WinRrule Active member


 | Subject: Re: Java Programs Wed Mar 17, 2010 10:58 am | |
| | In specimen Paper Q12 is actually a program related to deque..! so even i'm not so sure nw.. I will ask my teacher nw..! |
|
 | |
|