0% found this document useful (0 votes)
5 views

java_ass1

Uploaded by

honeytpatel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

java_ass1

Uploaded by

honeytpatel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Name : Jaydip Chauhan Batch:2023-24

JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 1


Enrollment No: Subject Code : 619402

/* Q1-Write a simple “Hello World” java program, compilation, debugging executing


using java compiler and interpreter. */
-> Source code
public class helloWorld
{
public static void main(String args[])
{
System.out.println("hello world");
}
}
-> Output

/* Q2-Write a program to pass Starting and Ending limit and print all prime numbers
and Fibonacci numbers between this range. */
->Source Code
import java.util.Scanner;
public class primeNumber
{
public static void main(String args[])
{
int start,end,i,j;
int flag=0;
Scanner n1 = new Scanner(System.in);
System.out.print("Enter starting value: ");
start = n1.nextInt();
System.out.print("Enter ending value: ");
end = n1.nextInt();

for(i=start;i<=end;i++)

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 2
Enrollment No: Subject Code : 619402

{
flag = 1;
for(j=2;j<i;j++)
{
if(i%j==0)
{
flag=0;
break;
}
}
if(flag==1)
{
System.out.println(i);
}
}
}
}
->Output

Q2-1 – Source Code


//Fibonacci series
import java.util.*;
public class fibonacci
{
public static void main(String args[])
{
int first = 0,sum,i second = 1;
Scanner s1 = new Scanner(System.in);
System.out.print("enter starting value:");
int start = s1.nextInt();

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 3
Enrollment No: Subject Code : 619402

System.out.print("enter ending value:");


int end = s1.nextInt();
for ( i = start; first + second <=end; i++)
{
sum = first + second;
first = second;
second = sum;
System.out.print(sum + " ");
}
}
}
->Output

/* Q3-Write a java program to check whether number is palindrome or not. Input: 528
Output: It is not palindrome Number Input: 545 Output: It is not palindrome number. */
->Source Code
import java.util.Scanner;
public class palinDrome

{
public static void main(String args[])
{
int num, realnum, revnum = 0, rem;

Scanner s1 = new Scanner(System.in);


System.out.print("Enter a number: ");
num = s1.nextInt();
realnum = num;
while (num != 0)
{

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 4
Enrollment No: Subject Code : 619402

rem = num % 10;


revnum = revnum * 10 + rem;
num = num / 10;
}
if (realnum == revnum)
{
System.out.println(realnum + " is a palindrome.");
}
else
{
System.out.println(realnum + " is not a palindrome.");
}
}
}
->Output

/* Q4- Write a java program to print value of x^n. Input: x=5 ,Input: n=3 Output: 125.*/
->Source code
import java.util.Scanner;
public class power
{
public static void main(String args[])
{
int base,power,i;
Scanner s1 = new Scanner(System.in);
System.out.print("enter number of base:");
base = s1.nextInt();
System.out.print("enter number of exponent:");

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 5
Enrollment No: Subject Code : 619402

power = s1.nextInt();
double result=1;
if(power>= 0)
{
for (i=0;i<power;i++)
{
result *= base;
}
}
System.out.println("the power is " + result);
}
}

->Output

/* Q5-Write a java program to check Armstrong number. Input: 153 Output: Armstrong
number Input: 22 Output: not Armstrong number. */
->Source code
import java.util.Scanner;
public class armstrongNumber
{
public static void main(String args[])
{
int number,rem,temp,result=0;
Scanner s1 = new Scanner(System.in);
System.out.print("enter any number:");
number = s1.nextInt();
temp = number;
while(number > 0)
{

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 6
Enrollment No: Subject Code : 619402

rem = number % 10;


result = (rem*rem*rem)+result;
number =number/10;
}
if(temp==result)
{
System.out.println(+ temp +" nummber is armstrong ....");
}
else
{
System.out.println(+ temp +" number is not armstrong ....");
}
}
}
->Output

/* Q6-Write a program in Java to find minimum of three numbers using conditional


operator.*/
->Source Code
import java.util.Scanner;
public class minimum
{
public static void main(String args[])
{
int first,second,third;

Scanner s1 = new Scanner(System.in);


System.out.print("enter first number:");

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 7
Enrollment No: Subject Code : 619402

first = s1.nextInt();
System.out.print("enter second number:");
second = s1.nextInt();
System.out.print("enter third number:");
third = s1.nextInt();

if(first < second && first < third)


{
System.out.print("first number is minimum " + first);
}
else if(second < third && second < first)
{
System.out.print("second number is minimum " + second);
}
else if(third < first && third < second)
{
System.out.print("third number is minimum "+ third);
}
else
{
System.out.print("some value are equal...");
}
}
}
->Output

/* Q7-Write a java program which should display maximum number of given 4


numbers.*/
->Source Code

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 8
Enrollment No: Subject Code : 619402

import java.util.Scanner;
public class maximum
{
public static void main(String args[])
{
int first,second,third,fourth;

Scanner s1 = new Scanner(System.in);


System.out.print("enter first number:");
first = s1.nextInt();
System.out.print("enter second number:");
second = s1.nextInt();
System.out.print("enter third number:");
third = s1.nextInt();
System.out.print("enter fourth number:");
fourth = s1.nextInt();

if(first > second && first > third && first > fourth)
{
System.out.print("first number is maximum " + first);
}
else if(second > third && second > first && second > fourth)
{
System.out.print("second number is maximum " + second);
}
else if(third > fourth && third > first && third > second)
{
System.out.print("third number is maximum "+ third);
}
else if(fourth > first && fourth > second && fourth > third)
{
System.out.print("fourth number is maximum "+ fourth);
}
else

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 9
Enrollment No: Subject Code : 619402

{
System.out.print("some value are same.....");
}
}
}

