Java DSA
Java DSA
Java DSA
}
}
int temp=num1;
num1=num2;
num2=temp;
num1= num1+num2;
num2=num1-num2;
num1=num1-num2;
System.out.println("After Swapping num1= " + num1 +" and num2 =" +
num2);
}
}
num1= num1*num2;
num2=num1/num2;
num1=num1/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);
}
}
}
}
//assignment operator
x+=5;// x=x+5;
y-=6;//y=y-6;
System.out.println(x++ + " " + ++y);
}
}
//assignment operator
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);
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();
//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);
}
}
//Vowel or Consonant
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner scanner =new Scanner(System.in);
}
}
//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();
//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);
}
}
}
}
}
}
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();
}
//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();
}
}
}
//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--;
}
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
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);
}
}
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
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);
}
}
int count=0;
int temp=number;
do{
temp=temp/10;
count++;
}while(temp!=0);
System.out.print("Number of digits in " + number +" : "+ count );
}
}
return reverseNum;
}
}
//calculator
import java.util.Scanner;
public class SimpleCalculator
{
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) ");
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");
double result=num1+num2;
System.out.println("Result of Addition : "+ result);
}
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();
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();
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 ");
}
}
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);
}
}
}
}
public static void main(String[] args) {
int n=100;
System.out.println("Numbers from 1 to " + n + " : ");
printNumber(n);
}
}
}
}
}
}