Simple program to print factorial of given number using recursion
import [Link];
public class Main
{
public static void main(String[] args)
Scanner sc = new Scanner([Link]);
[Link]("Enter the number:");
int num = [Link](); //Input the number
if(num>=0)
int factorial=findFactorial(num);
[Link]("The factorial of the entered the number is:"+factorial);
else
[Link]("Factorial not possible.");
[Link]("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;
[Link]( "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)
[Link](x + " ");
}
}
public static void main(String[] args)
int N = 45
prime_N(N);
[Link]
class Fibonacci {
public static void main(String args[])
int n1 = 0 ,n2 = 1 , n3, i, count=10;
[Link](n1+" "+n2+ " "); for(i=2;i<count;++i)
{
n3=n1+n2;
[Link](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 ([Link] > 0)
{[Link]("Command-line arguments are:"); // Loop through each argument and print it
for (int i = 0; i < [Link]; i++)
[Link]("Argument" + (i + 1) + ":" + args[i]);
}
}
else
[Link]("No command-line arguments found.");
}}
Simple java program to create student information using array
import [Link];
public class StudentinfoArray
public static void main(String[] args)
Scanner scanner = new Scanner([Link]); // Ask for the number of students
[Link]("Enter the number of students: ");
int numberOfStudents = [Link](); // 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++)
[Link]("\nEnter details for Student " + (i + 1) + ":");
[Link](); // Consume newline
[Link]("Name: ");
names[i] = [Link]();
[Link]("Roll Number: ");
rollNumbers[i] = [Link]();
[Link]("Marks: ");
marks[i] = [Link](); } // Display student details
[Link]("\nStudent Information:");
for (int i = 0; i < numberOfStudents; i++)
[Link]("\nStudent" + (i + 1) + ":");
[Link]("Name: " + names[i]);
[Link]("Roll Number: " + rollNumbers[i]);
[Link]("Marks: " + marks[i]); }
[Link]();
}}
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()
{
[Link]("Hello geeks!! How are you?");
public static void main(String args[])
{
gfg obj =new gfg();
[Link]();
}}
import [Link];
Public class GFG {
public static void
main (String args[])
{
gfg obj =new gfg();
[Link]();
}}
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){[Link]("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(){[Link](id+" "+name);}
public static void main(String args[]){
/ /creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
[Link]();
[Link]();
}}
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(){[Link](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
[Link]();
[Link]();
}}
Write a program in Java to demonstrate various operations on string functions.
import [Link];
public class StringOperations
{
public static void main(String[] args)
Scanner sc = new Scanner([Link]); // Input two strings
[Link]("Enter the first string: ");
String str1 = [Link]();
[Link]("Enter the second string: ");
String str2 = [Link]();
// Length of the strings
[Link]("\nLength of first string: " + [Link]());
[Link]("Length of second string: " + [Link]());
// Character at a specific position
[Link]("Character at index 2 of first string: " + [Link](2));
// Substring from the string
[Link]("Substring of first string from index 2 to 5: " + [Link](2, 5)); //
Check if strings are equal
[Link]("Are both strings equal?". [Link](str2));
// Compare two strings lexicographically
[Link]("Comparing the two strings: " + [Link](str2));
// Convert to uppercase and lowercase
[Link]("First string in uppercase: " + [Link]());
[Link]("Second string in lowercase: " + [Link]
Case());
// Replace characters in a string
[Link]("First string after replacing 'a' with 'o': " [Link]('a', 'o'));
// Find index of a character or substring
[Link]("Index of 'a' in first string: " + [Link]('a'));
// Check if string starts with or ends with a specific substrin
[Link]("Does the first string start with 'He'? " + [Link]("He"));
[Link]("Does the second
string end with 'ing'? " + [Link]("ing"));
// Concatenate two strings
[Link]("Concatenating both strings: " +
[Link](str2));
[Link]();
}}
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
[Link](25);
// printing the values from object
[Link]([Link](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;
[Link](name); [Link](age); [Link](salary); }}
// Base class
class Base
public static void main(String args[])
{
Sunstar s = new Employee();
[Link]();