Assignment
Assignment 1: Write a program in java to print hello world.
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!"); }}
Output:
java -cp /tmp/YWtpTKLfNX HelloWorld
Hello, World!
Assignment 2: Write a program to find factorial of a number .
import java.util.Scanner;
public class factacan {
public static void main(String[] args)
{ int num, i, fact=1;
Scanner s = new Scanner(System.in);
System.out.print("Enter a Number: ");
num = s.nextInt();
for(i=num; i>=1; i--)
{
fact = fact*i;
}
System.out.println("\nFactorial Result = " +fact);}}
Output:
Enter a Number: 5
Factorial Result = 120
Assignment 3: Write a program to find area of trinagle.
import java.util.Scanner;
public class AreaOfTriangle {
public static void main(String args[]){
int height, base, area;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the height of the triangle ::");
height = sc.nextInt();
System.out.println("Enter the base of the triangle ::");
base = sc.nextInt();
area = (height* base);
System.out.println("Area of the triangle is ::"+area);}}
Output: Enter the height of the triangle ::
15
Enter the base of the triangle ::
10
Area of the triangle is ::150
Assignment 4: Write a program to find sum of three numbers.
import java.util.Scanner;
public class sumof3num {
public static void main(String[] args) 3
5
{ int a,b,c,d;
System.out.println("Enter three number to find sum:");
Scanner sc= new Scanner(System.in);
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
d=a+b+c;
System.out.println("The sum of three numbers are: "+d);}}
Output:
Enter three number to find sum:
5
69
8
The sum of three numbers are: 82
Assignment 5: Write a program to create object in java.
-
public class CreateObject
{ void show()
{ System.out.println("Welcome to java lab students");
}
public static void main(String[] args)
{
//creating an object using new keyword
CreateObject obj = new CreateObject();
//invoking method using the object
obj.show(); }}
Output: Welcome to java lab students
Assignment 6: Write a program how to use constructor in java.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(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
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display(); }}
Output: a\jdt_ws\java_ec60db25\bin' 'Student4'
111 Karan
222 Aryan
Assignment 6: Write a program to print right angle triangle pattern
java.
public class RightTrianglePattern {
public static void main(String args[])
{
//i for rows and j for columns
//row denotes the number of rows you want to print
int i, j, row=6;
//outer loop for rows
for(i=0; i<row; i++)
{
//inner loop for columns
for(j=0; j<=i; j++)
{
//prints stars
System.out.print("* ");
}
//throws the cursor in a new line after printing each line
System.out.println(); }}}
Output:
*
* *
* * *
* * * *
* * * * *
* * * * * *
Assignment 8: Write a program to count the total number of
characters in a string.
import java.util.*;
public class CountCharacter
{public static void main(String[] args) { Scanner sc= new
Scanner (System.in);
System.out.println("Enter the string to calculate it's
length\n");
String string = sc.nextLine();
int count = 0;
//Counts each character except space
for(int i = 0; i < string.length();
i++) { if(string.charAt(i) != ' ')
count++;}
System.out.println("Total number of characters in a string: " +
count);}}
Output:
Enter the string to calculate it’s length
Preetam singh
Total number of characters in a string:9
Assignment 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}}
;
//creating another matrix to store the sum of two
matrices int c[][]=new int[3][3]; //3 rows and 3
columns
//adding and printing addition of 2 matrices
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j]; //use - for
subtraction System.out.print(c[i][j]+" ");
}
System.out.println();//new line}}}
Output:
Assignment 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);
System.out.println("Enter the marks of
subject-1:");
a=sc.nextInt();System.out.println("Enter the
marks of subject-2:"); b=sc.nextInt();
System.out.println("Enter the marks of
subject-3:"); c=sc.nextInt();
System.out.println("Enter the marks of
subject-4:"); d=sc.nextInt();
System.out.println("Enter the marks of
subject-5:"); e=sc.nextInt();
per = ((a+b+c+d+e)/400)*100;
System.out.println("your percentage is :"+per+"%");}}
Output:
Enter the maraks of subject-1
65
Enter the maraks of subject-2
45
Enter the maraks of subject-3
77
Enter the maraks of subject-4
88
Enter the maraks of subject-5
11
Your percentage is :57.2%
Assignment 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.*;
public class ass11 {
public static void main(String[] args) { Scanner sc =new
Scanner(System.in); String name;
System.out.println("Enter your name");
name = sc.nextLine();
System.out.println("Hello,"+name+" have a good day!");}}
Output:
Enter your name
Preetam
Hello,Preetam have a good day
Assignment 12: Write a program to convert a string to lowercase.
import java.util.*;
public class ass12 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String text;
System.out.println("Enter a string:");
text = sc.nextLine();
System.out.println(text.toLowerCase());}}
Output:
Assignment 10
Assignment 10
Assignment 13: Write a program to replace space with underscore.
mport java.util.*;
public class ass13
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
String text;
System.out.println("Enter the text:");
text = sc.nextLine();
char ch = '_';
String text1 = text.replace(' ',ch);
System.out.println(text1);}}
Output:
Enter the text:
This is java lab
This_is_java_lab
Assignment14. Write a program to find a year entered by the user is
leap year or not.
import java.util.*;
public class ass14 {
public static void main(String[] args) {
Scanner sc = new Scanner (System.in);
int a;
System.out.println("Enter A year:");
a = sc.nextInt();
if((a%400==0) ||(a % 100 != 0) && (a % 4 == 0)){
System.out.println("It is a leap year.");}
Else
System.out.println("It is not a leap year.");}}
Output:
Enter a year:
2000
It is a leap year
Assignment 15: Write a program to find out the type of website from
the URL: .com, .in, .org
import java.util.*;
public class ass15 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String site;
System.out.println("Enter your
website:"); site = sc.nextLine();
String find =".com";
boolean val = site.contains(find);
if(val)
System.out.println("the url is .com");
Else
ystem.out.println("String not found");
String find2 = ".org";
boolean val2 = site.contains(find2);
if(val2)
System.out.println("the url is .org");
Else
System.out.println("String not found");
String find3 = ".in";
boolean val3 = site.contains(find3);
if(val3)
System.out.println("the url is .in");
else
System.out.println("String not found");}}
Output:
Enter your website:
www.google.com
the url is .com
String not found
String not found
Assignment 16: Write a program to find whether an array is sorted or not.
import java.util.*;
public class ass16 {
static boolean check(int b[],int n){
if(n==0 || n==1)
return true;
return b[n - 1] >= b[n - 2] && check(b,n-1);}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int [5];
System.out.print("ENter the elements of array:");
for(int i = 0; i<5;i++)
{System.out.println("a["+ (i+1) +"]="); a[i]=sc.nextInt();
}
System.out.println("The entered elements of the array are :");
for(int i =0;i<5;i++){
System.out.println(a[i]+" ");
}
int n = a.length;
System.out.println();
if(check(a,n))
System.out.println("The array is sorted.");
else
System.out.println("The array is not
sorted"); }}
Output:
Assignment 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.*;
public class game1 {
// Function that implements the
// number guessing game
public static void
guessingNumberGame()
{// Scanner Class
Scanner sc = new Scanner(System.in);
// Generate the numbers
int number = 1 + (int)(100
* Math.random());
// 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.");
// Iterate over K
Trials for (i = 0; i
< K; i++) {
System.out.println(
"Guess the number:");
// Take input for
guessing guess =
sc.nextInt();
// If the number is
guessed if (number ==
guess) {
System.out.println(
"Congratulation
s!"
+ " You guessed the number.");
break;}
else if (number > guess
&&i != K - 1) {
System.out.printl
n( "The number
is " "greater
than " +
guess);}
else if (number < guess
&&i != K - 1) {
System.out.print
ln( "The
number is"
+ " less than " + guess);}}
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:
}