Java DSA

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 20

//printing Helllo world

public class firstProgram


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

//printing Helllo world


public class Main
{
public static void main(String[] args) {
int x=10;
int y=2;
System.out.print(x+y + " "+ 78 + 90 +" ");
System.out.print(x-y + " ");
System.out.print(x*y + " ");
System.out.print(x/y + " ");
System.out.print(x%y + " ");

}
}

//swapping of two numbers using temporary variable


public class Main
{
public static void main(String[] args) {
int num1=5 ,num2=10;
System.out.println("Before Swapping num1= " + num1 +" and num2 =" +
num2);

int temp=num1;
num1=num2;
num2=temp;

System.out.println("After Swapping num1= " + num1 +" and num2 =" +


num2);
}
}

//swapping of two numbers without using temporary variable


public class Main
{
public static void main(String[] args) {
int num1=5 ,num2=10;
System.out.println("Before Swapping num1= " + num1 +" and num2 =" +
num2);

num1= num1+num2;
num2=num1-num2;
num1=num1-num2;
System.out.println("After Swapping num1= " + num1 +" and num2 =" +
num2);
}
}

//swapping of two numbers without using temporary variable


public class Main
{
public static void main(String[] args) {
int num1=5 ,num2=10;
System.out.println("Before Swapping num1= " + num1 +" and num2 =" +
num2);

num1= num1*num2;
num2=num1/num2;
num1=num1/num2;

System.out.println("After Swapping num1= " + num1 +" and num2 =" +


num2);
}
}

// Area of a rectangle

import java.util.Scanner;
public class RectangleArea
{
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.print(" Enter the length of the rectangle ");
double length=scanner.nextDouble();
System.out.print(" Enter the width of the rectangle ");
double width=scanner.nextDouble();

double area=length*width;
System.out.println("The area of rectangle is " +area);
}
}

//pre increment and pre decrement

public class Operator


{
public static void main(String[] args) {
int x=10;
System.out.println(x++);//10
System.out.println(x);//11
System.out.println(++x);//12
System.out.println(x);//12
System.out.println(x--);//12
System.out.println(x);//11
System.out.println(--x);//10
System.out.println(x);//10
}
}

//pre increment and pre decrement

public class Operator


{
public static void main(String[] args) {
int x=10;
int y=10;
System.out.println(x++ + ++x + x);
System.out.println(y++ + y++ + y);

}
}

//assignment operator

public class Operator


{
public static void main(String[] args) {
int x=10;
int y=20;

x+=5;// x=x+5;
y-=6;//y=y-6;
System.out.println(x++ + " " + ++y);

}
}

//assignment operator

public class Operator


{
public static void main(String[] args) {
int x=10;
int y=20;

x+=5;// x=x+5;
y-=6;//y=y-6;
System.out.println(x++ + ++x);

}
}
//Simple interest calculation

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);

System.out.print("Enter the principal amount : ");


double principal=scanner.nextDouble();

System.out.print("Enter the arte of interest : ");


double rate=scanner.nextDouble();

System.out.print("Enter the time period : ");


double time=scanner.nextDouble();

double simpleInterest =(principal*rate*time)/100;


System.out.println("Simple Interest : " +simpleInterest);
}
}

//Distance between two points

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);

System.out.print("Enter x1 : ");
double x1=scanner.nextDouble();
System.out.print("Enter y1: ");
double y1=scanner.nextDouble();

System.out.print("Enter x2 : ");
double x2=scanner.nextDouble();
System.out.print("Enter y2: ");
double y2=scanner.nextDouble();

double distance =Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2));


System.out.println("Distance between the two points : " +distance);
}
}

//leap year

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.print("Enter a year : ");
int year=scanner.nextInt();
if((year%4==0 && year%100 !=0) || year%400==0){
System.out.println(year + " is a leap year");
}else{
System.out.println(year + " is not a leap year");
}

}
}

//Sign of a Number

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);

System.out.print("Enter a Integer : ");


int number=scanner.nextInt();
if(number>0){
System.out.println("Positive Number");
}else if(number<0){
System.out.println("Negative Number");
}else{
System.out.println("Zero");
}

}
}

//Vowel or Consonant

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);

System.out.print("Enter a Character : ");


char ch=scanner.next().charAt(0);
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u' || ch=='A' ||
ch=='E' || ch=='I' || ch=='O' || ch=='U'){
System.out.println("Vowel");
}else{
System.out.println("Consonant");
}

}
}