->Output

/* Q8-Write a program in Java to multiply two matrix. Declare a class Matrix where 2D
array is declared as instance variable and array should be initialized, within class.*/
->Source code
import java.util.Scanner;
public class multiplymatrix
{
public static void main(String args[])
{
int a[][]={{1,1},{1,1}};
int b[][]={{1,1},{1,1}};
int c[][]=new int[2][2];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
c[i][j]=0;
for(int k=0;k<2;k++)
{
c[i][j]+=a[i][j]*b[k][j];
}
System.out.print(c[i][j]+" ");

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 10
Enrollment No: Subject Code : 619402

}
System.out.println();
}
}
}
->Output

/* Q9-Write a java program to create a class “Matrix” that would contain integer
values having varied Numbers of columns for each row. Print row-wise sum of the
integer values for each row.*/
->Source Code
import java.util.*;
public class matrix9
{
public static void main(String args[])
{
int columns,i,j,sum=0,rows;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of rows: ");
rows = sc.nextInt();
System.out.print("Enter the number of columns: ");
columns = sc.nextInt();
int[][] matrix = new int[rows][columns];
System.out.println("Enter the matrix elements:");
for ( i = 0; i< rows; i++)
{
for (j = 0; j < columns; j++)
{
matrix[i][j] = sc.nextInt();
}
}

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 11
Enrollment No: Subject Code : 619402

for ( i = 0; i< rows; i++)


{
sum = 0;
for (j = 0; j < columns; j++)
{
sum += matrix[i][j];
}
System.out.println("Sum of elements in row " + i + ": " + sum);
}
}
}
->Output

/* Q10-Write a Java application which takes several command line arguments, which
are supposed to be names of students and prints output as given below: (Suppose we
enter 3 names then output should be as follows).. Number of arguments = 3
1.: First Student Name is = Arun
2.: Second Student Name is = Hiren
3.Third Student Name is = Hitesh */
->Source Code

import java.util.*;
public class comlinearg10
{

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 12
Enrollment No: Subject Code : 619402

public static void main(String args[])


{
System.out.println("First Student name: "+ args [0]);
System.out.println("second Student name: "+args [1]);
System.out.println("Third Student name: "+args [2]);
}
}
->Output

/* Q11- Write a Java application to count and display frequency of letters and digits
from the String given by user as command-line argument.*/
->Source Code
import java.util.Scanner;
public class frequency11
{
public static void main(String args[])
{
String str = args[0]; ;
int[] count = new int[str.length()];
int i, j;
char ch[] = str.toCharArray();
for(i = 0; i <str.length(); i++)
{
count[i] = 1;
for(j = i+1; j <str.length(); j++)
{
if(ch[i] == ch[j])
{
count[i]++;

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 13
Enrollment No: Subject Code : 619402

ch[j] = '0';
}
}
}
System.out.println("Characters and frequencies");
for(i = 0; i <ch.length; i++)
{
if(ch[i] != ' ' && ch[i] != '0')
{
System.out.println(ch[i] + "-" + count[i]);
}
}
}
}
->Output

/*q12-write a program in java to check number is odd or even.*/


->Source Code
import java.util.Scanner;
public class oddEven
{
public static void main(String args[])
{
int even ,odd;

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 14
Enrollment No: Subject Code : 619402

Scanner in1= new Scanner(System.in);


System.out.print("Enter any number:");
int number = in1.nextInt();
if(number <= 0)
{
System.out.println("please enter valid number....");
}
else if(number%2==0)
{
System.out.println("number is even:"+number);
}
else
{
System.out.println("number is odd:"+number);
}
}
}
->Output

/* q13-write a program for factorial number in java.*/


->Source Code
import java.util.Scanner;
public class factorial13
{
public static void main(String[] args)
{
int number,i;

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 15
Enrollment No: Subject Code : 619402

long factorial=1;
Scanner scanner = new Scanner(System.in);
System.out.print("Enter any number: ");
number = scanner.nextInt();
if (number < 0)
{
System.out.println("please enter positive number....");
}
else
{
for ( i = 1; i <= number; i++)
{
factorial = factorial*i;
}
System.out.println("Factorial of " + number + " is " + factorial);
}
}
}
->Output

/* q14-write a java program to print following pattern


*
* *
* * *
* * * *
* * * * * */
->Source Code
public class pattern14
{
public static void main(String args[])

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 16
Enrollment No: Subject Code : 619402

{
int i,j,rows = 5;
for (i = 1; i <= rows; i++)
{
for (j = 1; j <= i; j++)
{
System.out.print(" * ");
}
System.out.println();
}
}
}
->Output

/* q15-write a java program to print following pattern


*
* *
* * *
* * * *
* * * * * */
->Source Code
public class pattern15
{
public static void main(String args[])
{
int i, j, k;
for (i = 1; i <= 5; i++)
{

SAL Institute of Technology & Engineering Research Master of Computer Application


Name : Jaydip Chauhan Batch:2023-24
JAVA Subject : Object Oriented Programming In JAVA Semeseter : I 17
Enrollment No: Subject Code : 619402

for (j = i; j <= 5; j++)


{
System.out.print(" ");
}
for (k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
->Output

SAL Institute of Technology & Engineering Research Master of Computer Application

You might also like