Header Ads Widget

GTU-Sem-5_JAVA 3350703 Lab manual Solution

 


_JAVA

3350703
 Lab manual Solution


1: Write a Program in Java to generate first n prime numbers. 

Answer:-


public class prime
{
    public static void main(String args[])
    {
        int x,y,c=0,n=10;
        for(x=2;x<n;x++)
        {
            if(x%2 !=0 || x==2)
            {
                for(y=2;y<=x/2;y++)
                {
                    if(x%y==0)
                    {
                        break;
                    }
                }
                if(y>x/2)
                {
                    System.out.println(x);
                    c++;
                }
            }
        }
        System.out.println("\n Total" + c); 
    }
}


OUTPUT:-

2
3
5
7

Total: 4


2: Write a Program in Java to generate first n prime numbers. 

Answer:-


OUTPUT:-

Enter three integers 
2
3
4

Maximum of three is: 4

3: Write a Program in Java to find second maximum of n numbers without using arrays. 

Answer:-






OUTPUT:-

5 No.s: 
n1=90000
n2=18000
n3=20000
n4=16000
n5=30000
Largest=90000
2nd Largest= 30000

4: Write a Program in Java to reverse the digits of a number using while loop. 

Answer:-


OUTPUT:-

Please enter number to be reversed using Java program: 
123
Reverse of number: 423 is 324


5: Write a Program in Java to convert number into words & print it. 

Answer:-


OUTPUT:-

Output -1:
Enter the Value: 12515456
one crore twenty five lack fifteen thousand four hundred fifty six

Output -2:
Enter the Value: 54321
fifty four thousand three hundred twenty one

Output -3:
Enter the Value: 123
one hundred twenty three


6: Write a Program in Java to use Wrapper class of each primitive data types . 

Answer:-



OUTPUT:-

Value of Wrapper objects (printing as objects )

Byte object g1: 2
Integer object m1: 50
Float object f1: 8.6f
Double object r1: 50.5

Unwrapped values (printing as data types)
byte value, bv: 2
int value, iv: 50
float value, fv: 8.6f
double value, dv: 50.5





OUTPUT:-

100  100

7: Write a Program in Java to multiply two matrix. 

Answer:-



OUTPUT:-

Enter the number of rows and columns of first matrix
2 2
Enter the elements of first matrix
1
2
1
2

Enter the number of rows and columns of Second matrix
2 2
Enter the elements of second matrix
2
2
2
2

Product of entered matrices:-
 6 6
 6 6


8: Write a Static block which will be executed before main() method in a class. 

Answer:-


OUTPUT:-

Print inside static block
First object
Print Print inside printmsg() method
Second object
Print Print inside printmsg() method



9: Write a program in Java to demonstrate use of this keyword. Check whether this can access the private members of the class or not.

Answer:-


OUTPUT:-

x= 4
y= 3


Post a Comment

1 Comments