Search This Blog

Thursday 21 July 2016

Write an interactive program to print a diamond shape.

import java.util.Scanner;

class HelloWorld
{

    public static void main(String[] args)
 {
 
        int c=0,v=0;
int s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter Number :");
        s = in.nextInt();
     
        for(int i = 1 ;i<=s;i++)
        {
            for(int j=s;j>i;j--)
System.out.print(" ");
for(int k=0;k<i;k++)
System.out.print("* ");
System.out.println();
        }
        for(int i = s-1 ;i>0;i--)
        {
            for(int j=s;j>i;j--)
System.out.print(" ");
for(int k=0;k<i;k++)
System.out.print("* ");
System.out.println();
        }
    }
}


output:
Enter Number:
3
       *
   *      *
 *    *    *
   *      *
       *

Write an interactive program to print a string entrered in a pyramid form.

import java.util.Scanner;

class  Pyramid
{

    public static void main(String[] args)
{
 
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
     
        for(int i = 0 ;i<s.length();i++)
        {
            for(int j=s.length()/2 + 1;j>=i;j--)
System.out.print(" ");
for(int k=0;k<=i;k++)
{
char ch = s.charAt(k);
System.out.print(ch+" ");
}
System.out.println();
        }
    }
}

output:
Enter String:
SCET
        S
     S   C
  S    C    E
S   C   E   T

Create a class which ask the user to enter a sentence, and it should display count of each vowel type in the sentence. The program should continue till user enters a word “quit”. Display the total count of each vowel for all sentences.

import java.util.Scanner;

public class Vowel
 {

    public static void main(String[] args)
    {
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        s = s.toLowerCase();
        for(int i = 0 ;i<s.length();i++)
        {
            char ch = s.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' ||
                                        ch == 'o' || ch == 'u'){
                                v++;
            }
            else if(ch == ' '){
               
            }
            else if (Character.isDigit(s.charAt(i)))
            {
             
            }
         
            else
                c++;
        }
        System.out.println("Vowels are "+v+" and consonants are "+c);
    }
}


output:
Enter string:
Hello World
Vowels are 3 and consonants are 7.

Wednesday 6 July 2016

Write a program to find that given number or string is palindrome or not.

Unit 2

Prac 5


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        String s,s2="";
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        for(int i = s.length()-1;i>=0;i--)
        {
            s2 = s2 + s.charAt(i);
        }
        if(s.equals(s2))
            System.out.println("palindrome");
        else
            System.out.println("not palindrome");
    }
}


output:

Enter String :
gajjar
not palindrome

Enter String :
12321
palindrome

Write a program to count the number of words that start with capital letters.

Unit - 2

Practical 4


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        int ct=0;
        for(String str: s.split(" ")) {
        if(str.charAt(0)>=65 && str.charAt(0)<=90)
        {
            ct++;
        }
    }
    System.out.println("total  number of words start with capital letters are :"+ct);
    }
}


output:

Enter String :
Karan Gajjar kanu
total  number of words start with capital letters are :2

Write a program to accept a line and check how many consonants and vowels are there in line.

Unit - 2

Practical 3


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        int c=0,v=0;
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        s = s.toLowerCase();
     
        for(int i = 0 ;i<s.length();i++)
        {
            char ch = s.charAt(i);
            if (ch == 'a' || ch == 'e' || ch == 'i' ||
                                        ch == 'o' || ch == 'u'){
                                v++;
            }
            else if(ch == ' '){
               
            }
            else if (Character.isDigit(s.charAt(i)))
            {
             
            }
         
            else
                c++;
        }
        System.out.println("Vowels are "+v+" and consonants are "+c);
    }
}


output:

Enter String :
karan gajjar 123
Vowels are 4 and consonants are 7

Write a program to find length of string and print second half of the string.

Unit - 2

Practical - 2


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        String s;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter String :");
        s = in.nextLine();
        System.out.println("Length of ths String is :"+s.length());
        System.out.print("second half of the sring is:");
        System.out.println(s.substring(s.length() / 2));
   }
}


output:

Enter String :
gajjar
Length of ths String is :6
second half of the sring is:jar

Write a program to enter two numbers and perform mathematical operations on them

Unit - 1

Practical 2

package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        float m,n;
        Scanner in =new Scanner(System.in);
        System.out.println("Enter two numbers :");
        m = in.nextFloat();
        n= in.nextFloat();
        while(true)
        {
            System.out.println("Enter your choice:\n1.sum\t2.sub\t3.mul\t4.div\t5.exit : ");
            byte choice= in.nextByte();
            switch(choice)
            {
                case 1:System.out.println("Addition is :"+(m+n));
                break;
             
                case 2:System.out.println("Subtraction is :"+(m-n));
                break;
             
                case 3:System.out.println("mul is :"+(m*n));
                break;
             
                case 4:System.out.println("div is "+(m/n));
                break;
             
                case 5:System.exit(0);
                break;
             
                default:System.out.println("Enter proper choice");
                break;
             
            }
        }
   }
}


output:

Enter two numbers :
2 2
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
1
Addition is :4.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
2
Subtraction is :0.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
3
mul is :4.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
4
div is 1.0
Enter ypur choice:
1.sum 2.sub 3.mul 4.div 5.exit :
5

Write a program that calculate percentage marks of the student if marks of 6 subjects are given.

Unit 1

Practical - 1


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
 
        float sum=0;
        Scanner in =new Scanner(System.in);
        float a[]= new float[6];
        System.out.println("Enter marks of total six subjects :");
        for(int i=0;i<6;i++)
        {
            a[i]=in.nextFloat();
            sum = sum+a[i];
        }
        System.out.println("Percentage is :"+sum/6);
    }
}


