Java HelpDesk

    Assignments of Strings in Java

  • 1.Enter a string from the user and count the length of the entered string.
      import java.util.Scanner;
    public class String_Length {
        public static void main(String[] args) {
    
            Scanner sc=new Scanner(System.in);
            String str;
            int n;
            System.out.println("Enter a string");
            str=sc.nextLine();
            n=str.length();
            System.out.println("The length of the string is "+n);
    
        }//end of main method
    }//end of class String_Length
    
         
  • 2.Enter a string "COMPUHELP" from the user and count the number of vowels in it.
      // Enter a string "COMPUHELP" from the user and count the number of vowels in it.
    
    import java.util.Scanner;
    
    public class Count_Vowels {
        public static void main(String[] args) {
    
            Scanner sc=new Scanner(System.in);
            String str;
    
            int count=0;
            int n;
    
            System.out.println("Enter a string");
            str=sc.nextLine();
            n=str.length();  //counting the length of the string.
    
            for(int i=0;i<n;i++)
            {
                if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
                {
    
                    count++;
    
                }
                
            }
            System.out.println("Number of vowels present in string are "+count);
    
        }//end of main method
    }//end of class Count_Vowels
    
         
  • Q3. Enter a string from the user in lower case and change all the vowels of the entered string in upper case.
      /*Enter a string from the user in lower case and change all the vowels of the entered
     string in upper case.*/
    
    import java.util.Scanner;
    
    public class V_UpperCase {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String str,str1="";
            int n,i;
            System.out.println("Enter a string in Lowercase");
            str=sc.nextLine();
            n=str.length();
    
            for(i=0;i<n;i++)
            {
                if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
                {
    
                    str1=str1+str.substring(i,i+1).toUpperCase();
    
                }//end of if block
                else
                {
                    str1=str1+str.substring(i,i+1);
    
                } //end of else block
    
            }//end of for loop
            System.out.println(str1);
    
        }//end of main method
    }//end of class V_UpperCase
    
         
  • Q4. Enter a string from the user and count the number of words present in the entered string.
     //Enter a string from the user and count the number of words present in the entered string.
    import java.util.Scanner;
    
    public class Count_Words {
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str;
            int n, i,space=0;
            System.out.println("Enter a string");
            str = sc.nextLine();
            n = str.length();
    
            for(i=0;i<n;i++)
            {
                if(str.charAt(i)==' ')
                {
                    space++;
                }
    
            }// end of for loop
    
            System.out.println("The number of words present in the string are "+(space+1));
    
        } //end of main method
    }//end of class Count_Words
         
  • Q5. Enter a String from the user and print those characters that they have space before them.
     //Enter a String from the user and print those characters that they have space before them.
    
    import java.util.Scanner;
    
    public class Space_Before_Character {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            String str;
            int n, i;
            System.out.println("Enter a string");
            str = sc.nextLine();
            n = str.length();
    
            for(i=0;i<n;i++)
            {
                 if(str.charAt(i)==' ')
                 {
                     System.out.println(str.charAt(i+1));
    
                 }
    
            }//end of for loop
    
        }//end of main method
    }//end of class Space_Before_Character
         
  • Q6. Enter a string from the user and count how many times only two vowels occur together in the entered string.
     /*Enter a string from the user and count how many times only two vowels occur
    together in the entered string.*/
    
    import java.util.Scanner;
    
    public class Two_Vowels_Together {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            String str;
            int n, i,count=0;
            System.out.println("Enter a string");
            str = sc.nextLine();
            n = str.length();
    
            for(i=0;i<n-1;i++)
            {
                if(str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
                {
                    if(str.charAt(i+1)=='a'||str.charAt(i+1)=='e'||str.charAt(i+1)=='i'||str.charAt(i+1)=='o'||str.charAt(i+1)=='u')
                    {
    
                        count++;
                    } //end of inner if
    
                }//end of outer if
            }//enf of for loop
            System.out.println("The number of vowels together are "+count);
        }//end of main method
    }//end of class Two_Vowels_Together
         
  • Q7. Enter a string from the user and print the first and last word of the entered string.
     //Enter a string from the user and print the first and last word of the entered string.
    
    import java.util.Scanner;
    
    public class First_Last_Word {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str;
            int wl=0;
            System.out.println("Enter a string");
            str = sc.nextLine();
            String str1[]=str.split(" ");
            wl=str1.length;
    
            System.out.println(str1[0]);
            System.out.println(str1[wl-1]);
        }//end of main method
    }//end of class First_Last_Word
         
  • Q8. Enter a string from the user in uppercase and change each vowel present at the end of the words of the entered string in lowercase.
    /*Enter a string from the user in uppercase and change each vowel present at the end of
            the words of the entered string in lowercase.*/
    
    import java.util.Scanner;
    
    public class Vowel_End_UpperCase {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String str,strcat="";
    
            int n,wl;
    
            System.out.println("Enter a string in Uppercase");
            str=sc.nextLine();
            String  str1[]=str.split(" ");
              wl=str1.length;
    
            for(int i=0;i<wl;i++)
            {
              n=str1[i].length();
    
              if(str1[i].charAt(n-1)=='A'||str1[i].charAt(n-1)=='E'||str1[i].charAt(n-1)=='I'||str1[i].charAt(n-1)=='O'||str1[i].charAt(n-1)=='U')
                {
                    strcat=strcat+str1[i].substring(0,n-1)+str1[i].substring(n-1).toLowerCase()+" ";
                }
              else
              {
                 strcat=strcat+str1[i]+" ";
              }
    
                }
            System.out.println(strcat);
            
        }//end of main method
        }//end of class Vowel_End_UpperCase 
         
  • Q9. Enter two strings from the user and check if both the strings are equal or not. If the two strings are equal then print "strings are equal" otherwise print "strings are not equal".
    /*Enter two strings from the user and check if both the strings are equal or not. If the two
           strings are equal then print "strings are equal" otherwise print "strings are not equal". */
    
    import java.util.Scanner;
    
    public class Two_Strings_Equal {
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            String str1,str2;
            System.out.println("Enter first string");
            str1=sc.nextLine();
            System.out.println("Enter second string");
            str2=sc.nextLine();
    
            if(str1.equalsIgnoreCase(str2))
            {
                System.out.println("Strings are equal");
            }
            else
            {
                System.out.println("Strings are not equal");
            }
        }// end of main method
    }//end of class Two_Strings_Equal
         
  • Q10. Enter a string from the user and concatenate the alternative words of the string together.
    //Enter a string from the user and concatenate the alternative words of the string together.
    
    import java.util.Scanner;
    
    public class Concat_Alternative_Words {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str;
            System.out.println("Enter a string");
            str=sc.nextLine();
            int n,wl;
            n=str.length();
    
            String str1[]=str.split(" ");
            wl=str1.length;
    
            for(int i=0;i<wl;i++)
            {
                if(i%2==0)
                {
                    System.out.print(str1[i]+" ");
                }
    
            }
            
        }//end of main method
    }//end of class Concat_Alternative_Words
         
  • Q11. Enter a string from the user in lowercase and capitalize each word of the string.
    import java.util.Scanner;
    
    //Enter a string from the user in lowercase and capitalize each word of the string.
    public class Capitalize_Each_Word {
        public static void main(String[] args) {
    
            Scanner sc = new Scanner(System.in);
            String str,strcat="";
            System.out.println("Enter a string in lowercase");
            str=sc.nextLine();
    
            String str1[]=str.split(" ");
            int wl,n;
            wl=str1.length;
    
            for(int i=0;i<wl;i++)
            {
                n=str1[i].length();
              strcat=strcat+str1[i].substring(0,1).toUpperCase()+str1[i].substring(1,n)+" ";
            }
            System.out.println(strcat);
        }//end of main method
    }//end of class Capitalize_Each_Word
         
  • Q12. Enter a string "Compuhelp" from the user and print only "help" from the entered string.
    //Enter a string "Compuhelp" from the user and print only "help" from the entered string.
    
    import java.util.Scanner;
    
    public class Print_Help {
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            String str,strcat="";
            System.out.println("Enter a string");
            str=sc.nextLine();
            int n=str.length();
            System.out.println(str.substring(5,n));
    
        }//end of main method
    }//end of class Print_Help
         
  • Q13. Enter a string from the user and find the first and last index number of the character entered by the user.
    /*Enter a string from the user and find the first and last index number of the character
            entered by the user.*/
    
    import java.util.Scanner;
    
    public class Index_First_Last {
        public static void main(String[] args) {
    
            Scanner sc=new Scanner(System.in);
            String str;
            char ch;
            System.out.println("Enter A string");
            str=sc.nextLine();
            System.out.println("Enter a character");
            ch=sc.next().charAt(0);
            System.out.println("The first index number of the entered character is "+str.indexOf(ch));
            System.out.println("The last index number of the entered character is "+str.lastIndexOf(ch));
    
    
        }//end of main method
    }//end of class Index_First_Last
         
  • Q14. Enter a string "Compuhelp" from the user and replace the string "help" with "ter" so that the entered string become "Computer".
    import java.util.Scanner;
    public class String_Replace {
        public static void main(String[] args)
        {
            String str1=new String();
            String str2=new String();
            Scanner sc=new Scanner(System.in);
            System.out.println("Enter a string ");
            str1=sc.nextLine();
            str2=str1.replace("help","ter");
            System.out.println(str2);
        }//end of main method
    }//end of class String_Replace
         
  • Assignments of If and If..Else Statements


  • 1. Enter the marks from the user and display corresponding Grades:
         Marks        Grade
         100-80        A
         80-60          B
         60-40          C
         Below 40    Fail
     	import java.util.Scanner;
    	public class if_statements
    	{
        	public static void main(String[] args)
        	{
           		 Scanner scr=new Scanner(System.in);
           		 int marks;
            	 System.out.println("Enter marks ");
            	 marks=scr.nextInt();
            	 if(marks>=80 && marks<=100)
            	 {
                	System.out.println("A Grade");
            	 }
            	 else if(marks>=60 && marks<80)
            	 {
                	System.out.println("B Grade");
            	 }
            	 else if(marks>=40 && marks<60)
            	 {
                	System.out.println("C Grade");
            	 }
            	 else if(marks>=0 && marks<40)
            	 {
               		System.out.println("Fail");
            	 }
    
        	 }//end of main
    	 }//end of class
    
    
  • 2. Accept a number from the user and check if it is positive or negative or zero.
     
    import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number ");
            num=scr.nextInt();
            if(num>0)
            {
                System.out.println("Number is Positive");
            }
            else if(num<0)
            {
                System.out.println("Number is Negative");
            }
            //else if(num==0)
            else
            {
                System.out.println("Number is Zero");
            }
     	}//end of main
    }//end of class
     
  • 3. Accept a number from the user and check if it is odd or even.
    import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number ");
            num=scr.nextInt();
        	if(num%2==0)
        	{
            	System.out.println("Number is even");
        	}
       	    else
        	{
            	System.out.println("Number is odd");
        	} 
    	}//end of main
    }//end of class
      
  • 4. Accept a character from the user and check if it is vowel or consonent.
     import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            char ch;
            Scanner scr=new Scanner(System.in);
            System.out.println("Enter any character ");
            ch=scr.next().charAt(0);
            if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
            {
                System.out.println("character is vowel");
            }
            else
            {
                System.out.println("character is consonent");
            }
    	}//end of main
    }//end of class
    
  • 5. Accept a character from the user and check if it is vowel or consonent or special character.
     import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            char ch;
            Scanner scr=new Scanner(System.in);
            System.out.println("Enter any character ");
            ch=scr.next().charAt(0);        		if(ch=='a'||ch=='A'||ch=='e'||ch=='E'||ch=='i'||ch=='I'||ch=='o'||ch=='O'||ch=='u'||ch=='U')
            {
                System.out.println("character is vowel");
            }
            else if(ch=='@'||ch=='!'||ch=='#'||ch=='%'||ch=='&'||ch=='*')
            {
                System.out.println("character is special");
            }
            else
            {
                System.out.println("character is consonent");
            }
        }//end of main
    }//end of class
    
  • 6. Accept a number between 1 and 7 and print the corresponding week day.If the number is out of range, print message "Wrong Input".
      import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number ");
            num=scr.nextInt();
            if(num==1)
            {
                System.out.println("Day is Monday");
            }
            else if(num==2)
            {
                System.out.println("Day is Tuesday");
            }
            if(num==3)
            {
                System.out.println("Day is Wednesday");
            }
            if(num==4)
            {
                System.out.println("Day is Thursday");
            }
            if(num==5)
            {
                System.out.println("Day is Friday");
            }
            if(num==6)
            {
                System.out.println("Day is Saturday");
            }
            if(num==7)
            {
                System.out.println("Day is Sunday");
            }
        }//end of main
    }//end of class
    
    urn 0;
     }
  • 7. Accept a number between 0 to 9 and print the words equivalent".
    import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number between 0 to 9");
            num=scr.nextInt();
            if(num==0)
            {
                System.out.println("You entered Zero");
            }
            else if(num==1)
            {
                System.out.println("You entered One");
            }
            else if(num==2)
            {
                System.out.println("You entered Two");
            }
            else if(num==3)
            {
                System.out.println("You entered Three");
            }
            else if(num==4)
            {
                System.out.println("You entered Four");
            }
            else if(num==5)
            {
                System.out.println("You entered Five");
            }
            else if(num==6)
            {
                System.out.println("You entered Six");
            }
            else if(num==7)
            {
                System.out.println("You entered Seven");
            }
            else if(num==8)
            {
                System.out.println("You entered Eight");
            }
            else if(num==9)
            {
                System.out.println("You entered Nine");
            }
            else
            {
                System.out.println("wrong input");
            }
        }//end of main
    }//end of class
    
  • 8. Accept a number between 1 and 12 and print the month it represents in words.
    import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number between 1 and 12:");
            num=scr.nextInt();
            if(num==1)
            {
                System.out.println("You entered January");
            }
            else if(num==2)
            {
                System.out.println("You entered Feburary");
            }
            else if(num==3)
            {
                System.out.println("You entered March");
            }
            else if(num==4)
            {
                System.out.println("You entered April");
            }
            else if(num==5)
            {
                System.out.println("You entered May");
            }
            else if(num==6)
            {
                System.out.println("You entered June");
            }
            else if(num==7)
            {
                System.out.println("You entered July");
            }
            else if(num==8)
            {
                System.out.println("You entered August");
            }
            else if(num==9)
            {
                System.out.println("You entered September");
            }
            else if(num==10)
            {
                System.out.println("You entered October");
            }
            else if(num==11)
            {
                System.out.println("You entered November");
            }
            else if(num==12)
            {
                System.out.println("You entered December");
            }
            else
            {
                System.out.println("wrong input");
            }
        }//end of main
    }//end of class
    
    
  • 9. Accept a number between 1 and 5.If the user:
    Enter1: add two numbers
    Enter2: subtract two numbers
    Enter3: division of two numbers
    Enter4: multiplication of two numbers
    Enter5: exit the program
        import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int choice,num1,num2,res;
            System.out.println("Press 1 for addition");
            System.out.println("Press 2 for subtraction");
            System.out.println("Press 3 for multiplication");
            System.out.println("Press 4 for division");
            System.out.println("Press 5 for exit");
            choice=scr.nextInt();
            if(choice<=4)
            {
                System.out.println("Enter first number:");
                num1 = scr.nextInt();
                System.out.println("Enter second number:");
                num2 = scr.nextInt();
                if (choice == 1)
                {
                    res = num1 + num2;
                    System.out.println("The sum is: " + res);
                } 
                else if (choice == 2) 
                {
                    res = num1 - num2;
                    System.out.println("The difference is: " + res);
                } 
                else if (choice == 3)
                {
                    res = num1 * num2;
                    System.out.println("The multiplication is: " + res);
                } 
                else if (choice == 4) 
                {
                    res = num1 / num2;
                    System.out.println("The division is: " + res);
                } 
                else if (choice == 5) 
                {
                    System.exit(0);
                }
            }
        }//end of main
    }//end of class
    
      
  • 10. Enter three numbers and check the greatest out of the three.
    import java.util.Scanner;
    public class if_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int a, b ,c;
            System.out.println("Enter first number: ");
            a= scr.nextInt();
            System.out.println("Enter second number: ");
            b= scr.nextInt();
            System.out.println("Enter third number: ");
            c= scr.nextInt();
            if(a>b && a>c)
            {
                System.out.println("First number is greater");
            }
            else if(b>a && b>c)
                {
                    System.out.println("Second number is greater");
                }
            else
            {
                   System.out.println("Third number is greater");
            }
        }//end of main
    }//end of class
    
  • Assignments of Switch Statement


  • 1. To accept a number between 0 to 9 and print the word equivalent.
    import java.util.Scanner;
    public class switch_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number between 0 and 9:");
            num=scr.nextInt();
            switch(num)
            {
                case 0:
                    System.out.println("You entered ZERO");
                    break;
                case 1:
                    System.out.println("You entered ONE");
                    break;
                case 2:
                    System.out.println("You entered TWO");
                    break;
                case 3:
                    System.out.println("You entered THREE");
                    break;
                case 4:
                    System.out.println("You entered FOUR");
                    break;
                case 5:
                    System.out.println("You entered FIVE");
                    break;
                case 6:
                    System.out.println("You entered SIX");
                    break;
                case 7:
                    System.out.println("You entered SEVEN");
                    break;
                case 8:
                    System.out.println("You entered EIGHT");
                    break;
                case 9:
                    System.out.println("You entered NINE");
                    break;
                default:
                    System.out.println("wrong input");
            }
        }//end of main
    }//end of class
    
    
         
  • 2. To accept a number between 1 to 12 and print month equivalent.
      import java.util.Scanner;
    public class switch_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number between 1 and 12:");
            num=scr.nextInt();
            switch(num)
            {
                case 1:
                    System.out.println("You entered JANUARY");
                    break;
                case 2:
                    System.out.println("You entered FEBURARY");
                    break;
                case 3:
                    System.out.println("You entered MARCH");
                    break;
                case 4:
                    System.out.println("You entered APRIL");
                    break;
                case 5:
                    System.out.println("You entered MAY");
                    break;
                case 6:
                    System.out.println("You entered JUNE");
                    break;
                case 7:
                    System.out.println("You entered JULY");
                    break;
                case 8:
                    System.out.println("You entered AUGUST");
                    break;
                case 9:
                    System.out.println("You entered SEPTEMBER");
                    break;
                case 10:
                    System.out.println("You entered OCTOBER");
                    break;
                case 11:
                    System.out.println("You entered NOVEMBER");
                    break;
                case 12:
                    System.out.println("You entered DECEMBER");
                    break;
                default:
                    System.out.println("wrong input");
            }
        }//end of main
    }//end of class
    
    
         
  • 3. To accept a number between 1 to 7 and print days equivalent.
    import java.util.Scanner;
    public class switch_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num;
            System.out.println("Enter any number between 1 and 7:");
            num=scr.nextInt();
            switch(num)
            {
                case 1:
                    System.out.println("You entered SUNDAY");
                    break;
                case 2:
                    System.out.println("You entered MONDAY");
                    break;
                case 3:
                    System.out.println("You entered TUESDAY");
                    break;
                case 4:
                    System.out.println("You entered WEDNESDAY");
                    break;
                case 5:
                    System.out.println("You entered THURSDAY");
                    break;
                case 6:
                    System.out.println("You entered FRIDAY");
                    break;
                case 7:
                    System.out.println("You entered SATURDAY");
                    break;
                default:
                    System.out.println("wrong input");
            }
    
        }//end of main
    }//end of class
    
         
  • 4. Accept a character and check whether it is vowel or not.
    import java.util.Scanner;
    public class switch_statements
    {
        public static void main(String[] args)
        {
            char ch;
            Scanner scr=new Scanner(System.in);
            System.out.println("Enter any character ");
            ch=scr.next().charAt(0);
            switch(ch)
            {
                case 'a':
                    System.out.println("Character is vowel");
                    break;
                case 'e':
                    System.out.println("Character is vowel");
                    break;
                case 'i':
                    System.out.println("Character is vowel");
                    break;
                case 'o':
                    System.out.println("Character is vowel");
                    break;
                case 'u':
                    System.out.println("Character is vowel");
                    break;
                default:
                    System.out.println("character is consonent");
            }
    
        }//end of main
    }//end of class
    
    
         
  • 5. Accept a number between 1 and 5.If the user:
    Enter1: add two numbers
    Enter2: subtract two numbers
    Enter3: division of two numbers
    Enter4: multiplication of two numbers
    Enter5: exit the program
    import java.util.Scanner;
    public class switch_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int num1,num2,res;
            int value;
            System.out.println("Enter first number: ");
            num1=scr.nextInt();
            System.out.println("Enter second number: ");
            num2=scr.nextInt();
            System.out.println("Press 1 for addition");
            System.out.println("Press 2 for subtraction");
            System.out.println("Press 3 for division");
            System.out.println("Press 4 for multilication");
            System.out.println("Press 5 for EXIT");
            value=scr.nextInt();
            switch(value)
            {
                case 1:
                    res=num1+num2;
                    System.out.println("The sum is: "+res);
                    break;
    
                case 2:
                    res=num1-num2;
                    System.out.println("The subtraction is: "+res);
                    break;
    
                case 3:
                    res=num1/num2;
                    System.out.println("The division is: "+res);
                    break;
    
                case 4:
                    res=num1*num2;
                    System.out.println("The multiplication is: "+res);
                    break;
    
                default:
                    System.exit(0);
            }
        }//end of main
    }//end of class
         
  • 6. Accept a number between 1 and 5.If the user:
    Enter1: for Fibonacci series
    Enter2: for Factorial
    Enter3: for printing even series
    Enter4: for printing odd series
    Enter5: for Exit
    import java.util.Scanner;
    public class switch_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int x=0,y=1,z,i,fac=1;
            int value;
            System.out.println("Press 1 for fibbonacci series");
            System.out.println("Press 2 for factorial");
            System.out.println("Press 3 for printing even series");
            System.out.println("Press 4 for printing odd series");
            System.out.println("Press 5 for EXIT");
            value=scr.nextInt();
            switch(value)
            {
                case 1:
                    System.out.print("Fibbonacci series is:");
                    System.out.print(x+" "+y);
                    for(i=1;i<=8;i++)
                    {
                        z=x+y;
                        x=y;
                        y=z;
                        System.out.print(" "+z);
                    }
                    break;
                case 2:
                    for(i=1;i<=5;i++)
                    {
                        fac=fac*i;
                    }
                    System.out.print("factorial of 5 is: "+fac);
                    break;
                case 3:
                    System.out.print("even series is: ");
                    for(i=0;i<=10;i=i+2)
                    {
                        System.out.print(i+" ");
                    }
                    break;
                case 4:
                    System.out.print("odd series is: ");
                    for(i=1;i<=10;i=i+2)
                    {
                        System.out.print(i+" ");
                    }
                case 5:
                    System.exit(0);
                    break;
                default:
                    System.out.print("wrong input,Please try again");
            }
    
        }//end of main
    }//end of class       
         
  • 7. Accept a number between 1 and 5.If the user:
    Enter1: print even numbers starting from 0 till the user wants
    Enter2: print odd numbers starting from 0 till the user wants
    Enter3: print the square of the number entered by the user
    Enter4: print the character entered by the user,till the user wants
    Enter5: for Exit
    import java.util.Scanner;
    public class switch_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int value,n;
            char a;
            int x=0,y=1,z,i,fac=1;
            System.out.println("Press 1 for even numbers");
            System.out.println("Press 2 for odd numbers");
            System.out.println("Press 3 for square of the number");
            System.out.println("Press 4 for character series");
            System.out.println("Press 5 for EXIT");
            value=scr.nextInt();
            switch(value)
            {
                case 1:
                    System.out.println("Enter nth number: ");
                    n=scr.nextInt();
                    System.out.println("even series is: ");
                    for(i=0;i<=n;i=i+2)
                    {
                        System.out.print(i+" ");
                    }
                    break;
                case 2:
                    System.out.println("Enter nth number: ");
                    n=scr.nextInt();
                    System.out.println("odd series is: ");
                    for(i=1;i<=n;i=i+2)
                    {
                        System.out.print(i+" ");
                    }
                    break;
                case 3:
                    System.out.println("Enter nth number: ");
                    n=scr.nextInt();
                    System.out.print((n*n));
                    break;
                case 4:
                    System.out.println("Enter any character in lowercase: ");
                    a=scr.next().charAt(0);
                    for(i=97;a<=122;a++)
                    {
                        System.out.print(a+" ");
                    }
                    break;
                case 5:
                    System.exit(0);
                    break;
                default:
                {
                    System.out.println("wrong input");
                }
            }
        }//end of main
    }//end of class
    
    
                 
  • Assignments of For Loop


  • 1. Print the following pattern:
    **********
    public class loop_statements
    {
        public static void main(String[] args)
        {
            int i;
            for(i=1;i<=10;i++)
            {
                System.out.print("*");;
            }
        }//end of main
    }//end of class
    
    
  • 2. Print the following pattern:
    *
    *
    *
    *
    *
    *
    *
    public class loop_statements
    {
        public static void main(String[] args)
        {
            int i;
            for(i=1;i<=10;i++)
            {
                System.out.println("*");;
            }
        }//end of main
    }//end of class
        
  • 3. Print the following pattern:
           *
    COMPUHELP
           *
    COMPUHELP
           *
    COMPUHELP
           *
    COMPUHELP
           *
    COMPUHELP
           *
    COMPUHELP
    public class loop_statements
    {
        public static void main(String[] args)
        {
            int i;
            for(i=1;i<=6;i++)
            {
                System.out.println("\t*");
                System.out.println("COMPUHELP\n");
            }
        }//end of main
    }//end of class
  • 4. Print the series 0 to 9 using loops.
    public class loop_statements
    {
        public static void main(String[] args)
        {
            int i;
            for(i=0;i<=9;i++)
            {
                System.out.print(i+" ");
             }
        }//end of main
    }//end of class
  • 5. Print the series 9 to 0 using loops.
    public class loop_statements
    {
        public static void main(String[] args)
        {
        	int i;
        	for(i=9;i>=0;i--)
        	{
        		System.out.print(i+" ");
    
        	}
      }//end of main
    }//end of class
    
  • 6. Print the series 5 to 20 using loops.
    public class loop_statements
    {
        public static void main(String[] args)
        {
            int i;
            for(i=5;i<=20;i++)
            {
                System.out.print(i+" ");
            }
        }//end of main
    }//end of class
  • 7. Print the series 45 to 7 using loops.
    public class loop_statements
    {
        public static void main(String[] args)
        {
        	int i;
        	for(i=45;i>=7;i--)
        	{
        		System.out.print(i+" ");
    
        	}
      }//end of main
    }//end of class
  • 8. Print the series of odd numbers till 20 using loops.
    public class loop_statements
    {
        public static void main(String[] args)
        {
        	int i;
        	for(i=1;i<=20;i+=2)
        	{
      	 	 	System.out.print(i+" ");
        	}
        }//end of main
    }//end of class
  • 9. Print the series of even numbers till 20 using loops.
    public class loop_statements
    {
        public static void main(String[] args)
        {
        	int i;
        	for(i=0;i<=20;i+=2)
        	{
      	 	 	System.out.print(i+" ");
        	}
        }//end of main
    }//end of class
  • 10. Print the series of odd numbers till the user wants using loops.
    import java.util.Scanner;
    public class loop_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int i,num;
            System.out.println("Enter the limit ");
            num=scr.nextInt();
            for(i=1;i<=num;i+=2)
            {
                System.out.print(i+" ");
            }
        }//end of main
    }//end of class
    
    
  • 11. Print the series of even numbers till the user wants using loops.
    import java.util.Scanner;
    public class loop_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int i,num;
            System.out.println("Enter the limit ");
            num=scr.nextInt();
            for(i=0;i<=num;i+=2)
            {
                System.out.print(i+" ");
            }
        }//end of main
    }//end of class
  • 12. Print the series : 0 3 6 9 12 15 18......50
    public class loop_statements
    {
        public static void main(String[] args)
        {
            int i,num;
            for(i=0;i<=50;i+=3)
            {
                System.out.print(i+" ");
            }
        }//end of main
    }//end of class
    
  • 13. Print the fibonacci series : 0 1 1 2 3 5 8 13.....n
    import java.util.Scanner;
    public class loop_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int i,num,a=0,b=1,res=0;
            System.out.println("Enter the limit ");
            num=scr.nextInt();
            System.out.println(a+" "+b);
            for(i=0;res<=num;i++)
            {
                res=a+b;
                a=b;
                b=res;
                System.out.print(" "+res);
            }
        }//end of main
    }//end of class
    
  • 14. Print the series : 1 2 4 8 16 32.....n
    import java.util.Scanner;
    public class loop_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int i,num,prod=1;
            System.out.println("Enter the limit ");
            num=scr.nextInt();
            for(i=0;prod<=num;i++)
            {
                System.out.print(prod+" ");
                prod=prod*2;
            }
        }//end of main
    }//end of class
    
    
  • 15. Print the factorial of the number entered by the user.Output should be: 1*2*3*4*5=120.
    import java.util.Scanner;
    public class loop_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int i,num,fact=1;
            System.out.println("Enter the number whose factorial you want to print: ");
            num=scr.nextInt();
            for(i=1;i<=num;i++)
            {
                fact=fact*i;
                System.out.print(i+"*");
            }
            System.out.print("\b="+fact);
        }//end of main
    }//end of class
    
    
  • 16. Print the sum of the number entered by the user.Output should be: 1+2+3+4+5=15.
    import java.util.Scanner;
    public class loop_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int i,num,sum=0;
            System.out.println("Enter the number whose sum of series you want to print: ");
            num=scr.nextInt();
            for(i=1;i<=num;i++)
            {
                sum=sum+i;
                System.out.print(i+"+");
            }
            System.out.print("\b="+sum);
        }//end of main
    }//end of class
    
    
  • 17. Print the result of mn, where m and n are entered by the user.
    import java.util.Scanner;
    public class loop_statements
    {
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int m,n,i,res=1;
            System.out.println("Enter the value for m ");
            m=scr.nextInt();
            System.out.println("Enter the value for n ");
            n=scr.nextInt();
            for(i=1;i<=n;i++)
            {
                res=res*m;
            }
            System.out.print(res);
        }//end of main
    }//end of class
    
    
  • Assignments of Nested For Loop

  • 1.   ***
          ***
          ***
    public class NestedForLoop
    {
        public static void main(String[] args) {
            int row,col;
            for(row=1;row<=3;row++)
            {
                for(col=1;col<=3;col++)
                {
                    System.out.print("*");
                }//end of inner loop
                System.out.println();
            }//end of outer loop
        }//end of main
    }//end of class
    
  • 2.  111
          111
          111
    public class NestedForLoop
    {
        public static void main(String[] args) {
            int row,col,k=1;
            for(row=1;row<=3;row++)
            {
                for(col=1;col<=3;col++)
                {
                    System.out.print(k);
                }//end of inner loop
                System.out.println();
            }//end of outer loop
        }//end of main
    }//end of class
    
  • 3.  123
          123
          123
    public class NestedForLoop
    {
        public static void main(String[] args) {
            int row,col;
            for(row=1;row<=3;row++)
            {
                for(col=1;col<=3;col++)
                {
                    System.out.print(col);
                }//end of inner loop
                System.out.println();
            }//end of outer loop
        }//end of main
    }//end of class
  • 4.  111
          222
          333
    public class NestedForLoop
    {
        public static void main(String[] args) {
            int row,col;
            for(row=1;row<=3;row++)
            {
                for(col=1;col<=3;col++)
                {
                    System.out.print(row);
                }//end of inner loop
                System.out.println();
            }//end of outer loop
        }//end of main
    }//end of class
  • 5.  123
          456
          789
    public class NestedForLoop
    {
        public static void main(String[] args) {
            int row,col,k=1;
            for(row=1;row<=3;row++)
            {
                for(col=1;col<=3;col++)
                {
                    System.out.print(k);
                    k++;
                }//end of inner loop
                System.out.println();
            }//end of outer loop
        }//end of main
    }//end of class
  • 6.  333
          222
          111
    public class NestedForLoop
    {
        public static void main(String[] args) {
        int row,col;
        for(row=3;row>=1;row--)
        {
        	for(col=1;col<=3;col++)
        	{
        		System.out.print(row);
        	}//end of inner loop
        	System.out.println();
        }//end of outer loop
      }//end of main
    }//end of class
  • 7.  987
          654
          321
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        	int row,col,k=9;
        	for(row=3;row>=1;row--)
        	{
        		for(col=1;col<=3;col++)
        		{
        			System.out.print(k);
        			k--;
        		}//end of inner loop
        		System.out.println();
        }//end of outer loop
      }//end of main
    }//end of class
  • 8.  321
          321
          321
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        	int row,col;
        	for(row=3;row>=1;row--)
        	{
        		for(col=3;col>=1;col--)
        		{
        			System.out.print(col);
        		}//end of inner loop
        		System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 9.  *
          **
          ***
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=1;row<=3;row++)
        {
        	for(col=1;col<=row;col++)
        	{
        		System.out.print("*");
        	}//end of inner loop
             System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 10.  ***
            **
            *
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=3;row>=1;row--)
        {
        	for(col=1;col<=row;col++)
        	{
        		System.out.print("*");
        	}//end of inner loop
        		System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 11.  1
           12
           123
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=1;row<=3;row++)
        {
        	for(col=1;col<=row;col++)
        	{
        		System.out.print(col);
        	}//end of inner loop
        		System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 12.  1
            22
            333
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=1;row<=3;row++)
        {
        	for(col=1;col<=row;col++)
        	{
        		System.out.print(row);
        	}//end of inner loop
        		System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 13.  3
            22
            111
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=3;row>=1;row--)
        {
      	  for(col=3;col>=row;col--)
          {
    	     System.out.print(row);
        	}//end of inner loop
        		System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 14.  111
            22
            3
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=1;row<=3;row++)
        {
        	for(col=3;col>=row;col--)
        	{
        		System.out.print(row);
        	}//end of inner loop
        		System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 15.  3
            23
            123
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=3;row>=1;row--)
        {
        	for(col=row;col<=3;col++)
        	{
        		System.out.print(col);
        	}//end of inner loop
        	System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 16.  3
            32
            321
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=3;row>=1;row--)
        {
        	for(col=3;col>=row;col--)
        	{
        		System.out.print(col);
        	}//end of inner loop
        	System.out.println();
        	}//end of outer loop
        }//end of main
    }//end of class
  • 17.  ***
             **
               *
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col,k;
        for(row=1;row<=3;row++)
        {
      	  for(k=row;k>=2;k--)
          {
          	 System.out.print(" ");
          }
          for(col=3;col>=row;col--)
          {
        	 System.out.print("*");
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • 18.  123
               12
                 1
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col,k;
        for(row=3;row>=1;row--)
        {
      	  for(k=2;k>=row;k--)
          {
     	     System.out.print(" ");
          }
          for(col=1;col<=row;col++)
          {
        	 System.out.print(col);
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • 19.  111
             22
               3
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col,k;
        for(row=1;row<=3;row++)
        {
        	for(k=row;k>=2;k--)
        	{
        		System.out.print(" ");
        	}
        	for(col=row;col<=3;col++)
        	{
        		System.out.print(row);
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • 20.  *
            *  *
            *  *  *
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        for(row=1;row<=3;row++)
        {
        	for(col=1;col<=row;col++)
        	{
        		System.out.print("* ");
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • 21.       *
              *   *
            *  *  *
          *  *  *  *
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
         int i,j,k;
         for(i=1;i<=4;i++)
         {
         	for(k=3;k>=i;k--)
         	{
            	System.out.print(" ");
         	}
          	for(j=1;j<=i;j++)
          	{
            	System.out.print("* ");
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • 22.  A
            A  B
            A  B  C
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        //65 is ASCII value of A
        for(row=65;row<=67;row++)
        {
        	for(col=65;col<=row;col++)
        	{
        		System.out.print((char)col);
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • 23.  C B A
             C B
             C
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
        int row,col;
        //65 is ASCII value of A
        for(row=65;row<=67;row++)
        {
        	for(col=67;col>=row;col--)
        	{
        		System.out.print((char)col);
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • 24.      A
              A   B
            A  B  C
    public class NestedForLoop
    {
        public static void main(String[] args)
        {
    
        int i,j,k;
         //65 is ASCII value of A
         for(i=65;i<=67;i++)
         {
         	for(k=66;k>=i;k--)
         	{
            	System.out.print(" ");
         	}
          	for(j=65;j<=i;j++)
          	{
            	System.out.print((char)j);
           }//end of inner loop
        	System.out.println();
         }//end of outer loop
       }//end of main
    }//end of class
  • Assignments of While and Do While Loops

  • 1. To print the series starting from zero till the user wants:
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
            int i, m=0,n;
            System.out.println("Enter nth number: ");
            n=scr.nextInt();
            while(m<=n)
            {
                System.out.print(m+" ");
                m++;
            }
        }//end of main
    }//end of class
  • 2. To print the series starting from number enter by user till 0:
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
        int i,m;
        System.out.println("Enter starting number: ");
        m=scr.nextInt();
        while(m>=0)
        {
        	System.out.print(m+" ");
        	m--;
        }
       }//end of main
    }//end of class
  • 3. To generate table of entered value by the user:
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        	Scanner scr=new Scanner(System.in);
        	int i,n,cnt=1;
        	System.out.println("Enter any number: ");
        	n=scr.nextInt();
        	while(cnt<=10)
        	{
    			System.out.println(n+"*"+cnt+"="+(n*cnt));
        		cnt++;
        	}
        }//end of main
    }//end of class
  • 4. To print the fabonacci series till the user wants:
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
        int i,num,x=0,y=1,z=1;
        System.out.println("Enter any number: ");
        num=scr.nextInt();
        System.out.print(x+" "+y);
        do
        {
        	z=x+y;
        	System.out.print(" "+z);
        	x=y;
        	y=z;
        }
        while(z<=(num-x));
        }//end of main
    }//end of class
  • 5. To input the marks of five students in three subjects and print their average marks.
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
        int num=1,m1,m2,m3;
        float avg=0;
        while(num<=5)
        {
        	System.out.println("Enter your marks in maths:");
        	m1=scr.nextInt();
         	System.out.println("Enter your marks in english:");
        	m2=scr.nextInt();
         	System.out.println("Enter your marks in hindi:");
        	m3=scr.nextInt();
    		avg=(m1+m2+m3)/3;
        	System.out.println("The average marks is: "+avg);
        	num++;
        }
        }//end of main
    }//end of class
  • 6. To accept the number from the user and reverse it:
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
        int num=0,rev=0;
        System.out.println("Enter any number: ");
        num=scr.nextInt();
        System.out.println("The reverse of the number is: ");
        while(num>=1)
        {
        	rev=num%10;
        	num=num/10;
        	System.out.print(rev);
        }
        }//end of main
    }//end of class
  • 7. To print the series mn where m and n are entered by the user:
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
        int i,m,n,res=1,count=1;
        System.out.println("Enter the value for m: ");
        m=scr.nextInt();
        System.out.println("Enter the value for n: ");
        n=scr.nextInt();
        while(count<=n)
        {
        	res=res*m;
        	count++;
        }
        System.out.println("the result is: +res);
        }//end of main
    }//end of class
  • 8. To print the factorial of the number entered:
    import java.util.Scanner;
    public class WhileLoop
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
        int num=0, fac=1;
        System.out.println("Enter any number: ");
        num=scr.nextInt();
        System.out.println("The factorial of the number is: ");
        while(num>=1)
        {
        	fac=fac*num;
        	System.out.print(num+"*");
        	num--;
        }
        System.out.println("\b="+fac);
        }//end of main
    }//end of class
  • Assignments of Character

  • 1. Write a program to accept a character from the user and display the character.
      //program to accept a character from the user
    import java.util.Scanner;
    public class AcceptCharacter
    {
        public static void main(String[] args)
        {
        Scanner scr=new Scanner(System.in);
            char ch;
            System.out.println("Enter any character: ");
            ch=scr.next().charAt(0);
            System.out.println("The character is "+ch);
        }//end of main
    }//end of class
  • 2. Write a program to accept a character in lower case and display it in uppercase.
     //Program to accept a character in lower case and display it in uppercase.
    import java.util.Scanner;
    public class AcceptCharacter
    {
        public static void main(String[] args)
        {
        	Scanner scr=new Scanner(System.in);
            char ch;
            int x;
            System.out.println("Enter any character: ");
            ch=scr.next().charAt(0);
            x=ch;
            x=x-32;
            ch=(char)x;
            System.out.println("The character is "+ch);
        }//end of main
    }//end of class 
  • 3. Write a program to accept a character from the user and display its ASCII value.
     //program to accept a character from the user and display its ASCII value.
    import java.util.Scanner;
    public class AcceptCharacter
    {
        public static void main(String[] args)
        {
        	Scanner scr=new Scanner(System.in);
        	char ch;
        	System.out.println("Enter any character: ");
            ch=scr.next().charAt(0);
        	System.out.println("The ASCII value of "+ch+" is "+(int)ch);
     }//end of main
    }//end of class
         
  • 4. Write a program to accept a character in uppercase and display it in lowercase.
       // program to accept a character in uppercase and display it in lowercase.
    import java.util.Scanner;
    public class AcceptCharacter
    {
        public static void main(String[] args)
        {
        	Scanner scr=new Scanner(System.in);
            char ch;
            int x;
            System.out.println("Enter any character: ");
            ch=scr.next().charAt(0);
            x=ch;
            x=x+32;
            ch=(char)x;
            System.out.println("The character is "+ch);
        }//end of main
    }//end of class  
  • 5. Write a program to display the ASCII values of 0 and 9.
      //program to display the ASCII values of 0 and 9.
    public class AcceptCharacter
    {
        public static void main(String[] args)
        {
        char ch1,ch2;
        ch1='0';
        ch2='9';
        System.out.println("The ASCII value of 0 is "+(int)ch1);
        System.out.println("The ASCII value of 9 is "+(int)ch2);
       }//end of main
    }//end of class 
         
  • 6. Write a program to display the ASCII values from 0 to 9.
      //program to display the ASCII values fron 0 to 9.
    public class AcceptCharacter
    {
        public static void main(String[] args)
        {
        	char ch;
        	ch='0';
       		int i;
        	for(i=0;i<=9;i++)
        	{
            	System.out.println("The ASCII of "+ch+" is "+(int)ch);
            	ch++;
        	}
        }//end of main
    }//end of class 
         
  • 7. Write a program to display the ASCII values of all characters from A to Z.
      /*program to display the ASCII values of all characters from A to Z.*/
    public class AcceptCharacter
    {
        public static void main(String[] args)
        {
        	char ch;
        	ch='A';
        	int i;
        	for(i=1;i<=26;i++)
        	{
            	System.out.println("The ASCII of "+ch+" is "+(int)ch);
            	ch++;
        	}
       }//end of main
    }//end of class
         
  • 8. Write a program to let the user press any key on the keyboard until x but count only how many upper case character keys are pressed on the keyboard?
      /* program to let the user press any key on the keyboard until x
     
    but count only how many upper case character keys are pressed on the keyboard?*/ import java.util.Scanner; public class AcceptCharacter { public static void main(String[] args) { Scanner scr=new Scanner(System.in); int keys=0; char ch; System.out.println("Press any key including upper case alphabets: "); while(true) { ch=scr.next().charAt(0); if((ch>=65)&&(ch<=90)) { keys++; } if((ch=='x')||(ch=='X')) { break; } }//end of while loop System.out.println("The uppercase character keys are pressed ("+keys+") Times"); }//end of main }//end of class
  • Assignments of Single Dimensional Array

  • 1. To accept an integer array of five elements from user and display it.
      import java.util.Scanner;
    
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[]=new int[5];
            int i;
            for(i=0;i<5;i++)
            {
                System.out.println("Enter array elements ");
                arr[i]= scr.nextInt();
            }
            for(i=0;i<arr.length;i++)
            {
                System.out.println("Array elements are: "+arr[i]);
            }
       }//end of main
    }//end of class
         
  • 2. To accept an integer array of five elements from user and display it in reverse order.
    import java.util.Scanner;
    
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[]=new int[5];
            int i,len;
            len=arr.length;
            for(i=0;i<len;i++)
            {
                System.out.println("Enter array elements ");
                arr[i]= scr.nextInt();
            }
            System.out.println("Array elements in Reverse order are: ");
            for(i=len-1;i>=0;i--)
            {
                System.out.print(" "+arr[i]);
            }
        }//end of main
    }//end of class
    
    
  • 3. To copy the elements of one array to second array.
    import java.util.Scanner;
    
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[]=new int[5];
            int arr1[]=new int[5];
            int i,len;
            len=arr.length;
            for(i=0;i<len;i++)
            {
                System.out.println("Enter array elements ");
                arr[i]= scr.nextInt();
            }
            System.out.println("Copied elements are : ");
            for(i=0;i<len;i++)
            {
                arr1[i]=arr[i];
                System.out.print(" "+arr1[i]);
            }
        }//end of main
    }//end of class
    
  • 4. To display the reverse copy of one array into second array.
    import java.util.Scanner;
    
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[]=new int[5];
            int arr1[]=new int[5];
            int i,len,j;
            len=arr.length;
            for(i=0;i<len;i++)
            {
                System.out.println("Enter array elements ");
                arr[i]= scr.nextInt();
            }
            System.out.println("Copied elements are : ");
            j=0;
            for(i=4;i>=0;i--)
            {
                arr1[j]=arr[i];
                System.out.println("Element is "+arr1[j]);
                j++;
            }
        }//end of main
    }//end of class
    
  • 5. To add the elements of array and display them.e.g.
    10+20+30+40+50=150
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[]=new int[5];
            int i,sum=0;
    
            for(i=0;i<arr.length;i++)
            {
                System.out.println("Enter array elements ");
                arr[i]= scr.nextInt();
            }
            
            System.out.println("Sum of Array elements is : ");
            for(i=0;i<arr.length;i++)
            {
                sum=sum+arr[i];
                System.out.print(arr[i]+"+");
            }
            System.out.print("\b="+sum);
        }//end of main
    }//end of class
         
  • 6. To accept two arrays and display their sum in third array.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr1[]=new int[5];
            int arr2[]=new int[5];
            int sum[]=new int[5];
            int i;
            System.out.println("Enter elements into first array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr1[i]= scr.nextInt();
            }
            System.out.println("Enter elements into second array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr2[i]= scr.nextInt();
            }
            System.out.println("Sum of Two Array elements is : ");
            for(i=0;i<arr1.length;i++)
            {
                sum[i] =arr1[i]+arr2[i];
                System.out.print(sum[i]+" ");
            }
        }//end of main
    }//end of class
    
         
  • 7. To accept two arrays and display their difference in third array.
        import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr1[]=new int[5];
            int arr2[]=new int[5];
            int sum[]=new int[5];
            int i;
            System.out.println("Enter elements into first array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr1[i]= scr.nextInt();
            }
            System.out.println("Enter elements into second array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr2[i]= scr.nextInt();
            }
            System.out.println("Sum of Two Array elements is : ");
            for(i=0;i<arr1.length;i++)
            {
                sum[i] =arr1[i]-arr2[i];
                System.out.print(sum[i]+" ");
            }
        }//end of main
    }//end of class
         
  • 8. To find the greatest element in the array.Also find the location.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr1[]=new int[5];
            int i,large=0,loc=0;
            System.out.println("Enter elements into first array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr1[i]= scr.nextInt();
            }
            System.out.println("Array elements are : ");
            for(i=0;i<arr1.length;i++)
            {
               System.out.print(arr1[i]+" ");
                if(arr1[i]>large)
                {
                    large=arr1[i];
                    loc=i+1;
                }
            }
            System.out.println("The greatest element is "+large+" at location "+loc);
        }//end of main
    }//end of class
         
  • 9. To find the smallest element in the array.Also find the location.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr1[]=new int[5];
            int i,small,loc=0;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr1[i]= scr.nextInt();
            }
            small=arr1[0];
            System.out.println("Array elements are : ");
            for(i=1;i<arr1.length;i++)
            {
               System.out.print(arr1[i]+" ");
                if(arr1[i]<small)
                {
                    small=arr1[i];
                    loc=i+1;
                }
            }
            System.out.println();
            System.out.println("The smallest element is "+small+" at location "+loc);
        }//end of main
    }//end of class
         
  • 10. To sort the array in ascending order.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr1[]=new int[5];
            int i,j,temp;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr1[i]= scr.nextInt();
            }
            for(i=0;i<=4;i++)
            {
                for(j=i+1;j<5;j++)
                {
                    if(arr1[i]>arr1[j])
                    {
                        temp=arr1[i];
                        arr1[i]=arr1[j];
                        arr1[j]=temp;
                    }
                }
            }
            System.out.println("Sorted Array Elements are(Ascending Order) : ");
            for(i=0;i<arr1.length;i++)
            {
                System.out.println(arr1[i]);
            }
        }//end of main
    }//end of class
         
  • 11. To sort the array in decending order.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr1[]=new int[5];
            int i,j,temp;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr1[i]= scr.nextInt();
            }
            for(i=0;i<=4;i++)
            {
                for(j=i+1;j<5;j++)
                {
                    if(arr1[i]<arr1[j])
                    {
                        temp=arr1[i];
                        arr1[i]=arr1[j];
                        arr1[j]=temp;
                    }
                }
            }
            System.out.println("Sorted Array Elements are(Descending Order) : ");
            for(i=0;i<arr1.length;i++)
            {
                System.out.println(arr1[i]);
            }
        }//end of main
    }//end of class
       
  • 12. To search the number in the array.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr1[]=new int[5];
            int i,n,flag=0;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<arr1.length;i++)
            {
                arr1[i]= scr.nextInt();
            }
            System.out.println("Array Elements are : ");
            for(i=0;i<arr1.length;i++)
            {
                System.out.println(arr1[i]);
            }
            System.out.println("Enter any number which you want to search: ");
            n= scr.nextInt();
            for(i=0;i<=4;i++)
            {
                if(arr1[i]==n)
                {
                    flag=1;
                    System.out.println("The number is "+arr1[i]+" found at "+(i+1)+" location");
                }
            }
            if(flag==0)
            {
                System.out.println("Number is not found");
            }
        }//end of main
    }//end of class
    
    
    
  • Assignments of Two Dimensional Array

  • 1. To accept 3X3 matrix and display it.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][3];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j = 0; j < col; j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
    
            System.out.println("Array Elements are : ");
            for(i=0;i<row;i++)
            {
                for (j = 0; j < col; j++)
                {
                    System.out.print(arr[i][j]+" ");
                }
                System.out.println();
            }
        }//end of main
    }//end of class
    
    
    
  • 2. To accept 2X2 matrix and then copy elements of one array into another array.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int arr1[][]=new int[2][2];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
    
            System.out.println("Copied Array Elements are : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr1[i][j]=arr[i][j];
                    System.out.print(arr1[i][j]+" ");
                }
                System.out.println();
            }
        }//end of main
    }//end of class
    
    
    
  • 3. To accept a matrix,store the reverse copy of one array into another array.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int arr1[][]=new int[2][2];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Reverse copy of Array Elements are : ");
            for(i=row-1;i>=0;i--) {
                for (j = col - 1; j >= 0; j--) {
                    arr1[i][j] = arr[i][j];
                    System.out.print(arr1[i][j] + " ");
                }
                System.out.println();
            }
          /* int x=1;
             int y=1;
            System.out.println("Reverse copy of Array Elements are : ");
            for(i=0;i<row;i++)
            {
                y++;
                for (j=0;j<col;j++)
                {
                    arr1[i][j]=arr[x][y];
                    System.out.print(arr1[i][j]+" ");
                    y--;
                }
                System.out.println();
    
                x=0;
            }*/
    
        }//end of main
    }//end of class
    
    
    
    
  • 4. To accept a matrix and copy elements of one array into another array.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int arr1[][]=new int[2][2];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Copy of Array Elements are : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr1[i][j] = arr[i][j];
                    System.out.print(arr1[i][j] + " ");
                }
                System.out.println();
            }
         }//end of main
    }//end of class
    
  • 5. To accept a matrix and display its elements in reverse order.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Reverse of Array Elements are : ");
            for(i=row-1;i>=0;i--) {
                for (j = col - 1; j >= 0; j--) {
                   
                    System.out.print(arr[i][j] + " ");
                }
                System.out.println();
            }
         }//end of main
    }//end of class
  • 6. To accept a matrix,add its elements and display the sum.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int i,j,row,col,sum=0;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Sum of Array Elements are : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                   sum = sum+arr[i][j];
                    System.out.print(arr[i][j]+"+");
                }
            }
            System.out.println("\b="+sum);
         }//end of main
    }//end of class
    
  • 7. To accept two matrices,add them and display the result in third matrix.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int arr1[][]=new int[2][2];
            int sum[][]=new int[2][2];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into first Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Enter elements into Second Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr1[i][j] = scr.nextInt();
                }
            }
            System.out.println("Sum of two matrices is : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                   sum[i][j] = arr[i][j]+arr1[i][j];
                    System.out.print(sum[i][j]+" ");
                }
                System.out.println();
            }
            
         }//end of main
    }//end of class
    
  • 8. To accept two matrices,subtract them and display the result in third matrix.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int arr1[][]=new int[2][2];
            int sub[][]=new int[2][2];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into first Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Enter elements into Second Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr1[i][j] = scr.nextInt();
                }
            }
            System.out.println("Diffrence between two matrices is : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                   sub[i][j] = arr[i][j]-arr1[i][j];
                    System.out.print(sub[i][j]+" ");
                }
                System.out.println();
            }
            
         }//end of main
    }//end of class
  • 9. To accept two matrices,divide them and display the result in third matrix.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int arr1[][]=new int[2][2];
            int div[][]=new int[2][2];
            int i,j,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into first Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Enter elements into Second Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr1[i][j] = scr.nextInt();
                }
            }
            System.out.println("Division of two matrices is : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                   div[i][j] = arr[i][j]/arr1[i][j];
                    System.out.print(div[i][j]+" ");
                }
                System.out.println();
            }
            
         }//end of main
    }//end of class
  • 10. To accept two matrices,multiply them and display the result in third matrix.
     import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[3][3];
            int arr1[][]=new int[3][3];
            int ans[][]=new int[3][3];
            int i,j,k,row,col;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into first Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Enter elements into Second Matrix: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr1[i][j] = scr.nextInt();
                }
            }
            System.out.println("Multiplication of two matrices is : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                   ans[i][j]=0;      
                   for(k=0;k<3;k++)      
                   {      
                        ans[i][j]+=arr[i][k]*arr1[k][j];      
                   }//end of k loop  
                   System.out.print(ans[i][j]+" "); 
                }
                System.out.println();
            }        
         }//end of main
    }//end of class
    
  • 11. To find the greatest element along with its location.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int i,j,row,col,large=0,rloc=0,cloc=0;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Array Elements are : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    System.out.print(arr[i][j] +" ");
                }
                System.out.println();
            }
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    if(arr[i][j]>large)
                    {
                        large=arr[i][j];
                        rloc=i+1;
                        cloc=j+1;
                    }
                }
            }
            System.out.println("The greatest element is "+large+" and present at ["+rloc+"] row and ["+cloc+"] column");
        }//end of main
    }//end of class
         
  • 12. To find the smallest element along with its location.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int i,j,row,col,small=0,rloc=0,cloc=0;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Array Elements are : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    System.out.print(arr[i][j] +" ");
                }
                System.out.println();
            }
            small=arr[0][0];
            for(i=0;i<2;i++)
            {
                for(j=0;j<2;j++)
                {
                    if(arr[i][j]<small)
                    {
                        small=arr[i][j];
                        rloc=i+1;
                        cloc=j+1;
                    }
                }
            }
            System.out.println("The smallest element is "+small+" and present at ["+rloc+"] row and ["+cloc+"] column");
        }//end of main
    }//end of class
    
  • 13. To transpose a matrix.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int i,j,row,col,small=0,rloc=0,cloc=0;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Matrix is : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    System.out.print(arr[i][j] +" ");
                }
                System.out.println();
            }
            System.out.println("Tranpose of Matrix is : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    System.out.print(arr[j][i] +" ");
                }
                System.out.println();
            }
        }//end of main
    }//end of class
    
  • 14. To search a number and display its location also.
      import java.util.Scanner;
    public class MyArray
    {
        public static void main(String[] args) {
            Scanner scr=new Scanner(System.in);
            int arr[][]=new int[2][2];
            int i,j,row,col,n,flag=0;
            row=arr.length;
            col=arr[0].length;
            System.out.println("Enter elements into an array: ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    arr[i][j] = scr.nextInt();
                }
            }
            System.out.println("Matrix is : ");
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    System.out.print(arr[i][j] +" ");
                }
                System.out.println();
            }
            System.out.println("Enter the number which you want to search : ");
            n=scr.nextInt();
            for(i=0;i<row;i++)
            {
                for (j=0;j<col;j++)
                {
                    if (arr[i][j] == n)
                    {
                        flag = 1;
                        System.out.println("Number is found at " + (i + 1) + " row and " + (j + 1) + " col");
                    }//end of if
                }  //end of j loop
            }//end of i loop
            if(flag==0)
            {
                System.out.println("Number is not found");
            }
        }//end of main
    }//end of class
    
  • Assignments of Function

  • 1. To calculate the sum of four numbers and display their sum.
    import java.util.Scanner;
    
    public class FunctionPractice
    {
        void sum()
        {
            int num1,num2,num3,num4,res;
            Scanner scr=new Scanner(System.in);
            System.out.println("Enter first number");
            num1=scr.nextInt();
            System.out.println("Enter second number");
            num2=scr.nextInt();
            System.out.println("Enter third number");
            num3=scr.nextInt();
            System.out.println("Enter fourth number");
            num4=scr.nextInt();
            res=num1+num2+num3+num4;
            System.out.println("Sum is "+res);
        }//end of sum
        public static void main(String[] args)
        {
            FunctionPractice fp=new FunctionPractice();
            fp.sum();
        }//end of main
    }//end of class
    
  • 2. To perform all the arithmetic operations.
    import java.util.Scanner;
    public class FunctionPractice
    {
        int num1,num2,res;
        static Scanner scr=new Scanner(System.in);
        void input()
        {
            System.out.println("Enter first number");
            num1=scr.nextInt();
            System.out.println("Enter second number");
            num2=scr.nextInt();
        } //end of input
        void sum()
        {
            res=num1+num2;
            System.out.println("Sum is "+res);
        }//end of sum
        void subtract()
        {
            if(num1>num2)
            {
                res = num1 - num2;
            }
            else
            {
                res=num2-num1;
            }
            System.out.println("Subtraction is "+res);
        }//end of subtract
        void multiply()
        {
            res=num1*num2;
            System.out.println("Multiplication is "+res);
        }//end of multiply
        void divide()
        {
            res=num1/num2;
            System.out.println("Division is "+res);
        }//end of divide
        
        public static void main(String[] args)
        {
            FunctionPractice fp=new FunctionPractice();
            char ch;
            System.out.println("Press + for addition");
            System.out.println("Press - for subtraction");
            System.out.println("Press * for multiplication");
            System.out.println("Press / for division");
            System.out.println("Enter your choice ");
            ch=scr.next().charAt(0);
            fp.input();
            switch (ch)
            {
                case '+':
                    fp.sum();
                break;
                case '-':
                    fp.subtract();
                break;
                case '*':
                    fp.multiply();
                break;
                case '/':
                    fp.divide();
                break;
                default:
                    System.out.println("Wrong input! Try again.");
            }
        }//end of main
    }//end of class
    
         
  • 3. To display the odd numbers from 1 to n, where n is passed as an argument to the an odd() function.
    import java.util.Scanner;
    public class FunctionPractice
    {
        static void odd(int n1)
        {
            int i;
            System.out.println("The Odd Series is: ");
            for(i=1;i<=n1;i+=2)
            {
                System.out.print(i+" ");
            }
        }
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int n;
            System.out.println("Enter the nth number ");
            n=scr.nextInt();
            //no need to create the object for static function
            odd(n);
        }//end of main
    }//end of class
         
  • 4. To display the even numbers from 0 to n, where n is passed as an argument to the an even() function.
    import java.util.Scanner;
    public class FunctionPractice
    {
        static void even(int n1)
        {
            int i;
            System.out.println("The Even Series is: ");
            for(i=0;i<=n1;i+=2)
            {
                System.out.print(i+" ");
            }
        }
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            int n;
            System.out.println("Enter the nth number ");
            n=scr.nextInt();
            //no need to create the object for static function
            even(n);
        }//end of main
    }//end of class
         
  • 5. Enter a character: $
    $$$$$$$$$$$$$$$$$$$$$
    import java.util.Scanner;
    public class FunctionPractice
    {
        void display(char ch1,int n1)
        {
            int i;
            System.out.println("Series is: ");
            for(i=1;i<=n1;i++)
            {
                System.out.print(ch1);
            }//end of loop
        }//end of display function
        
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            FunctionPractice fp=new FunctionPractice();
            char ch;
            int n;
            System.out.println("Enter the character");
            ch=scr.next().charAt(0);
            System.out.println("Enter the nth number ");
            n=scr.nextInt();
            fp.display(ch,n);
        }//end of main
    }//end of class
         
  • 6.Program to accept two arguments,one is starting value and other is ending value and prints all the number between starting and ending value.
    import java.util.Scanner;
    public class FunctionPractice
    {
        void series(int n1,int n2)
        {
            int i;
            System.out.println("The Series is: ");
            for(i=n1;i<=n2;i++)
            {
                System.out.print(i+" ");
            }//end of loop
        }//end of display function
        
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            FunctionPractice fp=new FunctionPractice();
            int start,stop;
            System.out.println("Enter the starting number of the series");
            start=scr.nextInt();
            System.out.println("Enter the ending number of the series");
            stop=scr.nextInt();
            fp.series(start,stop);
        }//end of main
    }//end of class
         
  • 7.To find whether entered number is positive or negative.
    import java.util.Scanner;
    public class FunctionPractice
    {
        void pos_neg(int n)
        {
            if(n>0)
            {
                System.out.println("Number is positive");
            }//end of if
            else if(n<0)
            {
                System.out.println("Number is negative");
            }
            else
            {
                System.out.println("Number is zero");
            }
        }//end of display function
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            FunctionPractice fp=new FunctionPractice();
            int num;
            System.out.println("Enter the number : ");
            num=scr.nextInt();
            fp.pos_neg(num);
        }//end of main
    }//end of class
    
         
  • 8.To generate the fibonacci series till 'n' numbers, where n is passed as argument to function.
    import java.util.Scanner;
    public class FunctionPractice
    {
        void fibonacci(int n)
        {
            int i,j=0,k=1,m;
            System.out.print(j+" "+k);
            for(i=1;i<=n;i++)
            {
                m=j+k;
                j=k;
                k=m;
                if(m<=n)
                    System.out.print(" "+m);
            }//end of loop
        }//end of function
    
        public static void main(String[] args)
        {
            Scanner scr=new Scanner(System.in);
            FunctionPractice fp=new FunctionPractice();
            int num;
            System.out.println("Enter any number for ending value: ");
            num=scr.nextInt();
            fp.fibonacci(num);
        }//end of main
    }//end of class
         
  • 9.To generate the following series,where 'n' is passed as argument to the function.
    Enter a number 5
    Series:1+4+9+16+25=55
     import java.util.Scanner;
    public class FunctionPractice
    {
        void seriesSum(int n) {
            int j = 1;
            int sum = 0;
            System.out.print("SERIES =  ");
            for (int i = 1; i <= n; i++) {
                j = i * i;
                sum = sum + j;
    
                System.out.print(j);
                if(i<n)
                {
                    System.out.print(" + ");
                }
            }
            System.out.print(" = " + sum);
        }
    
        public static void main(String[] args) {
    
            int num;
            System.out.print("Enter the number whos series sum you want  ");
            Scanner sc = new Scanner(System.in);
            num = sc.nextInt();
            FunctionPractice fp=new FunctionPractice();
            fp.seriesSum(num);
        }//end of main
    }//end of class
    
         
  • 10.To calculate the factorial of the number given as argument and returns the value of the factorial.
    import java.util.Scanner;
    public class FunctionPractice
    {
        int factorialFunction(int num) {
            int fact = 1;
            for (int i = num; i >= 1; i--) {
                fact = fact * i;
            }
            return fact;
        }
    
        public static void main(String[] args) {
            System.out.println("Enter the number whose factorial you want :");
            Scanner sc = new Scanner(System.in);
            int number;
            number = sc.nextInt();
            FunctionPractice fpf = new FunctionPractice();
            int result = fpf.factorialFunction(number);
            System.out.println("The factorial is : " + result);
        }//end of main
    }//end of class
         
  • 11.To accept two numbers as arguments and swap these two numbers.
     import java.util.Scanner;
    public class FunctionPractice
    {
        void swappingNumber(int num1, int num2)
        {
            int temp;
            temp = num1;
            num1 = num2;
            num2 = temp;
            System.out.println("The numbers after swapping are : " + num1 +"  "+ num2);
        }
    
        public static void main(String[] args)
         {
            System.out.println("Enter two  numbers ");
            Scanner sc = new Scanner(System.in);
            int number1, number2;
            number1 = sc.nextInt();
            number2 = sc.nextInt();
            System.out.println("The numbers before swapping are : " + number1 +"  "+ number2);
            FunctionPractice fp = new FunctionPractice();
            fp.swappingNumber(number1, number2);
        }//end of main
    }//end of class
         
  • 12.To calculate the factorial of a number through recursion.
    import java.util.Scanner;
    public class FunctionPractice
    {
        int factorialFunction(int num) {
            int fact = 1;
            if (num == 0 || num == 1)
                return 1;
            else
                fact = num * factorialFunction(num - 1);
            return fact;
        }
    
        public static void main(String[] args) {
            System.out.println("Enter the number whose factorial you want :");
            Scanner sc = new Scanner(System.in);
            int number;
            number = sc.nextInt();
            FunctionPractice fp = new FunctionPractice();
            int result = fp.factorialFunction(number);
            System.out.println("The factorial is : " + result);
        }//end of main
    }//end of class
    
         
  • Assignments of Classes and Objects

  • 1. Define a class 'Math' with these specification:
    Data Members:
    private: int num1, num2, num3;
    Member Function:
    public: void input(), void sum()
    Add private data member of the class 'Math' and display the output.
     import java.util.Scanner;
    class Maths
    {
       private int num1,num2,res;
        Scanner scr=new Scanner(System.in);
       public void input()
        {
            System.out.println("Enter first number: ");
            num1=scr.nextInt();
            System.out.println("Enter second number: ");
            num2=scr.nextInt();
        }
       public void sum()
        {
            res=num1+num2;
            System.out.println("Sum is "+res);
        }
    };//end of class
    public class OopPracticeAssignments
    {
        public static void main(String[] args)
        {
            Maths mt=new Maths();
            mt.input();
            mt.sum();
        }//end of main
    }//end of class
         
  • 2. Define a class 'Student' and calculate percentage of two students. (Hint: using array of objects). private: int rollno; float marks[],per; public: get_data(),percent();
    import java.util.Scanner;
    class Student
    {
       private int rollno;
       private float marks[]=new float[2],per,sum;
        Scanner scr=new Scanner(System.in);
       public void get_data()
        {
            int i;
            sum=0;
            System.out.println("Enter rollno ");
            rollno=scr.nextInt();
            for(i=0;i<2;i++)
            {
                System.out.println("Enter marks ");
                marks[i]=scr.nextInt();
                sum=sum+marks[i];
            }
        }
       public void percent()
        {
            per=sum/2;
            System.out.println("Percentage is "+per);
        }
    };//end of class
    public class OopPracticeAssignments
    {
        public static void main(String[] args)
        {
            Student st[]=new Student[2];
            int i;
            st[0]=new Student();
            st[1]=new Student();
            for(i=0;i<2;i++)
            {
                st[i].get_data();
                System.out.println("Percentage of student "+(i+1));
                st[i].percent();
                System.out.println("----------------------------------\n");
            }
        }//end of main
    }//end of class
    
         
  • 3. Define a class 'Demo' as per following description private: int num; public: void get_data(), void sum() (sum is a function which will add data member of two objects taking other object as an argument)
    import java.util.Scanner;
    class Demo2
    {
    private int num;
        Scanner scr=new Scanner(System.in);
       public void get_data()
        {
            System.out.println("Enter number ");
            num=scr.nextInt();
        }
       public void sum(Demo2 dm)
        {
            int res;
            res=num+dm.num;
            System.out.println("Sum is "+res);
        }
    };//end of class
    public class OopPracticeAssignments
    {
        public static void main(String[] args)
        {
            Demo2 d1=new Demo2();
            Demo2 d2=new Demo2();
            d1.get_data();
            d2.get_data();
            d1.sum(d2);
        }//end of main
    }//end of class
         
  • 4. Write a program to swap data members of two objects by passing object by reference to a member function of a class.
    import java.util.Scanner;
    class Demo2
    {
       int num;
        Scanner scr=new Scanner(System.in);
        void get_data()
        {
            System.out.println("Enter number ");
            num=scr.nextInt();
        }
        void sum(Demo2 dm)
        {
            int res;
            res=num+dm.num;
            System.out.println("Sum is "+res);
        }
    };//end of class
    public class OopPracticeAssignments
    {
        public static void main(String[] args)
        {
            Demo2 d1=new Demo2();
            Demo2 d2=new Demo2();
            d1.get_data();
            d2.get_data();
            d1.sum(d2);
        }//end of main
    }//end of class
         
  • 5.Write a program to find the greatest of three numbers using max() function .
    import java.util.Scanner;
    class Demo
    {
        Scanner scr=new Scanner(System.in);
        private int num1,num2,num3;
      public  void max()
        {
            System.out.println("Enter first number ");
            num1=scr.nextInt();
            System.out.println("Enter second number ");
            num2=scr.nextInt();
            System.out.println("Enter third number ");
            num3=scr.nextInt();
            if(num1>num2 && num1>num3)
            {
                System.out.println("num1 is greater ");
            }
            else if(num2>num1 && num2>num3)
            {
                System.out.println("num2 is greater ");
            }
            else
            {
                System.out.println("num3 is greater ");
            }
        }
    }//end of class
    public class OopPracticeAssignments
    {
        public static void main(String[] args)
        {
            Demo dm=new Demo();
            dm.max();
        }//end of main
    }//end of class
         
  • Assignments of Constructor

  • 1. WAP to initialize the private data member of a class 'Rectangle' with given values and display the values of length, breadth; L=10; B=20;
     class MyRectangle {
        int length;
        int breath;
        int areaOfRectangle;
        public MyRectangle() {
            length = 10;
            breath = 20;
        }
        public void area() {
            System.out.println("The length is : " + length);
            System.out.println("The breath is : " + breath);
            areaOfRectangle = length * breath;
            System.out.println("The area is : " + areaOfRectangle);
        }
    }//end of class
    public class OopPracticeAssignments
    {
        public static void main(String[] args)
        {
            MyRectangle myRect = new MyRectangle();
            myRect.area();
        }//end of main
    }//end of class
         
  • 2. WAP to define a class 'Rectangle' and calculate the area of different objects of 'Rectangle' class.
    Length and breadth should be initialized by parameterized constructor.
     class MyRectangle
     {
        int length;
        int breath;
        int areaOfRectangle;
    
        public MyRectangle(int l, int b)
         {
            length = l;
            breath = b;
        }
    
        public void area()
        {
            System.out.println("The length is : " + length);
            System.out.println("The breath is : " + breath);
            areaOfRectangle = length * breath;
            System.out.println("The area is : " + areaOfRectangle);
        }//end of function
    }//end of class
    
    public class MyJavaProgram 
    {
        public static void main(String[] args)
         {
            MyRectangle myRect = new MyRectangle(10, 20);
            myRect.area();
            MyRectangle myRect2 = new MyRectangle(56, 19);
            myRect2.area();
        }//end of function
    }//end of class 
  • 3. WAP to define a class 'Rectangle' having default constructor as well as parameterized constructor in it.
      class MyRectangle
      {
        int length;
        int breath;
    
        public MyRectangle()
         {
            length = 10;
            breath = 20;
            System.out.println("The length using default constructor is : " + length);
            System.out.println("The breath usin default constructor is : " + breath);
        }
    
        public MyRectangle(int l, int b)
         {
            length = l;
            breath = b;
            System.out.println("The length using parametrised constructor is : " + length);
            System.out.println("The breath using parametrised constructor is : " + breath);
       }//end of function
    }//end of class
    
    public class MyJavaProgram
     {
        public static void main(String[] args)
         {
            MyRectangle myRect = new MyRectangle();
            MyRectangle myRect2 = new MyRectangle(56, 19);
    	}//end of function
    }//end of class
         
  • 4. WAP to define a class 'Rectangle' and calculate the area of different objects of 'Rectangle' class
    in which 2nd object copies the value of length and breadth of 1st object.
       class MyRectangle
       {
        int length;
        int breath;
        int areaOfRectangle;
    
        public MyRectangle()
        {
            length = 23;
            breath = 56;
        }
    
        public void copyValue(MyRectangle my)
        {
            length = my.length;
            breath = my.breath;
        }
    
        public void area()
         {
            System.out.println("The length is : " + length);
            System.out.println("The breath is : " + breath);
            areaOfRectangle = length * breath;
            System.out.println("The area is : " + areaOfRectangle);
        }//end of function
    }//end of class
    
    public class MyJavaProgram
    {
        public static void main(String[] args)
        {
            MyRectangle myRect = new MyRectangle();
            myRect.area();
            MyRectangle myRect2 = new MyRectangle();
            myRect.copyValue(myRect2);
            myRect2.area();
        }//end of main
    }//end of class
         
  • Assignments of Function Overloading

  • 1. WAP to add two integer, two float through a user defined function sum() numbers are passed as arguments.
      class Maths
      {
        int num1;
        int num2;
        float num3;
        float num4;
    
        /*
          public void sum(int n1, int n2)
          {
          	num1 = n1;
           	num2 = n2;
          	int sumOfInt = num1 + num2;
          	System.out.println("The sum of  integers values is : " + sumOfInt);
          }
         */
        public void sum(float n3, float n4)
         {
            num3 = n3;
            num4 = n4;
            float sumOfFloat = num3 + num4;
            System.out.println("The sum of integers values is : " + sumOfFloat);
        }//end of sum
      }//end of class
    
     public class MyFunctionOverloading
     {
        public static void main(String[] args) 
        {
            Maths mt = new Maths();
            mt.sum(10, 20);
            mt.sum(34.5f, 78.6f);
        }//end of main
      }//end of class
        
  • 2. WAP to find the area of rectangle, square through a function area(), using the concept of function overloading.
    class Shape
     {
        int length;
        int breath;
    	int side;
        public void area(int l, int b)
         {
            length = l;
            breath = b;
            int areaOfRect = length * breath;
            System.out.println("The area of the reactangle is : " + areaOfRect);
        }
    
        public void area(int s)
         {
            side = s;
            int areaOfSquare = side*side;
            System.out.println("The area of the reactangle is : " + areaOfSquare);
        }//end of function
    }//end of class
    
    public class MyFunctionOverloading
    {
        public static void main(String[] args) {
        Shape s1 = new Shape();
            s1.area(10, 20);
            s1.area(5);
        }//end of main
    }//ecd of class
         
  • Assignments of Static Data Member and Static Member Function

  • 1. WAP to display number of objects created of a class.
      class StaticDemo
      {
        static int n = 0;
        public StaticDemo()
        {
            n++;
            System.out.println("Object " + n + " created");
        }//end of function
    }//end of class
    
    public class staticFunction
    {
        public static void main(String[] args)
        {
            StaticDemo sd1 = new StaticDemo();
            StaticDemo sd2 = new StaticDemo();
            StaticDemo sd3 = new StaticDemo();
        }//end of main
    }//end of class
    
  • 2. WAP to define a class 'Rectangle' and calculate the area of rectangle and count that how many rectangles are created?
      class Rectangle
      {
        int l;
        int b;
        static int n = 0;
        public Rectangle()
        {
            n++;
            System.out.println("Rectangle " + n + " is created");
        }
    
        public void area(int length, int breath)
        {
            l = length;
            b = breath;
            int area;
            area = l * b;
            System.out.println("The area of rectangle is :" + area);
        }//end of function
    }//end of class
    
    public class staticFunction
    {
        public static void main(String[] args)
        {
            Rectangle rect = new Rectangle();
            Rectangle rect1 = new Rectangle();
            Rectangle rect2 = new Rectangle();
            rect.area(10, 40);
            rect1.area(89, 56);
            rect2.area(13, 59);
        }//end of main
    }//end of clas
    
  • 3. WAP to declare a static member function void init() in class 'Rectangle' to initialize the static data member count and calculate the area of rectangles.
      class MyRectangle
      {
       	static int length;
       	static int breath;
       	int areaOfRectangle;
    
        public static void init()
        {
            length=10;
            breath=20;
        }
        public void area()
        {
            System.out.println("The length is : " + length);
            System.out.println("The breath is : " + breath);
            areaOfRectangle = length * breath;
            System.out.println("The area is : " + areaOfRectangle);
        }//end of function
    }//end of class
    
    public class MyJavaProgram
    {
        public static void main(String[] args)
        {
            MyRectangle myRect = new MyRectangle();
            MyRectangle.init();
            myRect.area();
        }//end of main
    }//end of class