Posts

Showing posts from January, 2013

Factorial

This is a program to find the factorial of a number import java.io.*; class factorial { long fact(int n) { long f=1; for(int i=1;i<=n;i++) f=f*i; return f; } public void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter the value of m and n"); int m=Integer.parseInt(br.readLine()); int n=Integer.parseInt(br.readLine()); long S=(fact(n)/fact(m))*fact(n-m); System.out.println("The value of S is "+S); }}

Prime number

This is a Java program to check whether a given number is prime or not. A prime number is a non-negative integer which is divisible only by 1 and itself. For example, Five being divisible by only one and five, is a prime number. Six, on the other hand is divisible by one, two, three and six and hence is not a prime number. PROGRAM CODE import java.io.*; class prime { public void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number"); int a=Integer.parseInt(br.readLine()); int flag=0;   for(int i=2;i<=a/2;i++)                                               { if (a%i==0) { flag=1; break;}} if(flag==0) System.out.println("PRIME"); else System.out.println("NOT PRIME"); }}

Palindrome

/*Write a program to input a number.Find the sum of digits of the number and also check whether the number is a palindrome or not  */ import java.io.*; class palindrome { public static void main() throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter a number"); int n=Integer.parseInt(br.readLine()); int d,p=0,s=0,h=n;   do { d=n%10; s=s+d; p=p*10+d; n=n/10; } while(n!=0); System.out.println("The sum of the digits of the given number = "+s); if(p==h) System.out.println("The number is a palindrome"); else System.out.println("The number is not a palindrome"); } }    

Automorphic number

/* Write a program to check whether an entered number is an automorphic number or not.  * A number is said to be an automorphic number if it is present in the last digits of its square  */ import java.io.*; class automorphic { public static void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int m,b,p; double r=0; System.out.println("Enter a number"); int n=Integer.parseInt(br.readLine()); m=n; p=m*m; int c=0; do { c++; m=m/10; }while(m!=0); int q= p%(int)Math.pow(10,c); if(n==q) System.out.println(n+" is an Automorphic number"); else System.out.println(n+" is not an Automorphic number"); }}

Bubble sort

/*  *Write a program to implement bubble sort on a list of names and numbers according to user's choice  */ import java.io.*; class bubble_sort { public void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your choice \n 1.Sorting names \n 2.Sorting numbers"); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1:System.out.println("Enter number of names"); int n=Integer.parseInt(br.readLine()); String s[]=new String[n]; System.out.println("Enter names"); for(int i=0;i<n;i++) s[i]=br.readLine(); String temp=""; for(int i=0;i<n-1;i++) { for(int j=0;j<n-i-1;j++) { if(s[j].compareTo(s[j+1])>0) { temp=s[j]; s[j]=s[j+1]; s[j+1]=temp; }}} System.out.println("Sorted array"); for(int i=0;i<n;i++) System.out.println(s[i]+" "); break; case 2:System.out.println("Enter number of numbers"); in

Selection sort

/*  *Write a program to implement selection sort on a list of names and numbers according to user's choice  */ import java.io.*; class sel_sort { public void main()throws IOException { BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your choice \n 1.Sorting names \n 2.Sorting numbers"); int ch=Integer.parseInt(br.readLine()); switch(ch) { case 1:System.out.print("Enter number of names:"); int n=Integer.parseInt(br.readLine()); String s[]=new String[n]; System.out.println("Enter names"); for(int i=0;i<n;i++) s[i]=br.readLine(); int p; String min,temp; for(int i=0;i<n;i++) { min=s[i]; p=i; for(int j=i+1;j<n;j++) { if(s[j].compareTo(min)<0) { min=s[j]; p=j; } } temp=s[i]; s[i]=s[p]; s[p]=temp; } System.out.println("Sorted array"); for(int i=0;i<n;i++) System.out.println(s[i]+" "); break; case 2:System.out.print("Enter number

Before going for Computer Application exam

Hey guys.....before you go for the ICSE Computer Applications exam, make sure you know how to do the following PART -I *  ALL FUNCTIONS *  CHARACTERISTICS OF JAVA *  DEFINITION OF POLYMORPHISM, ENCAPSULATION, INHERITANCE, ABSTRACTION *  CONSTRUCTOR AND FUNCTION OVERLOADING PART - II PROGRAM - 1 *  PRIME NUMBER *  PERFECT NUMBER *  SUM OF DIGITS OF NUMBER *  AUTOMORPHIC NUMBER *  PALINDROME *  ARMSTRONG *  INVERTED,RIGHT ANGLED TRIANGLE PATTERNS *  SUM OF THE SERIES *  FACTORIAL *  ODD EVEN TERMS *  NEGATIVE POSITIVE TERMS PROGRAM - 2 IF-ELSE(LIKE THAT OF FINDING FARES OF TAXIS) PROGRAM - 3 *  REVERSING A STRING *  CHANGING THE CASE  *  COUNTING THE VOWELS *  ASCII VALUE PRINTING *  CONSECUTIVE VALUE PRINTING *  CHANGING TO CONSECUTIVE CHARACTER *  FINDING LENGTH OF EACH WORD IN A STRING *  PRINTING FIRST LETTER *  PIG LATIN(Very important as it is confusing) PROGRAM - 4 ARRAY *  MAXIMUM AND