Posts

Showing posts from 2018

Series printing

Sum of the series (1/1!)+(1/2!)+(1/3!)+...+(1/n!) in Java /*Problem: Write a program in Java to find the sum of the series  * using function name fact(int) to return the factorial  * of the number in order to obtain each term.  * The series :-  * S=(1/1!)+(1/2!)+(1/3!)+...+(1/n!)  */ import java.util.*; class series2 {     public static void main()     {         System.out.print("Enter the value of n : ");         Scanner sc=new Scanner(System.in);         int n,i;         n=sc.nextInt();         double S=0,f=0;         for(i=1;i<n;i++)         {             f=fact(i);             S=S+(1/f);         }         System.out.println("The sum ="+S);     }         public static double fact(int num)     {         int f=1,j;         for(j=1;j<=num;j++)         {             f=f*j;         }         return(f);     } }

icse 2015 question on pattern printing

Image
Write two separate programs to generate the following patterns using iteration(loop) statements : (a) (b) class Pattern1 { public static void main(String args[]) { for(int i=1;i<=5;i++) { for(int j=1;j<=i;j++) { if(j%2!=0) System.out.print (“* “); else System.out.print (“# “); } System.out.println (); } } } class Pattern2 {     public static void main(String args[]) { int k = 5; for(int i=5;i>=0;i–) { k=5; for(int j=i;j>=0;j–) { System.out.print(k +” “); k–; } System.out.println (); } } }

WAP TO MERGE TWO ARRAY INTO SINGLE DIMENSION ARRAY

import java.util.*; class mergeapc {     public static void main(String args[])     {             int n,i,m,p;             Scanner sc=new Scanner(System.in);             System.out.println("Enter the no.of element for A");             n=sc.nextInt();             System.out.println("Enter the no.of element FOR B");             m=sc.nextInt();             System.out.println("Enter the no.of element FOR C");             p=sc.nextInt();             int a[]=new int[n];             int b[]=new int[m];             int c[]=new int[p];             System.out.println("Enter the array element for 1st");             for(i=0;i<n;i++)             {                     a[i]=sc.nextInt();             }             System.out.println("Enter the array element for 2nd");             for(i=0;i<m;i++)             {                     b[i]=sc.nextInt();             }             for(i=0;i<n;i++)             {          

WAP IN JAVA TO INSERT AN ELEMENT IN AN ARRAY

import java.util.*; class INSERT {     public static void main(String args[])     {             int n,t,j,i,p;             Scanner sc=new Scanner(System.in);             System.out.println("Enter the no.of element");             n=sc.nextInt();             int a[]=new int[50];             System.out.println("Enter the array element");             for(i=0;i<n;i++)             {                     a[i]=sc.nextInt();             }             System.out.println("Enter the ELEMENT to INSERTED");             int ins=sc.nextInt();             System.out.println("Enter the position to insert");             p=sc.nextInt();             for(i=n;i>p;i--)             {                 a[i]=a[i-1];             }             a[p]=ins;                       System.out.println("ARRAY element in inserted order");             for(i=0;i<n+1;i++)             {                 System.out.println(a[i]);             }

WAP TO DELETE AN ELEMENT IN AN ARRAY

import java.util.*; class delete {     public static void main(String args[])     {             int n,t,j,i,p;             Scanner sc=new Scanner(System.in);             System.out.println("Enter the no.of element");             n=sc.nextInt();             int a[]=new int[n];             System.out.println("Enter the array element");             for(i=0;i<n;i++)             {                     a[i]=sc.nextInt();             }             System.out.println("Enter the position to delete");             p=sc.nextInt();             for(i=0;i<n;i++)             {                 if(a[i]==p)                 {                                         for(j=i;j<n-1;j++)                     {                         a[j]=a[j+1];                     }                     n--;                     i--;                 }             }             System.out.println("ARRAY element in DELETED order");             for(i=

WAP IN JAVA TO FIND THE MAXIMUM AND MINIMUM ELEMENT IN AN ARRAY

import java.util.*; class MAX_MIN {     public static void main(String args[])     {         int n,i,max,min;         Scanner sc=new Scanner(System.in);         System.out.println("Enter the number");         n=sc.nextInt();         int a[]=new int[n];         System.out.println("Enter the array element");         for(i=0;i<n;i++)         {             a[i]=sc.nextInt();         }         max=a[0];         min=a[0];         for(i=1;i<n;i++)         {             if(a[i]>max)             {                 max=a[i];             }             else             {                 min=a[i];             }         }         System.out.println("Maximum element="+max);         System.out.println("Minimum element="+min);     } } OUTPUT: Enter the number 5 Enter the array element 1 2 3 4 5 Maximum element=5 Minimum element=1

WAP TO SEARCH THE ELEMENT I AN GIVEN ARRAY USING BINARY SEARCH

import java.util.*; class BINARY {     public static void main(String args[])     {         int n,s,i,f=0,lb,ub,mid;         Scanner sc=new Scanner(System.in);         System.out.println("Enter the number");         n=sc.nextInt();         int a[]=new int[n];         System.out.println("Enter the array element");         for(i=0;i<n;i++)         {             a[i]=sc.nextInt();         }         System.out.println("Enter the searched element");         s=sc.nextInt();         lb=0;         ub=n-1;         while(lb<=ub)         {             mid=(lb+ub)/2;             if(a[mid]==s)             {                 System.out.println(s+ "element is found at position="+(mid+1));                 f=1;                 break;             }             if(a[mid]>s)             {                 ub=mid-1;             }             else             {                 lb=mid+1;             }         }         if(f=

WAP TO SEARCH THE ELEMENT IN AN GIVEN ARRAY USING LINEAR SEARCH

import java.util.*; class linear {     public static void main(String args[])     {         int n,s,i,f=0;         Scanner sc=new Scanner(System.in);         System.out.println("Enter the number");         n=sc.nextInt();         int a[]=new int[n];         System.out.println("Enter the array element");         for(i=0;i<n;i++)         {             a[i]=sc.nextInt();         }         System.out.println("Enter the searched element");         s=sc.nextInt();         for(i=0;i<n;i++)         {             if(a[i]==s)             {                 f=1;                 break;             }         }         if(f==1)         {             System.out.println("element is found="+a[i]);             System.out.println("Position of the element="+(i+1));         }         else         {             System.out.println("ELEMENT IS NOT FOUND");         }     } }

WAP TO SORT THE GIVEN ARRAY USING INSERTION SORT

import java.util.*; class insersort {     public static void main(String args[])     {         Scanner sc= new Scanner(System.in);         int a[]=new int[50];         int n,i,temp,j;         System.out.print("Enter the number of elements");         n=sc.nextInt();         System.out.print("Enter the numbers");         for(i=0;i<n;i++)         {             a[i]=sc.nextInt();         }                 for ( i = 1 ; i <n ; i++ )         {         for ( j = 0 ; j < i ; j++ )         {             if ( a[j] > a[i] )             {                 temp=a[i];                 a[i]=a[j];                 a[j]=temp;               }           }         }         System.out.print("After sorting ");          for(i=0;i<n;i++)         {             System.out.println(a[i]);         }     } } OUTPUT: Enter the number of elements5 Enter the numbers6 0 4 1 9 After sorting 0 1 4 6 9

WAP IN JAVA TO SORT THE ELEMENT IN ASCENDING ORDER USING BUBBLE SORT

import java.util.*; class bubblesort {     public static void main(String args[])     {             int n,t,j,i;             Scanner sc=new Scanner(System.in);             System.out.println("Enter the no.of element");             n=sc.nextInt();             int a[]=new int[n];             System.out.println("Enter the array element");             for(i=0;i<n;i++)             {                     a[i]=sc.nextInt();             }             for(i=0;i<n-1;i++)             {                 for(j=0;j<n-i-1;j++)                 {                                         if(a[j]>a[j+1])                     {                         t=a[j];                         a[j]=a[j+1];                         a[j+1]=t;                     }                 }             }             System.out.println("ARRAY element in sorted order");             for(i=0;i<n;i++)             {                 System.out.println(a[i]);    

WAP IN JAVA TO SORT AN GIVEN ELEMENT IN AN ARRAY USING SELECTION SORT

import java.util.*; class selection {     public static void main(String args[])     {             int n,t,j,i;             Scanner sc=new Scanner(System.in);             System.out.println("Enter the no.of element");             n=sc.nextInt();             int a[]=new int[n];             System.out.println("Enter the array element");             for(i=0;i<n;i++)             {                     a[i]=sc.nextInt();             }             for(i=0;i<n-1;i++)             {                 for(j=i+1;j<n;j++)                 {                                       if(a[i]>a[j])                     {                         t=a[i];                         a[i]=a[j];                         a[j]=t;                     }                 }             }             System.out.println("ARRAY element in sorted order");             for(i=0;i<n;i++)             {                 System.out.println(a[i]);             }

Another way to declare and intialize array elements

class ajit{    public static void main(String args[]) {      int a[]={33,3,4,5};//declaration, instantiation and initialization      //printing array   for(int i=0;i<a.length;i++)//length is the property of array   System.out.println(a[i]);      } }  Output:33        3        4        5

Declaration,initialisation and printing of array elements

class Testarray{   public static void main(String args[]){      int a[]=new int[5];//declaration and instantiation   a[0]=10;//initialization   a[1]=20;   a[2]=70;   a[3]=40;   a[4]=50;      //printing array   for(int i=0;i<a.length;i++)//length is the property of array   System.out.println(a[i]);      }}   Output: 10        20        70        40        50

WAP in java to check whether a number is pronic number or not

import java.util.*; class loop { public static void main(String args[]) { Scanner ob=new Scanner(System.in); int a,n=0,k=0; System.out.println("Enter a"); a=ob.nextInt(); for(int i=1;i<=a;i++) { if(a==i*(i+1)) k=1; } if(k==1) System.out.println("Pronic or heteromecic Number"); else System.out.println("not Pronic Number"); } }

WAP in Java to check whether a number is niven number or not

import java.util.*; class Harshad_Number_or_Niven_no { public static void main(String args[]) { Scanner sc=new Scanner(System.in); int a,r,s=0,t; System.out.println("Enter the no."); a=sc.nextInt(); t=a; while(a>0) { r=a%10; s=s+r; a=a/10; } if(t%s==0) System.out.println("niven no."); else System.out.println("not niven no."); } }

WAP to check whether a number is neon number or not

import java.util.*; class loop{ public static void main(String args[]) { int n,r=0,m,d; Scanner ob=new Scanner(System.in); System.out.println("enter the no:"); n=ob.nextInt(); m=(n*n); while(m!=0) { d=m%10; r=r+d; m=m/10; } System.out.println(+r); if(r==n) System.out.println("neon no"); else System.out.println("not neon"); } }

WAP in JAVA to check whether a number is armstrong or not

import java.util.*; class loop{ public static void main(String args[]) { int a,s=0,n; Scanner ob=new Scanner(System.in); System.out.println("enter the no:"); n=ob.nextInt(); while(n!=0) { a=n%10; s=s+(a*a*a); n=n/10; } System.out.println(+s); if(n==s) System.out.println("armstrong no"); else System.out.println("not armstrong"); } }

WAP in JAVA to check whether a number is palindrome or not

import java.util.*; class loop{ public static void main(String args[]) { int n,k,s=0; Scanner ob=new Scanner(System.in); System.out.println("enter the nos:"); n=ob.nextInt(); while(n!=0) { k=n%10; s=((s*10) + k); n=n/10; } if(n==s) System.out.println("palindrome"); else System.out.println("not palindrome"); } } input:enter the nos:101 ouput:palindrome

WAP in Java to Enter the number a print sum of the digit

import java.util.*; class loop{ public static void main(String args[]) { int n,k,sum=0; Scanner ob=new Scanner(System.in); System.out.println("enter the nos:"); n=ob.nextInt(); while(n!=0) { k=n%10; sum=sum+k; n=n/10; } System.out.println("sum="+sum); } } input:enter the nos:123 ouput:sum=6

WAP in Java To Check Whether a number is Duck No.or not.

A Duck number is a number which has zeroes present in it,  but there should be no zero present in the beginning of the number. For example 3210, 7056, 8430709 are all duck numbers whereas 08237, 04309 are not. class duck { public static void main(String args[]) {     int n=50603;  int rem=0,p,x=0; while(n>0) { rem=n%10; if(rem==0) { x++; } else { p=rem; } n=n/10; } if(x>0) System.out.println("duck number "); else System.out.println("not duck number "); } }

WAP IN JAVA TO DECIDE WHETHER THE GIVEN NUMBER IS PRIME OR NOT

import java.util.*; class loop { public static void main(String args[]) { int n,c=0; Scanner ob=new Scanner(System.in); System.out.println("enter the no:"); n=ob.nextInt(); for(int i=1;i<=n;i++) { if(n%i==0) c++; } if(c==2) System.out.println("prime"); else System.out.println("Not prime"); } }

Basic program for loop

class loop { public static void main(String args[]) { for(int i=1;i<=10;i++) { System.out.println(i); } } }

A number is said to be Buzz Number if it ends with 7 or is divisible by 7. Example- 1007 is a Buzz Number as it end with 7.343 is also a Buzz Number as it is divisible by 7 and 77777 is also a Buzz Number as it ends with 7 and also it is divisible by 7.

import java.util.*; class if_else11{ public static void main(String args[]) { int n; Scanner ob=new Scanner(System.in); System.out.println("enter the no:"); n=ob.nextInt(); if(n%10==7 || n%100==7 || n%1000==7 && n%7==0) System.out.println("Buzz number"); else System.out.println("not Buzz number"); } }

WAP in Java to decide wether a number is special two digit no.or not

Write a  program in java  to accept a  number  and check whether the  number  is a special two digit  or not. A  special two - digit number  is such that when the sum of the  digits  is added to the product of its  digits , the result is equal to the original  two - digit number . import java.util.*; class if_else9{ public static void main(String args[]) { int n; Scanner ob=new Scanner(System.in); System.out.println("enter the number:"); n=ob.nextInt(); if((((n/10)+(n%10))+((n/10)*(n%10)))==n) System.out.println("special two digit"); else System.out.println("NOt spacial two digit"); } }

WAP in JAVA to decide whether a year is leap year or not.

import java.util.*; class leapyear{ public static void main(String args[]) { int y; Scanner ob=new Scanner(System.in); System.out.println("enter the year"); y=ob.nextInt(); if((y%4==0 && y%100!=0)|| y%400==0) System.out.println("leap year");  else  System.out.println("not leap year"); }

Few more Operators in Java

Prefix   :When increment or decrement operators are applied before the operant ,it is known as prefix operators. Postfix   : When increment or decrement operators are applied after the operant ,it is known as prefix operators. Increment Operator   :Increment operator increases the value of operand by one. Decrement Operator   : Decrement operator decreases the value of operand by one. Erythematic Operator   :An Operator which performs arithmetic functions is known as Erythematic Operator. Logical Operator   :These Operator yield results in 0 and 1 depending upon the outcome of the result. They check between two variables or values. Relational Operators   : Operators which are used to show or check relationship between operands are known as relational operators. Precedence of operators   : Specific  order in which the operators in an expression are evaluated when the expression has several . Bitwise Operator   : The Bitwise operators calculate each bit of their result

Operator and their Types in Java

Operator   :A symbol or a token which performs arthematicla or logical operations and yield a result. Operand   : An operator acts on different data items/entities called operands. Statement   : An expression which is assigned to a variable is completely known as statement. Unary Operator   :An arithmetical operator which is applied with a single operand is known as Unary operator. Binary Operator   :An arithmetic operator which deals with two operands, is known as Binary Operator. Ternary Operator  (or) Conditional Assignment Operator : :Ternary operator deals with three operands. The value assigned to a variable depends upon a logical expression.

Type Conversion In Java Programming

Type Conversion   : The process of converting one predefined type into another is called Type Conversion. Type Casting :  The explicit conversion of an operand to a specific type is called type casting. The operator that converts its operand to a specified type is called the typecast operator. Explicit Conversion :  an explicit type conversion is user defined that forces an expression to be of specific type. Coercion :  A implicit type conversion is a conversion performed by the compiler. The Java compiler converts all operands up to the type of the largest operand. This is also known as type promotion.

JVM Process During Complilation

Compiler   :A software which converts high level language to machine level language. It complies the instructions at once and lists all the errors. Interpreter   : A software which converts High level language to machine level language line by line .It does not move to next line until the error is corrected. Source Code   : A set of instructions written in high level language which is taken as an input from the user . Machine code   : The source code which is Complied/Interpreted is known as Machine code . Byte Code   : Byte code is the simplest Form of Source code. Source code is compiled to an intermediate code called Byte code. It can easily pass from one computer to another. Object Code   :The conversion of high-level language to a machine code with help of a translator known as Object code. Java Virtual Machine (JVM)   :It is a virtual processor which processes byte code to machine code for various platforms. So ,Java Interpreter is known as JVM. Compilation   :The

Write Java program to allow the user to input his/her age. Then the program will show if the person is eligible to vote. A person who is eligible to vote must be older than or equal to 18 years old.

import java.util.*;  class Vote { public static void main(String args[]) { int age; Scanner sc=new Scanner(System.in); System.out.print("What is your age?"); age=sc.nextInt(); if(age>=18) System.out.println("You are eligible to vote."); else System.out.println("You are not eligible to vote."); } } Output:What is your age? 18 You are eligible to vote.

A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade.

A school has following rules for grading system: a. Below 25 - F b. 25 to 45 - E c. 45 to 50 - D d. 50 to 60 - C e. 60 to 80 - B f. Above 80 - A Ask user to enter marks and print the corresponding grade. import java.util.Scanner ; class Ans { public static void main ( String [] args ){ Scanner s = new Scanner ( System . in ); System . out . println ( "Enter your marks" ); int x = s . nextInt (); if ( x < 25 ){ System . out . println ( "F" ); } else if (( x >= 25 )&&( x < 45 )){ System . out . println ( "E" ); } else if (( x >= 45 )&&( x < 50 )){ System . out . println ( "D" ); } else if (( x >= 50 )&&( x < 60 )){ System . out . println ( "C" ); } else if (( x >= 60 )&&( x < 80 )){ System . out . println ( "B" ); } else if (( x >= 80

WAP in Java and take values of length and breadth of a rectangle from user and check if it is square or not.

import java.util.Scanner ; class Ans { public static void main ( String [] args ){ Scanner s = new Scanner ( System . in ); System . out . println ( "Enter length" ); int x = s . nextInt (); System . out . println ( "Enter breadth" ); int y = s . nextInt (); if ( x == y ){ System . out . println ( "Square" ); } else { System . out . println ( "Rectangle" ); } } }

NETBEANS PROCESS OF WRITING THE CODE

WAP in JAVA to calculate factorial

Image
Q > So , basically what is factorial ? Def :  A factorial is a function that multiplies  number by every number. For example 4!= 4*3*2*1=24. The function is used, among other things, to find the number of ways “n” objects can be arranged. In mathematics, there are n! ( Factorial ways to arrange N Objects ) in sequence. Q > How to calculate ? For example : Consider : 2! = 2 x 1 = 2 The possibility of 2! is two ways like {2,1}, { 1,2 }. Same as like : 4! = 4 x 3 x 2 x 1 = 24. 24 = the arrangement of 4! is {1,2,3,4}, {2,1,3,4}, {2,3,1,4}, {2,3,4,1}, {1,3,2,4}, etc. Same as like 5! , 10! , N!. class Factoral { public static void main ( String arg [ ] ) {              int n = 5 , fact = 1 ;          for ( int i = 1 ; i <= n ; i ++ )       {         fact = fact * i ;    }          System . out . println ( "factoral=" + fact ) ; } } output: factorial = 120