//Hello World for loop


public class Main
{
public static void main(String[] args) {
for(int i=1;i<=10;i++){
System.out.print("Hello World" + " ");
}
}
}

//Print natural numbers till 100


public class Main
{
public static void main(String[] args) {
for(int i=1;i<=100;i++){
System.out.print(i + " ");
}
}
}

//Print natural numbers till 100


public class Main
{
public static void main(String[] args) {
for(int i=40;i<=100;i++){
System.out.print(i*2 + " ");
}
}
}

//Multiplication Table
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.println("Enter the Number :");
int number =scanner.nextInt();

System.out.println("Multiplication table of " + number + " : ");


for(int i=1;i<=10;i++){
int product=number*i;
System.out.println(number + " * " + i + " = " + product);
}
}
}

//Sum of all numbers


public class Main
{
public static void main(String[] args) {
int sum=0;
for(int i=1;i<=100;i++){
sum=sum+i;
}
System.out.println("Sum is " + sum);
}
}

//factorial of numbers
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.println("Enter a number : ");
int number=scanner.nextInt();
if(number<0){
System.out.println("Factorial of negative number is not defined ");
}else{
int factorial=1;
for(int i=1;i<=number;i++){
factorial*=i; //factorial=factorial*i;
}
System.out.println("Factorial of " + number + " is " +factorial);

}
}

//use of continue keyword


public class Main
{
public static void main(String[] args) {
for(int i=0;i<=20;i++){
if(i%3==0){
continue;
}
System.out.print(i +" ");
}

}
}

//use of break keyword


public class Main
{
public static void main(String[] args) {
for(int i=0;i<=20;i++){
if(i==11){
break;
}
System.out.print(i +" ");
}

}
}

//chack if a number is prime


import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.println("Enter a number : ");
int number=scanner.nextInt();
if(number<=1){
System.out.println("Number is Not a prime ");
}else{
boolean isPrime=true;
for(int i=2;i<=number/2;i++){
if(number% i==0){
isPrime=false;
break;
}
}
if(isPrime){
System.out.println(number+ " is a prime Number ");
}else{
System.out.println(number+ " is not a prime Number ");
}

//Prime numbers between two numbers : 2 to 100


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

System.out.println("Prime Numbers : ");


for(int num=2;num<=100;num++){
boolean isPrime=true;
for(int i=2;i<=num/2;i++){
if(num% i==0){
isPrime=false;
break;
}
}
if(isPrime){
System.out.print(num+ " ");
}
}

//Prime numbers between two numbers

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scannner =new Scanner(System.in);
System.out.println("Enter the Start Number : ");
int start=scannner.nextInt();

System.out.println("Enter the Ending Number : ");


int end=scannner.nextInt();

System.out.println("Prime Numbers : ");


for(int num=start;num<=end;num++){
if(num<=1){
continue;
}
boolean isPrime=true;
for(int i=2;i<=num/2;i++){
if(num% i==0){
isPrime=false;
break;
}
}
if(isPrime){
System.out.print(num+ " ");
}
}

}
//how many Prime numbers between two numbers

import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scannner =new Scanner(System.in);
System.out.println("Enter the Start Number : ");
int start=scannner.nextInt();

System.out.println("Enter the Ending Number : ");


int end=scannner.nextInt();

System.out.println("Prime Numbers : ");


int count =0;
for(int num=start;num<=end;num++){
if(num<=1){
continue;
}
boolean isPrime=true;
for(int i=2;i<=num/2;i++){
if(num% i==0){
isPrime=false;
break;
}
}
if(isPrime){
count++;
}
}
System.out.println("Number of prime Numbers between "+ start + " And
" +end + " : " + count);

//print numbers from 1 to 100 using while loop


public class Main
{
public static void main(String[] args) {
int num =1;
while(num<=100){
System.out.print(num + " ");
num++;

}
}
}
//print numbers from 1 to 100 using while loop
public class Main
{
public static void main(String[] args) {
int num =90;
while(num>=-10){
System.out.print(num + " ");
num--;

}
}
}

//Sum of natural numbers


public class Main
{
public static void main(String[] args) {
int num=1;
int sum=0;
while(num<=100){
sum=sum+num;
num++;
}
System.out.println("Sum of natural no. " + sum);
}
}

//No. of prime and composite numbers between two numbers

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

Scanner scanner= new Scanner (System.in);

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


int start= scanner.nextInt();

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


int end= scanner.nextInt();

int primeCount=0;
int compositeCount=0;

int num=start;
while(num<=end){
if(num<=1){
num++;
continue;
}

boolean isPrime=true;
for(int i=2;i<=num/2;i++){
if(num%i==0){
isPrime=false;
break;
}
}
if(isPrime){
primeCount++;
}else{
compositeCount++;
}
num++;

}
System.out.println("Number of prime numbers between " +start + " and "
+ end + " : "+ primeCount);
System.out.println("Number of composite numbers between " +start + "
and " + end + " : "+ compositeCount);
}
}

//No. of prime and composite numbers between two numbers

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

Scanner scanner= new Scanner (System.in);

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


int start= scanner.nextInt();

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


int end= scanner.nextInt();

int primeSum=0;
int compositeSum=0;

int num=start;
while(num<=end){
if(num<=1){
num++;
continue;
}

boolean isPrime=true;
for(int i=2;i<=num/2;i++){
if(num%i==0){
isPrime=false;
break;
}
}
if(isPrime){
primeSum=primeSum+num;
}else{
compositeSum=compositeSum+num;
}
num++;

}
int difference= primeSum-compositeSum;
System.out.println("Sum of prime numbers between " + primeSum);
System.out.println("Sum of composite numbers between " + compositeSum);
System.out.println("Difference: " + difference);
}
}

//use of do while loop : print first 100 natural numbers


public class Main
{
public static void main(String[] args) {
int count =1;
do{
System.out.print(count + " ");
count++;
}while(count<=100);
}
}

//use of do while loop : sum of natural number


public class Main
{
public static void main(String[] args) {
int count =1;
int sum=0;
do{
sum+=count;
count++;
}while(count<=100);
System.out.print("Sum of natural numbers : " + sum);
}
}

//use of do while loop : count no. of digits in a number


import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
System.out.println("Enter a number ");
int number= scanner.nextInt();

int count=0;
int temp=number;
do{
temp=temp/10;
count++;

}while(temp!=0);
System.out.print("Number of digits in " + number +" : "+ count );
}
}

//maximum between two numbers using function


public class Main
{
public static int findMax(int num1,int num2){
if(num1>num2){
return num1;
}else{
return num2;
}
}

public static void main(String[] args) {


int a=56;
int b=39;
int max=findMax(a,b);
System.out.println("Maximum of "+ a + " and "+ b + " is "+ max);
}
}

//Factorial using function


public class Main
{
public static int calculateFactorial(int num){
if(num==0 || num==1){
return 1;
}
int fact=1;
for(int i=2;i<=num;i++){
fact*=i;
}
return fact ;
}

public static void main(String[] args) {


int number=0;
int factorial=calculateFactorial(number);
System.out.println("Factorial of no. "+ number +" is "+ factorial);
}
}

//prime or not using function


import java.util.*;
public class Main
{
public static boolean isPrime(int num){
if(num<=1)
return false;
else{
for(int i=2;i<=Math.sqrt(num);i++){
if(num%i==0)
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int n=sc.nextInt();
boolean isPrime=isPrime(n);
if(isPrime){
System.out.println(n+ " is a Prime number");
}
else {
System.out.println(n+ " is Not a prime");
}
}
}

//palindrome or not using function


import java.util.*;
public class PalindromeNumber{
public static int reverseNumber(int num) {
if (num >= 0 && num <= 9) {
return num;
}
int reverseNum = 0;
while (num != 0) {
int lastDigit = num % 10;
reverseNum = reverseNum * 10 + lastDigit;
num = num / 10;
}

return reverseNum;

public static boolean isPalindrome(int num) {


// int x1 = reverseNumber(num);
// if (num == x1) {
// return true;
// }
// return false;
int original=num;
int reversed=reverseNumber(num);
return original==reversed;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("enter no");
int number = sc.nextInt();
if (isPalindrome(number)) {
System.out.println(number + " is a palindrome");
} else {
System.out.println(number + "is not a palindrome");
}

}
}

//calculator
import java.util.Scanner;
public class SimpleCalculator
{

public static void main(String[] args) {


Scanner scanner =new Scanner(System.in);
boolean exit=false;
System.out.println("Welcome to the Calculator : ");

while(!exit){
System.out.println("Choose an Operator ");
System.out.println("1. Addition (+) ");
System.out.println("2. Substraction (-) ");
System.out.println("3. Multipliction (*) ");
System.out.println("4. Division (/)");
System.out.println("5. Square root (sqrt) ");
System.out.println("6. Swap Two Numbers (swap) ");

System.out.println("\nEnter your choice (1-6) : ");


int choice=scanner.nextInt();

switch(choice){
case 1:
performAddition(scanner);
break;
case 2:
performSubstraction(scanner);
break;
case 3:
performMultiplication(scanner);
break;
case 4:
performDivision(scanner);
break;
case 5:
performSquareRoot(scanner);
break;
case 6:
performSwap(scanner);
break;
default:
System.out.println("Invalid Choice , Please select a valid
Operation");
}
System.out.print("Do you want to perform another Calculation ?
(yes/no)");
String continueCalculation=scanner.next();

exit=!continueCalculation.equalsIgnoreCase("yes");

System.out.println("Calculator exited, Thank You ");


}

private static void performAddition(Scanner scanner ){


System.out.print("Enter the first Number ");
double num1=scanner.nextDouble();

System.out.print("Enter the Second Number ");


double num2=scanner.nextDouble();

double result=num1+num2;
System.out.println("Result of Addition : "+ result);
}

private static void performSubstraction(Scanner scanner ){


System.out.print("Enter the first Number ");
double num1=scanner.nextDouble();

System.out.print("Enter the Second Number ");


double num2=scanner.nextDouble();

double result=num1-num2;
System.out.println("Result of Substraction : "+ result);
}
private static void performMultiplication(Scanner scanner ){
System.out.print("Enter the first Number ");
double num1=scanner.nextDouble();

System.out.print("Enter the Second Number ");


double num2=scanner.nextDouble();

double result=num1*num2;
System.out.println("Result of Multipliction : "+ result);
}
private static void performDivision(Scanner scanner ){
System.out.print("Enter the first Number ");
double num1=scanner.nextDouble();

System.out.print("Enter the Second Number ");


double num2=scanner.nextDouble();

if(num2!=0){
double result=num1/num2;
System.out.println("Result of Addition : "+ result);
}else{
System.out.println("Can not divide a number by 0 ");
}

}
private static void performSquareRoot(Scanner scanner ){
System.out.print("Enter the Number ");
double num=scanner.nextDouble();

if(num>=0){
double result= Math.sqrt(num);
System.out.println("Square Root of " + num + " : "+ result);
}else{
System.out.println("Invalid Input ");
}
}

private static void performSwap(Scanner scanner ){


System.out.print("Enter the first Number ");
double num1=scanner.nextDouble();

System.out.print("Enter the Second Number ");


double num2=scanner.nextDouble();

System.out.println("Values Before Swapping : x = "+num1 + ", y = " +num2


);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
System.out.println("Values After Swapping : x = "+num1 + " , y = " +num2
);
}

//print n natural numbers using recursion


public class Main
{

public static void printNumber(int n){


if(n>0){

printNumber(n-1);//recursive case
System.out.print(n +" ");

}
}
public static void main(String[] args) {
int n=100;
System.out.println("Numbers from 1 to " + n + " : ");
printNumber(n);
}
}

//print n natural numbers and reverse using recursion


public class Main
{

public static void printNumber(int n){


if(n>0){
System.out.print(n +" ");
printNumber(n-1);//recursive case
System.out.print(n +" ");

}
}
public static void main(String[] args) {
int n=100;
System.out.println("Numbers from 1 to " + n + " : ");
printNumber(n);
}
}

//Factorial of a number using recursion


public class Main
{

public static int factorial(int n){


//base case
if(n==0 || n==1){
return 1;
}
//recursive case
return n*factorial(n-1);
}
public static void main(String[] args) {
int n=10;
int result= factorial(n);
System.out.println("Factorial of " + n + " is : " + result );

}
}

//Sum of digits using recursion


public class Main
{

public static int sumOfDigits(int n){


//base case
if(n==0){
return 0;
}
//recursive case
return n%10 + sumOfDigits(n/10);
}
public static void main(String[] args) {
int n=12345;
int result= sumOfDigits(n);
System.out.println("sumOfDigits " + n + " is : " + result );

}
}

You might also like