You are on page 1of 18

Assignment 1

Q.1) Write a program in java to print Hello World.


import java.util.*;

public class hi{


public static void main(String[] args) {
System.out.println("Hello World");

}
}

Output:

Assignment 2
Q.2) Write a program to find a factorial of a number.
import java.util.*;

public class prog1{

//program to calculate factorial by using recursion function


static int factorial(int n){
if (n == 0)
return 1;
else
return(n * factorial(n-1));
}
public static void main(String args[]){
int i,fact=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
int number=sc.nextInt();//It is the number to calculate factorial
fact = factorial(number);
System.out.println("Factorial of "+number+" is: "+fact);
}
}

Output:

Assignment 3
Q.3) Write a program to find the area of a triangle.
import java.util.Scanner;

public class prog2 {


// program to calculate the area of the triangle.
public static void main(String[] args) {
float b,h,area;
Scanner bh = new Scanner (System.in);
System.out.println("ENTER the value of Base and Height");//taking
input
b=bh.nextFloat();
h=bh.nextFloat();
area=(b*h)/2;// formula implementation
System.out.println("The Area of triangle is:"+area);// answer.
}
}

Output:
ASSIGNMENT-4
Q.4) Write a program to find the sum of three numbers.
import java.util.Scanner;

public class ass4 {

public static void main(String[] args) {

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:
Assignment 5
Q.5) Write a program to create an object in java.
import java.util.*;

public class prog3


{

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.*;

public class star {


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,rows;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE NO OF ROWS:\n\n");
rows=sc.nextInt();
// outer loop for rows
for(i=0;i<rows;i++)
{
// inner loops for columns
for(j=0;j<=i;j++)
{
// print stars
System.out.print("*");
}
// throws the cursor in a new line after printing each line
System.out.println();
}
}

Output:

Assignment 8
Q.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++;
}

//Displays the total number of characters present in the given


string
System.out.println("Total number of characters in a string: " +
count);
}
}

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}};

//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
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);

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:
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.*;

public class ass11 {

public static void main(String[] args) {

Scanner sc =new Scanner(System.in);

String name;

System.out.println("Enter you name");

name = sc.nextLine();

System.out.println("Hello,"+name+"have a good day!");

}
Output:

Assignment 12
Q.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 13
Q.13) Write a program to replace space with underscore.
import 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:

Assignment 14
Q.14) 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:

ASSIGNMENT-15
Q.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 you website:");

site = sc.nextLine();

String find =".com";

boolean val = site.contains(find);

if(val)

System.out.println("the url is .com");

else
System.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:
Assignment 16
Q.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
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.*;

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(
"Congratulations!"
+ " You guessed the number.");
break;
}
else if (number > guess
&& i != K - 1) {
System.out.println(
"The number is "
+ "greater than " + guess);
}
else if (number < guess
&& i != K - 1) {
System.out.println(
"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:

You might also like