output:

Enter marks of total six subjects :

1 2 3 4 5 6
Percentage is :3.5

Write a program to convert rupees to dollar. 60 rupees=1 dollar

Practical - 1


package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
   
        double i;
        Scanner in =new Scanner(System.in);
       
        System.out.println("Enter Rs. to convert into $ :");
        i = in.nextInt();
        System.out.println("INR = "+i+"\nUSD = "+i/67.50);
   }
}

output:

Enter Rs. to convert into $ :
10000
INR = 10000.0
USD = 148.14814814814815

Matrix Multiplication - java

package pl;
import java.util.Scanner;

public class Pl {

    public static void main(String[] args) {
    int m, n, p, q, sum = 0, i, j, k;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of rows and columns of 1st matrix");
      m = in.nextInt();
      n = in.nextInt();
      int a[][] = new int[m][n];

      System.out.println("Enter the data of 1st matrix:");

      for ( i = 0 ; i < m ; i++ )
         for ( j = 0 ; j < n ; j++ )
            a[i][j] = in.nextInt();

      System.out.println("Enter the number of rows and columns of 2nd matrix");
      p = in.nextInt();
      q = in.nextInt();

      if ( n != p )
         System.out.println("Matrices can't be multiplied !.");
      else
      {
         int b[][] = new int[p][q];
         int c[][] = new int[m][q];

         System.out.println("Enter data of 2nd matrix");

         for ( i = 0 ; i < p ; i++ )
            for ( j = 0 ; j < q ; j++ )
               b[i][j] = in.nextInt();

         for ( i = 0 ; i < m ; i++ )
         {
            for ( j = 0 ; j < q ; j++ )
            {  
               for ( k = 0 ; k < p ; k++ )
               {
                  sum = sum + a[i][k]*b[k][j];
               }

               c[i][j] = sum;
               sum = 0;
            }
         }

         System.out.println("Multiplication is :");

         for ( i = 0 ; i < m ; i++ )
         {
            for ( j = 0 ; j < q ; j++ )
               System.out.print(c[i][j]+" ");

            System.out.print("\n");
         }
      }
   }
 
}


output:

Enter the number of rows and columns of 1st matrix
3
3
Enter the data of 1st matrix:
1
2
3
4
5
6
7
8
9
Enter the number of rows and columns of 2nd matrix
3
3
Enter data of 2nd matrix
9
8
7
6
5
4
3
2
1
Multiplication is :
30 24 18
84 69 54
138 114 90

Sum of two 2d array java-scanner


package pl;
import java.util.Scanner;
/**
 *
 * @author Kanu
 */
public class Pl {

    public static void main(String[] args) {
     int m,n;
           
            System.out.print("Enter rows and columns : ");
            Scanner in = new Scanner(System.in);
            m = in.nextInt();  
            n = in.nextInt();
            System.out.println("Enter data for array 1:");
            int [][]a=new int[m][n];          
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++){
                System.out.print("Enter Num :");
                a[i][j]=in.nextInt();
                }
            }
            System.out.println("Enter data for array 2:");
            int [][]b=new int[m][n];          
            for(int i=0;i<m;i++)
            {
                for(int j=0;j<n;j++){
                System.out.print("Enter Num :");
                b[i][j]=in.nextInt();
                }
            }
            int [][]c=new int[m][n];          
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                    c[i][j]=a[i][j]+b[i][j];
                }
            }
            System.out.println("Array 1 is :");
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                System.out.print(a[i][j]+"");
                }System.out.println();
               
            }
            System.out.println("Array 2 is :");
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                System.out.print(b[i][j]+"");
                }System.out.println();
            }
            System.out.println("sum of two array is :");
            for(int i =0;i<m;i++)
            {
                for(int j =0;j<n;j++){
                System.out.print(c[i][j]+"");
                }System.out.println();
               
            }
    }
}

output:

Enter rows and columns : 2
2
Enter data for array 1:
Enter Num :1
Enter Num :2
Enter Num :3
Enter Num :4
Enter data for array 1:
Enter Num :1
Enter Num :2
Enter Num :3
Enter Num :4
Array 1 is :
12
34
Array 2 is :
12
34
sum of two array is :
24
68

Sunday 3 July 2016

Array of Sum - java with scanner and bufferedreader

Scanner class 

import java.util.Scanner;

public class Pl {

    public static void main(String[] args)  {
        int n,sum=0,num;    
        System.out.print("Enter length : ");
            Scanner in = new Scanner(System.in);
            n = in.nextInt();    
            int []a=new int[n];            
            for(int i=0;i<n;i++)
            {
               System.out.print("Enter Num :");
               num = in.nextInt();
               a[i]=num;
               sum = sum+a[i];
            }
            for(int i =0;i<n;i++)
            {
                System.out.print(a[i]+" ");
            }
          System.out.print("Sum is "+sum);
 
    }
 
}

output:
Enter length : 3
Enter num : 1
Enter num : 2
Enter num :3

1 2 3
sum is : 6

BufferedReader

package pl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Pl {

    public static void main(String[] args) throws IOException  {
        int sum=0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter length");
        int n = Integer.parseInt(br.readLine());
        int []a=new int[n];              
        for(int i =0 ;i <n;i++)
        {
            System.out.print("Enter num:");
            int k = Integer.parseInt(br.readLine());
            a[i]=k;
            sum= sum + a[i];
        }
        for(int i =0;i<n;i++)
        {
            System.out.print(a[i]+" ");
        }
        System.out.print("Sum i :"+sum);
       

    }
   

}

5th sem syllabus and Materials

Syllabus  -  click here


E - Books 


algorithm - click here

java the complete reference - click here


Practical List


java  - click here

algorith - click here