Professional Documents
Culture Documents
}
}
Output:
Assignment 2
Q.2) Write a program to find a factorial of a number.
import java.util.*;
Output:
Assignment 3
Q.3) Write a program to find the area of a triangle.
import java.util.Scanner;
Output:
ASSIGNMENT-4
Q.4) Write a program to find the sum of three numbers.
import java.util.Scanner;
int a,b,c,d;
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
d=a+b+c;
Output:
Assignment 5
Q.5) Write a program to create an object in java.
import java.util.*;
void show()
{
System.out.println("Welcome to JAVA LAB STUDENT");
}
public static void main(String[] args) {
// creating an object using new keyword
prog3 obj = new prog3();
//invoking method using the object
obj.show();
}
}
Output:
Assignment 6
Q.6) Write a program to how to use constructor in java.
public class Student {
int id;
String name;
//creating a parameterized constructor.
Student(int i,String n){
id=i;
name=n;
}
// method to display the values
void display()
{
System.out.println(id+" "+name);
}
public static void main(String[] args) {
// creating objects and passing values
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}
Output:
Assignment 7
Q.7) Write a program to print the right angle triangle star pattern in java.
import java.util.*;
Output:
Assignment 8
Q.8) Write a program to count the total number of characters in a string.
import java.util.*;
Output:
Assignment 9
Q.9) Write a program to add two matrices in java.
public class matrix{
public static void main(String args[]){
//creating two matrices
int a[][]={{5,23,7},{23,44,43},{33,74,75}};
int b[][]={{6,64,7},{6,44,63},{31,42,54}};
Assignment 10
Q.10) Write a program to calculate the percentage of a given student in the RTU exam. His
marks from 5 subjects must be taken as input from the keyboard, marks out of 80.
import java.util.*;
public class ass10{
public static void main(String args[])
{
float a,b,c,d,e,per;
Scanner sc = new Scanner(System.in);
per = ((a+b+c+d+e)/400)*100;
}
}
Output:
ASSIGNMENT-11
Q.11) Write a program to ask a user to enter his/her name and gets output with hello “name”,
have a good day.
import java.util.*;
String name;
name = sc.nextLine();
}
Output:
Assignment 12
Q.12) Write a program to convert a string to lowercase.
import java.util.*;
System.out.println(text.toLowerCase());
}
Output:
Assignment 13
Q.13) Write a program to replace space with underscore.
import java.util.*;
System.out.println(text1);
}
}
Output:
Assignment 14
Q.14) Write a program to find a year entered by the user is leap year or not.
import java.util.*;
Output:
ASSIGNMENT-15
Q.15) Write a program to find out the type of website from the URL: .com, .in, .org
import java.util.*;
String site;
site = sc.nextLine();
if(val)
else
System.out.println("String not found");
if(val2)
else
if(val3)
else
Output:
Assignment 16
Q.16) Write a program to find whether an array is sorted or not.
import java.util.*;
Assignment 17
Q.17) Create a class game, which allows a user to play “Guess the number” game once.
// Java program for the above approach
import java.util.*;
// Given K trials
int K = 5;
int i, guess;
System.out.println(
"A number is chosen"
+ " between 1 to 100."
+ "Guess the number"
+ " within 5 trials.");
System.out.println(
"Guess the number:");
if (i == K) {
System.out.println(
"You have exhausted"
+ " K trials.");
System.out.println(
"The number was " + number);
}
}
// Driver Code
public static void
main(String arg[])
{
// Function Call
guessingNumberGame();
}
}
Output: