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

Java

Uploaded by

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

Java

Uploaded by

yasha.bharambe25
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Simple program to print factorial of given number using recursion

import java.util.Scanner;
public class Main
{
public static void main(String[] args)

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number:");

int num = sc.nextInt(); //Input the number

if(num>=0)

int factorial=findFactorial(num);

System.out.println("The factorial of the entered the number is:"+factorial);

else

System.out.println("Factorial not possible.");

System.out.println("Please enter valid input.");

//Recursive Function to Find the Factorial of a Number

public static int find Factorial(int num)

if(num==0)

return 1;
else if(num==1)
return 1;
else
return num*findFactorial(num-1);
}}

Simple jave to print prime numbers up to given number

class prime

{ static void prime_N(int N)

int x, y, flg;
System.out.println( "All the Prime numbers within 1 and "+ N +" are:");
for (x = 1; x <= N; x++)

if (x==1|| x== 0)

continue;
flg=1;

for(y=2;y<=x/2;++y)

(x%y==0)

flg= 0 ;

break;

if (flg == 1)

System.out.print(x + " ");

}
}

public static void main(String[] args)

int N = 45

prime_N(N);

Fibonacci.java

class Fibonacci {

public static void main(String args[])

int n1 = 0 ,n2 = 1 , n3, i, count=10;


System.out.print(n1+" "+n2+ " "); for(i=2;i<count;++i)

{
n3=n1+n2;
System.out.print(n3 +" ");

n1=n2

n2 =n3

}
}}

Write java Program to demonstrate Command Line Arguments

public class CommandLineDemo


{

public static void main(String[] args)

{
// Check if there are any arguments

if (args.length > 0)

{System.out.println("Command-line arguments are:"); // Loop through each argument and print it


for (int i = 0; i < args.length; i++)

System.out.println("Argument" + (i + 1) + ":" + args[i]);


}
}

else

System.out.println("No command-line arguments found.");

}}
Simple java program to create student information using array

import java.util.Scanner;

public class StudentinfoArray

public static void main(String[] args)

Scanner scanner = new Scanner(System.in); // Ask for the number of students

System.out.print("Enter the number of students: ");

int numberOfStudents = scanner.nextInt(); // Arrays to store student data

String[] names = newString[numberOfStudents];

int[] rollNumbers = new int[numberOfStudents];


float[] marks = new float[numberOfStudents]; // Input student details

for (int i = 0; i < numberOfStudents; i++)

System.out.println("\nEnter details for Student " + (i + 1) + ":");

scanner.nextLine(); // Consume newline

System.out.print("Name: ");

names[i] = scanner.nextLine();

System.out.print("Roll Number: ");

rollNumbers[i] = scanner.nextInt();

System.out.print("Marks: ");
marks[i] = scanner.nextFloat(); } // Display student details

System.out.println("\nStudent Information:");

for (int i = 0; i < numberOfStudents; i++)

System.out.println("\nStudent" + (i + 1) + ":");

System.out.println("Name: " + names[i]);

System.out.println("Roll Number: " + rollNumbers[i]);

System.out.println("Marks: " + marks[i]); }

scanner.close();

}}

Write a program in Java to implement user defined package.

// Java program to create a user- defined


// package and function to print

// a message for the users

package example;

// Class

public class gfg {

public void show()


{
System.out.println("Hello geeks!! How are you?");

public static void main(String args[])


{
gfg obj =new gfg();

obj.show();

}}

import example.gfg;
Public class GFG {
public static void
main (String args[])
{
gfg obj =new gfg();
obj.show();

}}

//Java Program to create and call a default constructor

class Bike1{

//creating a default constructor

Bike1(){System.out.println("Bike is created");}

//main method
public static void main(String args[]){

//calling a default constructor

Bike1 b=new Bike1();

Let us see another example of default constructor


//which displays the default values

class Student3{

int id;

String name;

//method to display the value of id and name

void display(){System.out.println(id+" "+name);}

public static void main(String args[]){

/ /creating objects

Student3 s1=new Student3();

Student3 s2=new Student3();

//displaying values of the object

s1.display();

s2.display();

}}

Java Program to demonstrate the use of the parameterized constructor.

class Student4{

int id;
String name;

//creating a parameterized constructo

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

}}

Write a program in Java to demonstrate various operations on string functions.

import java.util.Scanner;

public class StringOperations

{
public static void main(String[] args)

Scanner sc = new Scanner(System.in); // Input two strings

System.out.print("Enter the first string: ");

String str1 = sc.nextLine();

System.out.print("Enter the second string: ");

String str2 = sc.nextLine();

// Length of the strings

System.out.println("\nLength of first string: " + str1.length());

System.out.println("Length of second string: " + str2.length());

// Character at a specific position

System.out.println("Character at index 2 of first string: " + str1.charAt(2));

// Substring from the string

System.out.println("Substring of first string from index 2 to 5: " + str1.substring(2, 5)); //

Check if strings are equal

System.out.println("Are both strings equal?". str1.equals(str2));

// Compare two strings lexicographically

System.out.println("Comparing the two strings: " + str1.compareTo(str2));

// Convert to uppercase and lowercase


System.out.println("First string in uppercase: " + str1.toUpperCase());

System.out.println("Second string in lowercase: " + str2.toLower


Case());

// Replace characters in a string


System.out.println("First string after replacing 'a' with 'o': " str1.replace('a', 'o'));

// Find index of a character or substring

System.out.p.intln("Index of 'a' in first string: " + str1.indexOf('a'));

// Check if string starts with or ends with a specific substrin

System.out.println("Does the first string start with 'He'? " + str1.startsWith("He"));


System.out.println("Does the second

string end with 'ing'? " + str2.endsWith("ing"));

// Concatenate two strings

System.out.println("Concatenating both strings: " +

str1.concat(str2));

sc.close();

}}

Program in java to Demonstrate Wrapper classess.

class Autoboxing {

public static void main(String[] args)

char ch = 'a';

// Autoboxing- primitive to Character object

// conversion

Character a= ch;

ArrayList<Integer> arrayList
= new ArrayList<Integer>();

// Autoboxing because ArrayList stores only


objects

arrayList.add(25);

// printing the values from object

System.out.println(arrayList.get(0));

}}

Program in java to demonstrate abstract Class

// Abstract class

abstract class Sunstar {

abstract void printinfo(); }

// Abstraction performed using extends

class Employee extends Sunstar

void printInfo() {

String name = "avinash";

int age = 21;

float salary = 222.2F;

System.out.println(name); System.out.println(age); System.out.println(salary); }}

// Base class

class Base

public static void main(String args[])

{
Sunstar s = new Employee();

s.printInfo();

You might also like